Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 441c9b0

Browse files
Raphaël Drozfabpot
Raphaël Droz
authored andcommitted
SMTP Transport to provide the (final) Message-ID if available
1 parent acef571 commit 441c9b0

File tree

1 file changed

+29
-1
lines changed

1 file changed

+29
-1
lines changed

src/Symfony/Component/Mailer/Transport/Smtp/SmtpTransport.php

+29-1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ class SmtpTransport extends AbstractTransport
3838
private int $pingThreshold = 100;
3939
private float $lastMessageTime = 0;
4040
private AbstractStream $stream;
41+
private string $mtaResult = '';
4142
private string $domain = '[127.0.0.1]';
4243

4344
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
146147
throw $e;
147148
}
148149

150+
$messageId = $this->parseMessageId($this->mtaResult);
151+
if ($messageId) {
152+
$message->setMessageId($messageId);
153+
}
154+
149155
$this->checkRestartThreshold();
150156

151157
return $message;
152158
}
153159

160+
protected function parseMessageId(string $mtaResult): ?string
161+
{
162+
$regexps = [
163+
'/250 Ok (?P<id>[0-9a-f-]+)\r?$/mis',
164+
'/250 Ok:? queued as (?P<id>[A-Z0-9]+)\r?$/mis'
165+
];
166+
167+
if ($mtaResult === '') {
168+
return null;
169+
}
170+
171+
$matches = [];
172+
foreach ($regexps as $regexp) {
173+
preg_match($regexp, $mtaResult, $matches);
174+
if (!empty($matches['id'])) {
175+
return $matches['id'];
176+
}
177+
}
178+
179+
return null;
180+
}
181+
154182
public function __toString(): string
155183
{
156184
if ($this->stream instanceof SocketStream) {
@@ -213,7 +241,7 @@ protected function doSend(SentMessage $message): void
213241
$this->getLogger()->debug(sprintf('Email transport "%s" stopped', __CLASS__));
214242
throw $e;
215243
}
216-
$this->executeCommand("\r\n.\r\n", [250]);
244+
$this->mtaResult = $this->executeCommand("\r\n.\r\n", [250]);
217245
$message->appendDebug($this->stream->getDebug());
218246
$this->lastMessageTime = microtime(true);
219247
} catch (TransportExceptionInterface $e) {

0 commit comments

Comments
 (0)