Get "PHP 8 in a Nuthshell" (Now comes with PHP 8.3)
Amit Merchant

Amit Merchant

A blog on PHP, JavaScript, and more

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 config/services.php file.

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 config/mail.php

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 Mail facade which gives you the ability to choose different mail drivers for different scenarios. For example, your application might use Postmark to send transactional mail while using Amazon SES to send bulk mail.

Here’s how you can use the mailer method to select one of the mailer that you’ve configured config/services.php file.

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!

Learn the fundamentals of PHP 8 (including 8.1, 8.2, and 8.3), the latest version of PHP, and how to use it today with my new book PHP 8 in a Nutshell. It's a no-fluff and easy-to-digest guide to the latest features and nitty-gritty details of PHP 8. So, if you're looking for a quick and easy way to PHP 8, this is the book for you.

Like this article? Consider leaving a

Tip

👋 Hi there! I'm Amit. I write articles about all things web development. You can become a sponsor on my blog to help me continue my writing journey and get your brand in front of thousands of eyes.

Comments?