8000 [Mailer] Use recipients in sendmail transport · symfony/symfony@435d84b · GitHub
[go: up one dir, main page]

Skip to content

Commit 435d84b

Browse files
committed
[Mailer] Use recipients in sendmail transport
1 parent ab136f0 commit 435d84b

File tree

3 files changed

+76
-0
lines changed

3 files changed

+76
-0
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/usr/bin/env php
2+
<?php
3+
$argsPath = sys_get_temp_dir().\DIRECTORY_SEPARATOR.'sendmail_args';
4+
5+
file_put_contents($argsPath, implode(' ', $argv));
6+
7+
while (!feof(\STDIN)) {
8+
fread(\STDIN, 1024);
9+
}

src/Symfony/Component/Mailer/Tests/Transport/SendmailTransportTest.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,71 @@
1212
namespace Symfony\Component\Mailer\Tests\Transport;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Mailer\DelayedEnvelope;
1516
use Symfony\Component\Mailer\Transport\SendmailTransport;
17+
use Symfony\Component\Mime\Address;
18+
use Symfony\Component\Mime\Email;
1619

1720
class SendmailTransportTest extends TestCase
1821
{
22+
private const FAKE_SENDMAIL = __DIR__.'/Fixtures/fake-sendmail.php -t';
23+
24+
/**
25+
* @var string
26+
*/
27+
private $argsPath;
28+
29+
protected function setUp(): void
30+
{
31+
$this->argsPath = sys_get_temp_dir().\DIRECTORY_SEPARATOR.'sendmail_args';
32+
}
33+
34+
protected function tearDown(): void
35+
{
36+
@unlink($this->argsPath);
37+
unset($this->argsPath);
38+
}
39+
1940
public function testToString()
2041
{
2142
$t = new SendmailTransport();
2243
$this->assertEquals('smtp://sendmail', (string) $t);
2344
}
45+
46+
public function testToIsUsedWhenRecipientsAreNotSet()
47+
{
48+
$mail = new Email();
49+
$mail
50+
->from('from@mail.com')
51+
->to('to@mail.com')
52+
->subject('Subject')
53+
->text('Some text')
54+
;
55+
56+
$envelope = new DelayedEnvelope($mail);
57+
58+
$sendmailTransport = new SendmailTransport(self::FAKE_SENDMAIL);
59+
$sendmailTransport->send($mail, $envelope);
60+
61+
$this->assertStringEqualsFile($this->argsPath, __DIR__.'/Fixtures/fake-sendmail.php -ffrom@mail.com to@mail.com');
62+
}
63+
64+
public function testRecipientsAreUsedWhenSet()
65+
{
66+
$mail = new Email();
67+
$mail
68+
->from('from@mail.com')
69+
->to('to@mail.com')
70+
->subject('Subject')
71+
->text('Some text')
72+
;
73+
74+
$envelope = new DelayedEnvelope($mail);
75+
$envelope->setRecipients([new Address('recipient@mail.com')]);
76+
77+
$sendmailTransport = new SendmailTransport(self::FAKE_SENDMAIL);
78+
$sendmailTransport->send($mail, $envelope);
79+
80+
$this->assertStringEqualsFile($this->argsPath, __DIR__.'/Fixtures/fake-sendmail.php -ffrom@mail.com recipient@mail.com');
81+
}
2482
}

src/Symfony/Component/Mailer/Transport/SendmailTransport.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,11 @@ protected function doSend(SentMessage $message): void
8686
$this->getLogger()->debug(sprintf('Email transport "%s" starting', __CLASS__));
8787

8888
$command = $this->command;
89+
90+
if (!empty($recipients = $message->getEnvelope()->getRecipients())) {
91+
$command = str_replace(' -t', '', $command);
92+
}
93+
8994
if (!str_contains($command, ' -f')) {
9095
$command .= ' -f'.escapeshellarg($message->getEnvelope()->getSender()->getEncodedAddress());
9196
}
@@ -96,6 +101,10 @@ protected function doSend(SentMessage $message): void
96101
$chunks = AbstractStream::replace("\n.", "\n..", $chunks);
97102
}
98103

104+
foreach ($recipients as $recipient) {
105+
$command .= ' '.escapeshellarg($recipient->getEncodedAddress());
106+
}
107< 48A4 code class="diff-text syntax-highlighted-line addition">+
99108
$this->stream->setCommand($command);
100109
$this->stream->initialize();
101110
foreach ($chunks as $chunk) {

0 commit comments

Comments
 (0)
0