I want to override the Laravels' Mail's classes facade method send (just intercept it forcing some checks and then if it passes triggering parent::send())
What is the best way to do this?
I want to override the Laravels' Mail's classes facade method send (just intercept it forcing some checks and then if it passes triggering parent::send())
What is the best way to do this?
A Facade doesn't work like that. It's essentially kind of like a wrapper class that calls the underlying class that it represents.
The
Mail
facade doesn't actually have asend
method. When you doMail::send()
, under the hood, the "facade accessor" is used to reference an instance of theIlluminate\Mail\Mailer
class bound in the IoC container. It's on that object thesend
method is called.The way in which you can achieve what you're after is actually a little bit trickier than it seems. What you can do is:
Mailer
, extendingIlluminate\Mail\Mailer
, in which you can override thesend
method, implement your checks and callparent::send()
.Illuminate\Mail\MailServiceProvider
), in particular re-implement theregister
method. It should create an instance of your ownMailer
in place of Laravel's own. (You can copy most of the code from Laravel'sregister
method).config/app.php
file, in theproviders
array, replaceIlluminate\Mail\MailServiceProvider::class,
with your own provider.That should let you hook into Laravel's Mail functionality.
For more information, you can take a look at the following question/answer which achieves a similar thing. It extends the Mail functionality to add a new transport driver, but it takes a similar approach in that it provides it's own Mailer implementation and service provider.
Add a new transport driver to Laravel's Mailer
app/MyMailer/Mailer.php
app/MyMailer/MailServiceProvider.php (Most of the code copied from Laravel's MailServiceProvider class)
config/app.php (In the providers array)