From 441c9b007f50b288c5298491376ecae72a124e6e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Droz?= Date: Wed, 11 May 2022 11:57:16 -0300 Subject: [PATCH] SMTP Transport to provide the (final) Message-ID if available --- .../Mailer/Transport/Smtp/SmtpTransport.php | 30 ++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/src/Symfony/Component/Mailer/Transport/Smtp/SmtpTransport.php b/src/Symfony/Component/Mailer/Transport/Smtp/SmtpTransport.php index 9432be7f3645f..5ac4b2dbdaa98 100644 --- a/src/Symfony/Component/Mailer/Transport/Smtp/SmtpTransport.php +++ b/src/Symfony/Component/Mailer/Transport/Smtp/SmtpTransport.php @@ -38,6 +38,7 @@ 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) @@ -146,11 +147,38 @@ public function send(RawMessage $message, Envelope $envelope = null): ?SentMessa throw $e; } + $messageId = $this->parseMessageId($this->mtaResult); + if ($messageId) { + $message->setMessageId($messageId); + } + $this->checkRestartThreshold(); return $message; } + protected function parseMessageId(string $mtaResult): ?string + { + $regexps = [ + '/250 Ok (?P[0-9a-f-]+)\r?$/mis', + '/250 Ok:? queued as (?P[A-Z0-9]+)\r?$/mis' + ]; + + if ($mtaResult === '') { + return null; + } + + $matches = []; + foreach ($regexps as $regexp) { + preg_match($regexp, $mtaResult, $matches); + if (!empty($matches['id'])) { + return $matches['id']; + } + } + + return null; + } + public function __toString(): string { if ($this->stream instanceof SocketStream) { @@ -213,7 +241,7 @@ protected function doSend(SentMessage $message): void $this->getLogger()->debug(sprintf('Email transport "%s" stopped', __CLASS__)); throw $e; } - $this->executeCommand("\r\n.\r\n", [250]); + $this->mtaResult = $this->executeCommand("\r\n.\r\n", [250]); $message->appendDebug($this->stream->getDebug()); $this->lastMessageTime = microtime(true); } catch (TransportExceptionInterface $e) {