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

Skip to content

Commit 703596e

Browse files
committed
feature #46326 SMTP Transport to provide the (final) Message-ID if available (Raphaël Droz)
This PR was squashed before being merged into the 6.2 branch. Discussion ---------- SMTP Transport to provide the (final) Message-ID if available bug #46323 [Mailer] SMTP transport does not provide the (final) Message-ID | Q | A | ------------- | --- | Branch? | 6.1 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | Fix #46323 | License | MIT | Doc PR | symfony/symfony-docs# Questions / Answers: - **Q**: Why storing 250 inside **`$mtaResult`** instead of using the already available `$message->getDebug()`? - A: Because the later may be huge (especially in case of attachment) and running a regexp over it would be less than safe. Instead `$mtaResult` only contains one line - **Q**: Why a new **`public`** method? - A: Because child SMTP Transport may make other assumptions about the return `250 Ok` value and provide a custom logic / regexp. - **Q**: Why **regexp**? - A: Because there is no official standard for this line (other than the `250 Ok` prefix) Additionally (see https://symfony.com/releases): - [ ] Always add tests and ensure they pass. _I can't. Don't have the time. Left to maintainers / other contributors_ - [x] Bug fixes must be submitted against the lowest maintained branch where they apply (lowest branches are regularly merged to upper ones so they get the fixes too.) - [x] Features and deprecations must be submitted against the latest branch. - [x] Changelog entry should follow https://symfony.com/doc/current/contributing/code/conventions.html#writing-a-changelog-entry - [x] Never break backward compatibility (see https://symfony.com/bc). Commits ------- 441c9b0 SMTP Transport to provide the (final) Message-ID if available
2 parents 5675aa8 + 441c9b0 commit 703596e

File tree

1 file changed

+29
-1
lines changed

1 file changed

+29
-1
lines changed

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

Lines changed: 29 additions & 1 deletion
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)