When I send email using telnet smtp server answers me 250 2.0.0 Ok: queued as 7A821440123E
when email was sent. So I need to get ID 7A821440123E
to track email in mail log.
Is it posible to get this with Swiftmailer?

- 53,325
- 33
- 152
- 143

- 1,489
- 4
- 22
- 40
2 Answers
SwiftMailer is event based so it can be easily extended by other developers. Every time the send
method on Swift_Transport
is called the proper event is dispatched. You can either use already developed listeners (plugins) or write your own which will be more customized.
Existing plugin
SwiftMailer comes out already with a few plugins that you can use to solve your problem.
Simply use the logger plugin. It will log all command calls within Swift_Transport
implementation.
Example
$transport = Swift_SmtpTransport::newInstance('example.com', 25);
$mailer = Swift_Mailer::newInstance($transport);
$mailer->registerPlugin(
new Swift_Plugins_LoggerPlugin(
new Swift_Plugins_Loggers_EchoLogger(false)
)
);
$message = Swift_Message::newInstance('Wonderful Subject');
$mailer->send($message);
Output
++ Starting Swift_SmtpTransport
<< 220 example.com ESMTP ready
>> EHLO [127.0.0.1]
<< 250-example.com
250-SIZE 5242880
250-PIPELINING
250-ENHANCEDSTATUSCODES
250-8BITMIME
250-DSN
250-AUTH PLAIN LOGIN CRAM-MD5
250 STARTTLS
>> AUTH CRAM-MD5
<< 235 2.0.0 OK
++ Swift_SmtpTransport started
>> MAIL FROM: <john@example.com>
<< 250 2.1.0 Ok
>> RCPT TO: <receiver@example.com>
<< 250 2.1.0 Ok
>> DATA
<< 354 Go ahead
>>
.
<< 250 2.0.0 Ok: queued as 7A821440123E
1++ Stopping Swift_SmtpTransport
>> QUIT
<< 221 2.0.0 Bye
++ Swift_SmtpTransport stopped
As you can see at the end, there is the desired id.
Custom plugin
Swift_Transport
provides an interface for registering a plugin.
It is nothing more than attaching an event listener to the event dispatcher.
You can write a simple plugin by yourself. Everything you need to do is to implement the Swift_Events_ResponseListener
class FindEmailIdResponse implements Swift_Events_ResponseListener
{
/**
* {@inheritdoc}
*/
public function responseReceived(Swift_Events_ResponseEvent $evt)
{
if (strpos($evt->getResponse(), 'queued') !== false) {
// do whatever you want with the response or event
}
}
}
And then simply register your plugin in the mailer instance
$mailer->registerPlugin(new FindEmailIdResponse());

- 581
- 3
- 9
-
2In other words, Swift Mailer does not seem to provide this piece of info out of the box and you need to parse it yourself from the server response. While the library must do some basic parsing, it's only interested in the 250 status code. Too bad you don't have direct access to the exact log line. – Álvaro González Jan 24 '15 at 15:50
According to the documentation there is no need of 3rd party plugins in order to get the Message-ID. The Message-ID is set by SwiftMailer, otherwise the SMTP server creates one itself. So, all you need is:
$message = \Swift_Message::newInstance($subject, $body);
...
$messageID = $message->getId();

- 363
- 3
- 12
-
1
-
@ÁlvaroGonzález, why do you think it is not the same ID discussed above? It's the same ID, but retrieved before actually sending the message instead of parsing SMTP communication log. The solution given by me will not work only if you let the SMTP generate itself the Message-ID header instead of using one provided by SwiftMailer. – stz184 Nov 09 '15 at 09:09
-
1If you inspect source code you'll see that Swift generated IDs end with "@something" (e.g. "@swift.generated" or "@example.com") but everytime I've checked the SMTP log I've found simple hexadecimal strings as the one shown in the question. *(There're also 2 deleted answers that state the same you do, you can see them when you have enough rep.)* – Álvaro González Nov 09 '15 at 09:27
-
1I am using mailcatcher as dummy SMTP server in order to test my mail services and I can confirm that the ID generated by SwiftMailer is the one reported by mailcatcher SMTP. I will try to reproduce what you say with real-world SMTP. – stz184 Nov 09 '15 at 09:41
-
Just tested with Gmail SMTP - the message ID reported by Gmail is the one SwiftMailer set. May be there is use case scenario where SMTP overrides the Message-ID header generated by SwiftMailer, but I am not aware of it. – stz184 Nov 09 '15 at 12:50
-
1I've worked recently with a two or three providers and none would use the Swift generated ID. Also, I used to maintain a Postfix server some years ago and it made heavy use of hexadecimal queue IDs: you could use the ID to track, resend, remove... with the *postqueue* command, and in that context the ID needs to be unique. I can't tell for sure after 10 years but I suspect that ID was the one reported. Whatever, to obtain a definitive answer we'd need to dig into the SMTP RFC's and see what they say (or don't say) about queue IDs. – Álvaro González Nov 11 '15 at 09:38
-
5**You're talking about a different ID than the OP is!** What the OP asks for is called *Queue ID*, and usually looks like `7A821440123E`. What you are talking about is the *Message-ID* (i.e. a header inside the email), and usually looks like an email address, e.g. `<0d0d90b541b06acce4559ef40bd238b4@example.com>` – Thomas Landauer Apr 19 '17 at 13:25
-
This is what I actually needed, the title is confusing as I assumed he also wanted the message-id – Timo Huovinen Mar 21 '18 at 16:10
-
Amazon SES does generate its own message ID, and uses this one to track opens, bounces, complaints etc., not Swift's. – BenMorel Oct 08 '19 at 12:28