Skip to content

SMTP Transport to provide the (final) Message-ID if available #46326

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
Jul 20, 2022
Merged
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
30 changes: 29 additions & 1 deletion src/Symfony/Component/Mailer/Transport/Smtp/SmtpTransport.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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<id>[0-9a-f-]+)\r?$/mis',
'/250 Ok:? queued as (?P<id>[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) {
Expand Down Expand Up @@ -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) {
Expand Down