How to set multiple email configurations in Laravel?

There are times where you need to send emails from different email, lets sey: when a user creates an accounts in your aplication, you send an email with accounts@domail.com and when something happen within your aplication and you want to let the user know about the change, you can send an email with info@domail.com or use other email.

In this article I will guide you to accomplish this requirement.

Here, I assume that you already know how to send Email and Mail Notifications with Laravel.

Within your mail configuration file, you will find a mailers configuration array. This array contains a sample configuration entry for each of the major mail drivers / transports supported by Laravel, while the default configuration value determines which mailer will be used by default when your application needs to send an email message.

First, we need to make some changes to this file to be able to set two email config. Open your mail configuration fileconfig/mail.php ,locate the global “from” address, comment it out or remove it.

'from' => [
'address' => env('MAIL_FROM_ADDRESS', 'hello@example.com'),
'name' => env('MAIL_FROM_NAME', 'Example'),
],

Next, you will place the from array that we removed into the mailers array:

'mailers' => [
//This is the default configuration
'smtp' => [
'transport' => 'smtp',
'host' => env('MAIL_HOST', 'smtp.mailgun.org'),
'port' => env('MAIL_PORT', 587),
'encryption' => env('MAIL_ENCRYPTION', 'tls'),
'username' => env('MAIL_USERNAME'),
'password' => env('MAIL_PASSWORD'),
'timeout' => null,
'auth_mode' => null,
'from' => [
'address' => env(
'MAIL_FROM_ADDRESS',
'hello@example.com'),
'name' => env('MAIL_FROM_NAME', 'Example'),
],
], //This is the second configuration
//Used to send info messages
info' => [
'transport' => 'smtp',
'host' => env('INFO_MAIL_HOST', 'smtp.mailgun.org'),
'port' => env('INFO_MAIL_PORT', 587),
'encryption' => env('INFO_MAIL_ENCRYPTION', 'tls'),
'username' => env('INFO_MAIL_USERNAME'),
'password' => env('INFO_MAIL_PASSWORD'),
'timeout' => null,
'auth_mode' => null,
'from' => [
'address' => env(
'INFO_MAIL_FROM_ADDRESS',
'hello@example.com'),
'name' => env('MAIL_FROM_NAME', 'Example'),
],
],...

We will modify our .envfile to match the changes me made in config/mail.php file.

#Default mail configuration
MAIL_MAILER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null
MAIL_FROM_ADDRESS=null
MAIL_FROM_NAME="${APP_NAME}"
#Second mail configuration
INFO_MAIL_MAILER=smtp
INFO_MAIL_HOST=smtp.mail
trap.ioINFO_MAIL_PORT=2525
INFO_MAIL_USERNAME=null
INFO_MAIL_PASSWORD=null
INFO_MAIL_ENCRYPTION=null
INFO_MAIL_FROM_ADDRESS=null

The final step is to make use of our configurations.

When you need to send emails with the default configuration, you use Mail::to($recipient)->send(new OrderShipped($order));

And when you need to use the second mail configuration you do like this:

Mail::mailer('postmark')->to($recipient)->send(new OrderShipped($order));

I hope it helps.

Sou desenvolvedor de software, atualmente trabalho como analista de sistema e escrevo programas para a web. Escrevo no medium e gravo vídeos para Youtube.