Skip to content

Commit

Permalink
bug #53969 [Mailer] include message id provided by the MTA when dispa…
Browse files Browse the repository at this point in the history
…tching the `SentMessageEvent` (xabbuh)

This PR was merged into the 6.4 branch.

Discussion
----------

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

| Q             | A
| ------------- | ---
| Branch?       | 6.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Issues        | Fix #52515
| License       | MIT

Commits
-------

a6fda75 include message id provided by the MTA when dispatching the SentMessageEvent
  • Loading branch information
xabbuh committed Mar 28, 2024
2 parents 33f4d2a + a6fda75 commit 020ef60
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 7 deletions.
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
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
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

0 comments on commit 020ef60

Please sign in to comment.