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?
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());
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();
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 © 2022 - 2024 — McMap. All rights reserved.