How to get Email Queue ID with swiftmailer?
Asked Answered
J

2

6

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?

Jardiniere answered 26/12, 2014 at 12:8 Comment(0)
F
14

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: <[email protected]>

<< 250 2.1.0 Ok

>> RCPT TO: <[email protected]>

<< 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());
Formal answered 11/1, 2015 at 21:35 Comment(1)
In 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.Cyclograph
L
0

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();
Laureen answered 6/11, 2015 at 13:35 Comment(9)
But it isn't the same ID, is it?Cyclograph
@Á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.Laureen
If 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.)Cyclograph
I 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.Laureen
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.Laureen
I'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.Cyclograph
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. <[email protected]>Boughpot
This is what I actually needed, the title is confusing as I assumed he also wanted the message-idFloatable
Amazon SES does generate its own message ID, and uses this one to track opens, bounces, complaints etc., not Swift's.Moramorabito

© 2022 - 2024 — McMap. All rights reserved.