8000 [Mailer] Use recipients in sendmail transport by HypeMC · Pull Request #45690 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Mailer] Use recipients in sendmail transport #45690

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env php
<?php
$argsPath = sys_get_temp_dir().\DIRECTORY_SEPARATOR.'sendmail_args';

file_put_contents($argsPath, implode(' ', $argv));
< 8000 td class="blob-code blob-code-addition js-file-line"> }
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,81 @@
namespace Symfony\Component\Mailer\Tests\Transport;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Mailer\DelayedEnvelope;
use Symfony\Component\Mailer\Transport\SendmailTransport;
use Symfony\Component\Mime\Address;
use Symfony\Component\Mime\Email;

class SendmailTransportTest extends TestCase
{
private const FAKE_SENDMAIL = __DIR__.'/Fixtures/fake-sendmail.php -t';

/**
* @var string
*/
private $argsPath;

protected function setUp(): void
{
$this->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');
}
}
9 changes: 9 additions & 0 deletions src/Symfony/Component/Mailer/Transport/SendmailTransport.php
Original file line number Diff line number Diff line change
Expand Up @@ -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())) {
8000
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (!empty($recipients = $message->getEnvelope()->getRecipients())) {
if ($recipients = $message->getEnvelope()->getRecipients()) {

$command = str_replace(' -t', '', $command);
}

if (!str_contains($command, ' -f')) {
$command .= ' -f'.escapeshellarg($message->getEnvelope()->getSender()->getEncodedAddress());
}
Expand All @@ -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) {
Expand Down
0