PHPackages                             dokicro/laravel-multimail - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. dokicro/laravel-multimail

ActiveLibrary

dokicro/laravel-multimail
=========================

A package to send mails easily from multiple mail accounts with Laravel

02.3k↓100%PHP

Since Apr 18Pushed 2y agoCompare

[ Source](https://github.com/dokicro/laravel-multimail)[ Packagist](https://packagist.org/packages/dokicro/laravel-multimail)[ RSS](/packages/dokicro-laravel-multimail/feed)WikiDiscussions master Synced today

READMEChangelog (1)DependenciesVersions (1)Used By (0)

Laravel Multimail
=================

[](#laravel-multimail)

[![Mail image](https://camo.githubusercontent.com/e139689bbf828298a6aa4baebfc5789420bc2638874dde7ad3beb852ec469489/68747470733a2f2f6d69726f2e6d656469756d2e636f6d2f6d61782f3634302f312a5841684f36396546504836703332566c796c554361772e706e67)](https://camo.githubusercontent.com/e139689bbf828298a6aa4baebfc5789420bc2638874dde7ad3beb852ec469489/68747470733a2f2f6d69726f2e6d656469756d2e636f6d2f6d61782f3634302f312a5841684f36396546504836703332566c796c554361772e706e67)

[![CircleCI](https://camo.githubusercontent.com/a1dcc093cf0d5ea5da3c8b1009ccf53182464d75fed40bd1d449761afd591be5/68747470733a2f2f636972636c6563692e636f6d2f67682f69776173686572656669727374322f6c61726176656c2d6d756c74696d61696c2f747265652f6d61737465722e7376673f7374796c653d737667)](https://circleci.com/gh/iwasherefirst2/laravel-multimail/tree/master)[![codecov](https://camo.githubusercontent.com/d98bc0363b462f42a5544db20de69b684869b8f505cd4f704814db13124d50e2/68747470733a2f2f636f6465636f762e696f2f67682f69776173686572656669727374322f6c61726176656c2d6d756c74696d61696c2f6272616e63682f6d61737465722f67726170682f62616467652e7376673f746f6b656e3d3358365a565252354551)](https://codecov.io/gh/iwasherefirst2/laravel-multimail)

This package helps you to send mails from your Laravel application from multiple email accounts.

The package supports sending queued, localized and bulk mails.

This package works for `SMTP` and `log` drivers.

Table of Contents
-----------------

[](#table-of-contents)

- [Requirments](#requirements)
- [Installation](#installation)
- [Usage](#usage)
    - [Basic Examples](#basic-examples)
    - [Queued Mails](#queued-mails)
    - [Specify in Mailable](#specify-in-mailable)
    - [Bulk messages](#bulk-messages)
- [Special Settings](#special-settings)
    - [Multiple Mail Providers](#multiple-mail-providers)
    - [Default mailaccount](#default-mailaccount)
    - [Testing](#testing)
    - [Get Mail From Database](#get-mail-from-database)
    - [Troubleshoot](#troubleshoot)
- [For Package Developer](#for-package-developer)

Requirements
------------

[](#requirements)

Laravel 5, 6 or 7

Installation
------------

[](#installation)

Install the package into your Laraval application with composer:

```
composer require iwasherefirst2/laravel-multimail

```

Publish the config file:

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

```

Configure your email clients in `config/multimail.php`:

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

```

Make sure to put your credentials in the `.env` file, so they don't get tracked in your repository.

For each mail you may specify multiple columns:

AttributFunctionalityrequired`pass`Password of email accountyes`username`Username of email account, only neccessary if different from email addressno`from_name`Name that should appear in front of emailno`provider`Provider of email account, only necessary if mail host/encription/port is not default (see [here](#multiple-mail-providers) for more)noUsage
-----

[](#usage)

One may send a mail using `\MultiMail` instead of `\Mail`. The methods `to`, `cc`, `bcc`, `locale` are exactly the same as provided by the [mail facade](https://laravel.com/docs/5.8/mail#sending-mail) (note that `locale` is only available since Laravel 5.6).

The `from` method from `MultiMail` needs a string of an email provided in `config/multimail.php`. You can pass optionaly a second parameter as from name instetad of using the default falue given in the config. When using `send` or `queue` the mail will be send from the mail account specified in `cofing/multimail.php`.

### Basic Examples

[](#basic-examples)

This example assumes that `office@example.net` and `contact@example.net` have been specified in `config/multimail.php`.

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

// Send from malaccount email@gmail.com
\MultiMail::to($user)->from('email@example.net')->locale('en')->send(new \App\Mail\Invitation($user));

```

### Queued Mails

[](#queued-mails)

Queued mails work exactly the same as for the normal [Mail](https://laravel.com/docs/5.8/mail#queueing-mail) facade, i.e. they are either send explicitly be the `queue` method or the mailable class implements the `ShouldQueue` contract.

```
// Queue Mail
\MultiMail::from('contact@foo.org')->queue(new \App\Mail\Invitation($user));

```

It is of course necessary to install a [queue driver](https://laravel.com/docs/5.8/queues#driver-prerequisites).

### Specify in mailable

[](#specify-in-mailable)

You may set `to`, `cc`, `bcc`, `locale` and `from` in your mailable class. In this case, you could reduce the basic example from above to:

```
// Send mail from office@example.net
\MultiMail::send(new \App\Mail\Invitation($user));

```

Mailable:

```
/**
 * Create a new message instance.
 *
 * @return void
 */
public function __construct($user)
{
  $this->to  = $user;
  $this->fromMailer = 'office@example.com'
  $this->locale('en');
}

/**
 * Build the message.
 *
 * @return $this
 */
public function build()
{
    return $this->markdown('emails.invitation')
                ->subject('Invitation mail');
}

```

### Bulk messages

[](#bulk-messages)

For bulk messages, you may first require a mailer object. You can define a pause in seconds ($timeout) after a number of mails ($frequency) has been send.

```
$mailer = \MultiMail::getMailer('office@example.com' , $timeout, $frequency);

```

Then you can iterate through your list.

```
foreach($users as $user){
$mailer->send(new \App\Mail\Invitation($user));
};

```

Special Settings
----------------

[](#special-settings)

### Multiple Mail Providers

[](#multiple-mail-providers)

If you wish to send from mails with different provider, then you may create another provider in the `provider` array and reference it inside the `emails` array:

```
'emails'  => [
    'office@example.net' =>
        [
          'pass'     => env('first_mail_password'),
          'username' => env('first_mail_username'),
          'from_name'     => "Max Musterman",
                                                    //
        [
          'pass'     => env('second_mail_password'),
          'username' => env('second_mail_username'),
          'from_name'     => "Alice Armania",
          'provider' => 'new_provider',            //  [
  'default' =>
    [
      'host'       => env('MAIL_HOST'),
      'port'       => env('MAIL_PORT'),
      'encryption' => env('MAIL_ENCRYPTION'),
      'driver'     => env('MAIL_DRIVER'),
    ],
  'new_provider' =>
    [
      'host'      => env('MAIL_HOST_PROVIDER_B'),
      'port'      => env('MAIL_PORT_PROVIDER_B'),
      'encryption' => env('MAIL_ENCRYPTION_PROVIDER_B'),
  'driver'     => env('MAIL_DRIVER_B'),
  // you may also add options like `stream`, `source_ip` and `local_domain`
    ]'
],

```

### Default mailaccount

[](#default-mailaccount)

You may provide `default` credentials inside the `email` array from `config/multimail.php`:

```
'emails'  => [
    'office@example.net' =>
        [
          'pass'     => env('first_mail_password'),
          'username' => env('first_mail_username'),
          'from_name'     => "Max Musterman",
        ],
    'contact@example.net'  =>
        [
          'pass'     => env('second_mail_password'),
          'username' => env('second_mail_username'),
          'from_name'     => "Alice Armania",
        ],
    'default' =>
      [
        'pass'            => env('MAIL_PASSWORD'),
        'username'        => env('MAIL_USERNAME'),
      ]
],

```

When `first_mail_password` and `first_mail_username` are empty, `office@example.net` will use credentials specified by `default`. This is useful for your local development, when you want to send all mails from one mailaccount while testing. This way you only need to specify `MAIL_PASSWORD` and `MAIL_USERNAME` locally.

Testing
-------

[](#testing)

#### Don't put credentials in local `env`

[](#dont-put-credentials-in-local-env)

Do not specify any email accounts in your local `.env`. Otherwise you may risk to send testing mails to actual users.

#### Use one fake mail account or log

[](#use-one-fake-mail-account-or-log)

Use `log` driver or setup a fake mail SMTP account like [mailtrap](https://mailtrap.io/) or similar services.

It is not needed to specify the same credentials for all your email accounts. Instead, simply provide a default mail account (see above `Default mail account`).

#### Use log mail driver on testing

[](#use-log-mail-driver-on-testing)

To avoid latency, I recommend to always use the `log` mail driver when `phpunit` is running. You can set the mail driver in your `phpunit.xml` file like this: ``.

#### Use Mocking

[](#use-mocking)

If you want to use the mocking feature [Mail fake](https://laravel.com/docs/mocking#mail-fake) during your tests, enable `use_default_mail_facade_in_tests`in your config file `config/multimail.php`. Note that `assertQueued` will never be true, because `queued` mails are actually send through `sent` through a job.

### Get Mail From Database

[](#get-mail-from-database)

If you want to load your mail account configuration from database publish the package migrations:

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

```

In your migration folder are now two tabels, email\_accounts and email\_providers

Instead of adding emails to the config they should be added to the table email\_accounts.

Make sure to update your config `config/multimail.php`:

```
