For various reasons I want/need to log all emails sent through my website which runs on Symfony 5.
What I have so far is a subscriber that creates an Entity of type EmailLogEntry when a MessageEvent class is created (at least that's what I understand from (MessageEvent::class
) - correct me if I'm wrong). I also use this subscriber to fill in missing emailadresses with the default system address.
Now, after sending the email, I'd like to adjust my entity and call $email->setSent(true);
, but I can't figure out how to subscribe to the event that tries to send the email. And for the reusability of the code I don't want to do that in the Services (yes, it's multiple since there's multiple sources that generate mails) where I actually call $this->mailer->send($email);
.
My questions now are:
- Can someone tell me how I can subscribe to the Mailers send event?
- How, in general, do I figure out what events I can subscribe to? The kernel events are listed in the documentation, but what about all the other events that are fired?
Btw, my subscriber code at the moment:
class SendMailSubscriber implements EventSubscriberInterface
{
public static function getSubscribedEvents()
{
return [
MessageEvent::class => [
['onMessage', 255],
['logMessage', 0],
],
];
}
public function logMessage(MessageEvent $event) {
$email = new EmailLogEntry();
[...]
}
}
Thanks.