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

Skip to content

Commit bd7c8e6

Browse files
committed
[Mailer] Use recipients in sendmail transport
1 parent f1f123b commit bd7c8e6

File tree

3 files changed

+80
-0
lines changed

3 files changed

+80
-0
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
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));

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

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,79 @@
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+
if ('\\' === \DIRECTORY_SEPARATOR) {
49+
$this->markTestSkipped('Windows does not support non-blocking standard streams');
50+
}
51+
52+
$mail = new Email();
53+
$mail
54+
->from('from@mail.com')
55+
->to('to@mail.com')
56+
->subject('Subject')
57+
->text('Some text')
58+
;
59+
60+
$envelope = new DelayedEnvelope($mail);
61+
62+
$sendmailTransport = new SendmailTransport(self::FAKE_SENDMAIL);
63+
$sendmailTransport->send($mail, $envelope);
64+
65+
$this->assertStringEqualsFile($this->argsPath, __DIR__.'/Fixtures/fake-sendmail.php -ffrom@mail.com to@mail.com');
66+
}
67+
68+
public function testRecipientsAreUsedWhenSet()
69+
{
70+
if ('\\' === \DIRECTORY_SEPARATOR) {
71+
$this->markTestSkipped('Windows does not support non-blocking standard streams');
72+
}
73+
74+
$mail = new Email();
75+
$mail
76+
->from('from@mail.com')
77+
->to('to@mail.com')
78+
->subject('Subject')
79+
->text('Some text')
80+
;
81+
82+
$envelope = new DelayedEnvelope($mail);
83+
$envelope->setRecipients([new Address('recipient@mail.com')]);
84+
85+
$sendmailTransport = new SendmailTransport(self::FAKE_SENDMAIL);
86+
$sendmailTransport->send($mail, $envelope);
87+
88+
$this->assertStringEqualsFile($this->argsPath, __DIR__.'/Fixtures/fake-sendmail.php -ffrom@mail.com recipient@mail.com');
89+
}
2490
}

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 6A59 = AbstractStream::replace("\n.", "\n..", $chunks);
97102
}
98103

104+
foreach ($recipients as $recipient) {
105+
$command .= ' '.escapeshellarg($recipient->getEncodedAddress());
106+
}
107+
99108
$this->stream->setCommand($command);
100109
$this->stream->initialize();
101110
foreach ($chunks as $chunk) {

0 commit comments

Comments
 (0)
0