From 7134e46638bfc11be5f8fbf4d5a10d6cc10a9956 Mon Sep 17 00:00:00 2001 From: HypeMC Date: Wed, 9 Mar 2022 23:15:29 +0100 Subject: [PATCH] [Mailer] Use recipients in sendmail transport --- .../Transport/Fixtures/fake-sendmail.php | 5 ++ .../Tests/Transport/SendmailTransportTest.php | 68 +++++++++++++++++++ .../Mailer/Transport/SendmailTransport.php | 9 +++ 3 files changed, 82 insertions(+) create mode 100755 src/Symfony/Component/Mailer/Tests/Transport/Fixtures/fake-sendmail.php diff --git a/src/Symfony/Component/Mailer/Tests/Transport/Fixtures/fake-sendmail.php b/src/Symfony/Component/Mailer/Tests/Transport/Fixtures/fake-sendmail.php new file mode 100755 index 0000000000000..5a4bafd20f1d1 --- /dev/null +++ b/src/Symfony/Component/Mailer/Tests/Transport/Fixtures/fake-sendmail.php @@ -0,0 +1,5 @@ +#!/usr/bin/env php +argsPath = sys_get_temp_dir().\DIRECTORY_SEPARATOR.'sendmail_args'; + } + + protected function tearDown(): void + { + if (file_exists($this->argsPath)) { + @unlink($this->argsPath); + } + unset($this->argsPath); + } + public function testToString() { $t = new SendmailTransport(); $this->assertEquals('smtp://sendmail', (string) $t); } + + public function testToIsUsedWhenRecipientsAreNotSet() + { + if ('\\' === \DIRECTORY_SEPARATOR) { + $this->markTestSkipped('Windows does not support shebangs nor non-blocking standard streams'); + } + + $mail = new Email(); + $mail + ->from('from@mail.com') + ->to('to@mail.com') + ->subject('Subject') + ->text('Some text') + ; + + $envelope = new DelayedEnvelope($mail); + + $sendmailTransport = new SendmailTransport(self::FAKE_SENDMAIL); + $sendmailTransport->send($mail, $envelope); + + $this->assertStringEqualsFile($this->argsPath, __DIR__.'/Fixtures/fake-sendmail.php -ffrom@mail.com to@mail.com'); + } + + public function testRecipientsAreUsedWhenSet() + { + if ('\\' === \DIRECTORY_SEPARATOR) { + $this->markTestSkipped('Windows does not support shebangs nor non-blocking standard streams'); + } + + $mail = new Email(); + $mail + ->from('from@mail.com') + ->to('to@mail.com') + ->subject('Subject') + ->text('Some text') + ; + + $envelope = new DelayedEnvelope($mail); + $envelope->setRecipients([new Address('recipient@mail.com')]); + + $sendmailTransport = new SendmailTransport(self::FAKE_SENDMAIL); + $sendmailTransport->send($mail, $envelope); + + $this->assertStringEqualsFile($this->argsPath, __DIR__.'/Fixtures/fake-sendmail.php -ffrom@mail.com recipient@mail.com'); + } } diff --git a/src/Symfony/Component/Mailer/Transport/SendmailTransport.php b/src/Symfony/Component/Mailer/Transport/SendmailTransport.php index c53d72f6974d6..e272d05e0e31c 100644 --- a/src/Symfony/Component/Mailer/Transport/SendmailTransport.php +++ b/src/Symfony/Component/Mailer/Transport/SendmailTransport.php @@ -86,6 +86,11 @@ protected function doSend(SentMessage $message): void $this->getLogger()->debug(sprintf('Email transport "%s" starting', __CLASS__)); $command = $this->command; + + if (!empty($recipients = $message->getEnvelope()->getRecipients())) { + $command = str_replace(' -t', '', $command); + } + if (!str_contains($command, ' -f')) { $command .= ' -f'.escapeshellarg($message->getEnvelope()->getSender()->getEncodedAddress()); } @@ -96,6 +101,10 @@ protected function doSend(SentMessage $message): void $chunks = AbstractStream::replace("\n.", "\n..", $chunks); } + foreach ($recipients as $recipient) { + $command .= ' '.escapeshellarg($recipient->getEncodedAddress()); + } + $this->stream->setCommand($command); $this->stream->initialize(); foreach ($chunks as $chunk) {