Coder Social home page Coder Social logo

laravel-multimail's Introduction

Laravel Multimail

Mail image

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

Requirements

Laravel 5, 6 or 7

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"

Configure your email clients in config/multimail.php:

'emails'  => [
    '[email protected]' =>
        [
          'pass'     => env('first_mail_password'),
          'from_name'     => "Max Musterman",
        ],
    '[email protected]'  =>
        [
          '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:

Attribut Functionality required
pass Password of email account yes
username Username of email account, only neccessary if different from email address no
from_name Name that should appear in front of email no
provider Provider of email account, only necessary if mail host/encription/port is not default (see here for more) no

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 (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. When using send or queue the mail will be send from the mail account specified in cofing/multimail.php.

Basic Examples

This example assumes that [email protected] and [email protected] have been specified in config/multimail.php.

// Send mail from [email protected]
\MultiMail::to($user)->from('[email protected]')->send(new \App\Mail\Invitation($user));

// Send from malaccount [email protected]
\MultiMail::to($user)->from('[email protected]')->locale('en')->send(new \App\Mail\Invitation($user));

Queued Mails

Queued mails work exactly the same as for the normal 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('[email protected]')->queue(new \App\Mail\Invitation($user));

It is of course necessary to install a queue driver.

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 [email protected]
\MultiMail::send(new \App\Mail\Invitation($user));

Mailable:

/**
 * Create a new message instance.
 *
 * @return void
 */
public function __construct($user)
{
  $this->to  = $user;
  $this->fromMailer = '[email protected]'
  $this->locale('en');
}

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

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('[email protected]' , $timeout, $frequency);

Then you can iterate through your list.

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

Special Settings

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'  => [
    '[email protected]' =>
        [
          'pass'     => env('first_mail_password'),
          'username' => env('first_mail_username'),
          'from_name'     => "Max Musterman",   
                                                    // <------ no provider given because 'default' provider is used
        ],
    'contact@other_domain.net'  =>
        [
          'pass'     => env('second_mail_password'),
          'username' => env('second_mail_username'),
          'from_name'     => "Alice Armania",
          'provider' => 'new_provider',            // <------ specify new provider here
        ],
],

'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

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

'emails'  => [
    '[email protected]' =>
        [
          'pass'     => env('first_mail_password'),
          'username' => env('first_mail_username'),
          'from_name'     => "Max Musterman",
        ],
    '[email protected]'  =>
        [
          '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, [email protected] 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

Don't 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 log driver or setup a fake mail SMTP account like mailtrap 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

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: <env name="MAIL_DRIVER" value="log"/>.

Use Mocking

If you want to use the mocking feature 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. Therefore, always use assertSent.

Troubleshoot

Laravel 7 is not working

Please update to version 1.2.2 to support Laravel 7

laravel-multimail's People

Contributors

alirezasedghi avatar iwasherefirst2 avatar jedliu avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.