How to send from multiple mail accounts in Laravel

Dr. Adam Nielsen
1 min readNov 30, 2019

--

Image from Tumisu

Laravel works out of the box with only one mail account. If you want to send from multiple mail accounts, you may use the Laravel-MultiMail Package.

Setup

First one needs to install the package:

composer require iwasherefirst2/laravel-multimail

and publish the config file:

php artisan vendor:publish --provider="IWasHereFirst2\LaravelMultiMail\MultiMailServiceProvider"

Next, we need to setup the mail accounts in config/multimail.php

'emails'  => [
'office@example.net' =>
[
'pass' => env('first_mail_password'),
'from' => "Max Musterman",
],
'contact@example.net' =>
[
'pass' => env('second_mail_password'),
'from' => "Alice Armania",
],
],

If you have email accounts from different mail hosts, you can specify the hosts as explained in the docs. If your mail username is different from its mail address, you may specify the username by username attribute.

The credentials may be stored in the .env file:

first_mail_password=12345qwer
second_mail_password=1234qwer

Usage

Simply specify the email address that you have declared in config/multimail.php with the from method:

\MultiMail::to($user)
->from('office@example.com')
->send(new \App\Mail\Invitation($user));

or

\MultiMail::to($user)
->from('contact@example.net')
->send(new \App\Mail\Invitation($user));

--

--

Dr. Adam Nielsen
Dr. Adam Nielsen

Written by Dr. Adam Nielsen

PHD in math. and Laravel / Vue Full-Stack-Developer

Responses (1)