Im using swiftmailer for sending mails from my symfony2.2 project. Is there a way to log globally all email info and send results?
It would be great if mailer send() method have trigger somę event, but I can't see it does.
Im using swiftmailer for sending mails from my symfony2.2 project. Is there a way to log globally all email info and send results?
It would be great if mailer send() method have trigger somę event, but I can't see it does.
You can wrap
SwiftMailer
with your own custom mailer class. Like,I did it this way:
1. My service configuration
2. Mailer wraper service
It extends
Swift_Mailer
, because it is passed to different classes expecting mailer to be instance ofSwift_Mailer
. And it createsSwift_Mailer
instance as a field, because...$transport
isprivate
in\Swith_Mailer
(link). Code would be so much better if$transport
wasprotected
...3. Bundle builder
4. And
OverrideServiceSwiftMailer
classThis question was answered already, This solution is better for Symfony 4 combined with Monolog. It is based on umpirsky his Answer. But without the overhead of a custom file logger.
Note: Logs will be placed in ./var/logs/...
App\Util\MailLoggerUtil.php
services.yaml
If you want the mailer logs to be in another channel add this:
dev/monolog.yaml (optional)
Service:
Config:
Adding the following to the 'services' section of your configuration will print interaction with the transport to stdout (which can be useful if you're debugging by using the console commands, e.g. 'swiftmailer:email:send' or 'swiftmailer:spool:send'):
Example output, using SMTP transport to localhost: