Using multiple email smtp mailers in Laravel

Using multiple smtp configurations in laravel 8

Sometimes in your application, you may need to use more than one mail drivers to cover different areas, but Laravel 5.3 only supports one,

how can we extend it to support more mail drivers and be able to switch the drivers at runtime?

Check below code, I have tried it and its working perfect for my application.

In this tutorial we will discuss you how can configure multiple smtp in your Laravel applications to send emails.

Steps to configure multiple emails

Step 1: Add your smtp configurations

'mailers' => [
'smtp' => [
'transport' => 'smtp',
'host' => env('MAIL_HOST', ''),
'port' => env('MAIL_PORT', 587),
'encryption' => env('MAIL_ENCRYPTION', 'tls'),
'username' => env('MAIL_USERNAME'),
'password' => env('MAIL_PASSWORD'),
'timeout' => null,
'auth_mode' => null,

'smtp2' => [
'transport' => 'smtp',
'host' => env('SMTP2_MAIL_HOST', ''),
'port' => env('SMTP2_MAIL_PORT', 587),
'encryption' => env('SMTP2_MAIL_ENCRYPTION', 'tls'),
'username' => env('SMTP2_MAIL_USERNAME'),
'password' => env('SMTP2_MAIL_PASSWORD'),
'timeout' => null,
'auth_mode' => null,
// your other email configurations

Step 2. Add your smtp settings in .env file

//smtp configurations
//smtp2 configurations

Step 3. Then you can directly use

//to send using first smtp configurations
Mail::mailer('smtp')->to('')->send(new TestMail());
//to send using seconds smtp configurations
Mail::mailer('smtp2')->to('')->send(new TestMail());

You can also use this with your mail jobs or queues as same in step 3.

Don't forget to reload your configuration before testing.

I believe this brief tutorial will be of much help. Do not forget to share this tutorial your friends. Don’t forget to leave your comments below, if you have any. Love Leads!!!

Software Developer, developing desktop, web and mobile applications using Java, PHP, Spring, Laravel and React.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Must Have Dev Tools of 2018

Combining Data Structure With Algorithm for Clean Code in PHP

Writing documentation for elixir projects and serving it on localhost

Creating and Minting Solana NFTs using next-candy-machine

Using Ampersand (&) with blocks in Ruby

Web development involves a frontend, which interacts with the client, and a backend, which…

How well Digimaster3 / CGDI Pro does the BMW odometer correction?

Which tool should BMW choose for mileage correction in 2022

ExoPlayer Components Explained

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Laks Yalamati

Laks Yalamati

Software Developer, developing desktop, web and mobile applications using Java, PHP, Spring, Laravel and React.

More from Medium

Login with Laravel OTP

Email verification login using Laravel

Easily Integrate Kopokopo Payments API with Laravel 8

Modifying iOS SMS database