Best way to add custom MessageSent event to mailer

Posted 4 weeks ago by bramcou

Hi all,

In my application I send all my emails through a queue. For one Mailable I want to log whether the email has actually been sent. Now the mailer fires a standard MessageSent event which contains the message and the data provided to the mailable, but not the subject, which I also want to log.

So I figured that I have to extend the Illuminate mailer dispatchSentEvent() method to fire a custom event, with the subject. I've figured out one way to do this but I'm doubting whether this is the preferred way to do this.

I figured that I could extend the Illuminate mailer to override the dispatchSentEvent() method. In order to extend the mailer, I know I can create a new mailserviceprovider, which at the minimum extends the default mailserviceprovider registerIlluminateMailer() method so as to register my extended mailer instead of the Illuminate mailer.

This does appear to be a bit of a detour to accomplish my goal. And while a detour by itself isn't that bad, it does have me extend the registerIlluminateMailer() which might change at some point in a newer version of Laravel.

Am I going about this the right way or is there a (way) better way?


P.S. One obvious, way easier, solution would be to simply include the subject in the mailable constructor, however this mailable is a mailable that is used for user customized emails, so it's used to send 10+ different kind of emails and the subject is extracted from the database in the mailable build() method..

Please sign in or create an account to participate in this conversation.