Using different mail drivers dynamically in Laravel 7.x
Sending emails in Laravel is a breeze. All you have to do is, configure your default mail service in
config/mail.php and it’s related configuration in
For instance, if you want to use AWS SES, you’ll first need to setup its configuration in
config/services.php like so.
return [ // ... 'ses' => [ 'key' => env('AWS_ACCESS_KEY_ID'), 'secret' => env('AWS_SECRET_ACCESS_KEY'), 'region' => env('AWS_DEFAULT_REGION', 'us-east-1'), ], ];
And then setting it as default in
return [ // code commented for brevity 'driver' => env('MAIL_DRIVER', 'ses'), // code commented for brevity ]
Now, each time you try to send the email using, for instance,
Illuminate\Support\Facades\Mail facade, it will always use the
default option. i.e. “ses” in our case.
But what if you want some kind of flexibility of using different mailers at different places? Well, Laravel 7.x get you covered on this.
Dynamic Mail Drivers
Laravel 7 indroduced a new method called
mailer in the
Here’s how you can use the
mailer method to select one of the mailer that you’ve configured
Mail::mailer('postmark') ->to($request->user()) ->send(new OrderShipped($order));
You can quickly swithch to other, say “mandrill”, for certain scenarios like so (providing you’ve configured it brforehand).
Mail::mailer('mandrill') ->to($request->user()) ->send(new OrderShipped($order));
It’s as easy as that!
Like this article? Consider leaving aTip