php-swiftmailerHow to configure Swiftmailer logger?
Swiftmailer logger can be configured to log emails sent by the application.
// Create the Transport
$transport = (new Swift_SmtpTransport('smtp.example.org', 25))
->setUsername('your username')
->setPassword('your password')
;
// Create the Mailer using your created Transport
$mailer = new Swift_Mailer($transport);
// Create the logger
$logger = new Swift_Plugins_Loggers_ArrayLogger();
// Register the logger
$mailer->registerPlugin(new Swift_Plugins_LoggerPlugin($logger));
// Send the message
$result = $mailer->send($message);
// Dump the log
echo $logger->dump();
Output example
== START LOGGING ==
Sending message to: [email protected]
<<< 220 smtp.example.org ESMTP
>>> EHLO localhost
<<< 250-smtp.example.org
250-PIPELINING
250-SIZE 10240000
250-VRFY
250-ETRN
250-STARTTLS
250-AUTH PLAIN LOGIN
250-AUTH=PLAIN LOGIN
250-ENHANCEDSTATUSCODES
250-8BITMIME
250 DSN
>>> STARTTLS
<<< 220 2.0.0 Ready to start TLS
>>> EHLO localhost
<<< 250-smtp.example.org
250-PIPELINING
250-SIZE 10240000
250-VRFY
250-ETRN
250-AUTH PLAIN LOGIN
250-AUTH=PLAIN LOGIN
250-ENHANCEDSTATUSCODES
250-8BITMIME
250 DSN
>>> AUTH LOGIN
<<< 334 VXNlcm5hbWU6
>>> <credentials hidden>
<<< 334 UGFzc3dvcmQ6
>>> <credentials hidden>
<<< 235 2.7.0 Authentication successful
>>> MAIL FROM:<[email protected]>
<<< 250 2.1.0 Ok
>>> RCPT TO:<[email protected]>
<<< 250 2.1.5 Ok
>>> DATA
<<< 354 End data with <CR><LF>.<CR><LF>
>>> Date: Mon, 13 Jul 2020 15:00:00 +0200
>>> To: [email protected]
>>> Subject: Test
>>>
>>> This is a test
>>>
>>> .
<<< 250 2.0.0 Ok: queued as 12345
== END LOGGING ==
Code explanation
- Create the Transport - This creates the transport layer for the mailer.
- Create the Mailer - This creates the mailer using the transport layer.
- Create the logger - This creates the logger for the mailer.
- Register the logger - This registers the logger with the mailer.
- Send the message - This sends the message using the mailer.
- Dump the log - This dumps the log of the mailer.
Helpful links
More of Php Swiftmailer
- How to use SMTP with Swiftmailer?
- How to set timeout with Swiftmailer?
- How to use TLS 1.2 with Swiftmailer?
- How to set the 'from' address in Swiftmailer?
- How to configure Swiftmailer for SMTP without authentication?
- How to use Swiftmailer with Symfony?
- How to use Swiftmailer to send RFC 2822 compliant emails?
- How to send emails in UTF8 using Swiftmailer?
- How to use Swiftmailer transport?
- How to set the sender name in SwiftMailer?
See more codes...