@@ -38,6 +38,7 @@ class SmtpTransport extends AbstractTransport
38
38
private int $ pingThreshold = 100 ;
39
39
private float $ lastMessageTime = 0 ;
40
40
private AbstractStream $ stream ;
41
+ private string $ mtaResult = '' ;
41
42
private string $ domain = '[127.0.0.1] ' ;
42
43
43
44
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
146
147
throw $ e ;
147
148
}
148
149
150
+ $ messageId = $ this ->parseMessageId ($ this ->mtaResult );
151
+ if ($ messageId ) {
152
+ $ message ->setMessageId ($ messageId );
153
+ }
154
+
149
155
$ this ->checkRestartThreshold ();
150
156
151
157
return $ message ;
152
158
}
153
159
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
+
154
182
public function __toString (): string
155
183
{
156
184
if ($ this ->stream instanceof SocketStream) {
@@ -213,7 +241,7 @@ protected function doSend(SentMessage $message): void
213
241
$ this ->getLogger ()->debug (sprintf ('Email transport "%s" stopped ' , __CLASS__ ));
214
242
throw $ e ;
215
243
}
216
- $ this ->executeCommand ("\r\n. \r\n" , [250 ]);
244
+ $ this ->mtaResult = $ this -> executeCommand ("\r\n. \r\n" , [250 ]);
217
245
$ message ->appendDebug ($ this ->stream ->getDebug ());
218
246
$ this ->lastMessageTime = microtime (true );
219
247
} catch (TransportExceptionInterface $ e ) {
0 commit comments