Skip to content
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

[Mailer] include message id provided by the MTA when dispatching the SentMessageEvent #53969

Merged
merged 1 commit into from
Mar 28, 2024
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public function write(string $bytes, $debug = true): void
} elseif (str_starts_with($bytes, 'QUIT')) {
$this->nextResponse = '221 Goodbye';
} else {
$this->nextResponse = '250 OK';
$this->nextResponse = '250 OK queued as 000501c4054c';
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

use PHPUnit\Framework\TestCase;
use Symfony\Component\Mailer\Envelope;
use Symfony\Component\Mailer\Event\MessageEvent;
use Symfony\Component\Mailer\Event\SentMessageEvent;
use Symfony\Component\Mailer\Exception\LogicException;
use Symfony\Component\Mailer\Exception\TransportException;
use Symfony\Component\Mailer\Transport\Smtp\SmtpTransport;
Expand All @@ -24,6 +26,7 @@
use Symfony\Component\Mime\Part\DataPart;
use Symfony\Component\Mime\Part\File;
use Symfony\Component\Mime\RawMessage;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;

/**
* @group time-sensitive
Expand Down Expand Up @@ -137,6 +140,37 @@ public function testWriteEncodedRecipientAndSenderAddresses()
$this->assertContains("RCPT TO:<recipient2@example.org>\r\n", $stream->getCommands());
}

public function testMessageIdFromServerIsEmbeddedInSentMessageEvent()
{
$calls = 0;
$eventDispatcher = $this->createMock(EventDispatcherInterface::class);
$eventDispatcher->expects($this->any())
->method('dispatch')
->with($this->callback(static function ($event) use (&$calls): bool {
++$calls;

if (1 === $calls && $event instanceof MessageEvent) {
return true;
}

if (2 === $calls && $event instanceof SentMessageEvent && '000501c4054c' === $event->getMessage()->getMessageId()) {
return true;
}

return false;
}));
$transport = new SmtpTransport(new DummyStream(), $eventDispatcher);

$email = new Email();
$email->from('sender@example.com');
$email->to('recipient@example.com');
$email->text('.');

$transport->send($email);

$this->assertSame(2, $calls);
}

public function testAssertResponseCodeNoCodes()
{
$this->expectException(LogicException::class);
Expand Down
11 changes: 5 additions & 6 deletions src/Symfony/Component/Mailer/Transport/Smtp/SmtpTransport.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ class SmtpTransport extends AbstractTransport
private int $pingThreshold = 100;
private float $lastMessageTime = 0;
private AbstractStream $stream;
private string $mtaResult = '';
private string $domain = '[127.0.0.1]';

public function __construct(?AbstractStream $stream = null, ?EventDispatcherInterface $dispatcher = null, ?LoggerInterface $logger = null)
Expand Down Expand Up @@ -148,10 +147,6 @@ public function send(RawMessage $message, ?Envelope $envelope = null): ?SentMess
throw $e;
}

if ($this->mtaResult && $messageId = $this->parseMessageId($this->mtaResult)) {
$message->setMessageId($messageId);
}

$this->checkRestartThreshold();

return $message;
Expand Down Expand Up @@ -235,9 +230,13 @@ protected function doSend(SentMessage $message): void
$this->getLogger()->debug(sprintf('Email transport "%s" stopped', __CLASS__));
throw $e;
}
$this->mtaResult = $this->executeCommand("\r\n.\r\n", [250]);
$mtaResult = $this->executeCommand("\r\n.\r\n", [250]);
$message->appendDebug($this->stream->getDebug());
$this->lastMessageTime = microtime(true);

if ($mtaResult && $messageId = $this->parseMessageId($mtaResult)) {
$message->setMessageId($messageId);
}
} catch (TransportExceptionInterface $e) {
$e->appendDebug($this->stream->getDebug());
$this->lastMessageTime = 0;
Expand Down