How to integrate Mandrill mail server in Laravel

How to integrate Mandrill mail server in Laravel

How to integrate Mandrill mail server in Laravel

In this tutorial, i will tell you how to integrate Mandrill mail server in Laravel.

Its very essential part to send emails in web application. There are some reason why we send emails to our subsribers as notification, newsletters,marketing, adverts etc.

There are so many services available to provide an API for sending emails to users for engagement.

Mandrill is a reliable and secure email infrastructure service and it's very easy to integrate in Laravel application.

Driver Prerequisites

First you will have to install Guzzle HTTP library by running following command :

"guzzlehttp/guzzle": "~5.3|~6.0"

After Guzzle installation, you will have to set driver option in your config/mail.php.

For Mandrill Mail Server you must have API Key to use mandrill service.

If you do not have mandrill account then create first to get API key.

  1. <?php
  2. return array(
  3.     /*
  4.     |--------------------------------------------------------------------------
  5.     | Mail Driver
  6.     |--------------------------------------------------------------------------
  7.     |
  8.     | Laravel supports both SMTP and PHP's "mail" function as drivers for the
  9.     | sending of e-mail. You may specify which one you're using throughout
  10.     | your application here. By default, Laravel is setup for SMTP mail.
  11.     |
  12.     | Supported: "smtp", "mail", "sendmail", "mailgun", "mandrill", "log"
  13.     |
  14.     */
  15.     'driver' => 'mandrill',
  16.     /*
  17.     |--------------------------------------------------------------------------
  18.     | SMTP Host Address
  19.     |--------------------------------------------------------------------------
  20.     |
  21.     | Here you may provide the host address of the SMTP server used by your
  22.     | applications. A default option is provided that is compatible with
  23.     | the Mailgun mail service which will provide reliable deliveries.
  24.     |
  25.     */
  26.     'host' => 'smtp.mandrillapp.com',
  27.     /*
  28.     |--------------------------------------------------------------------------
  29.     | SMTP Host Port
  30.     |--------------------------------------------------------------------------
  31.     |
  32.     | This is the SMTP port used by your application to deliver e-mails to
  33.     | users of the application. Like the host we have set this value to
  34.     | stay compatible with the Mailgun e-mail application by default.
  35.     |
  36.     */
  37.     'port' => 587,
  38.     /*
  39.     |--------------------------------------------------------------------------
  40.     | Global "From" Address
  41.     |--------------------------------------------------------------------------
  42.     |
  43.     | You may wish for all e-mails sent by your application to be sent from
  44.     | the same address. Here, you may specify a name and address that is
  45.     | used globally for all e-mails that are sent by your application.
  46.     |
  47.     */
  48.     'from' => array('address' => 'from_email_address', 'name' => 'from_name'),
  49.     /*
  50.     |--------------------------------------------------------------------------
  51.     | E-Mail Encryption Protocol
  52.     |--------------------------------------------------------------------------
  53.     |
  54.     | Here you may specify the encryption protocol that should be used when
  55.     | the application send e-mail messages. A sensible default using the
  56.     | transport layer security protocol should provide great security.
  57.     |
  58.     */
  59.     'encryption' => 'tls',
  60.     /*
  61.     |--------------------------------------------------------------------------
  62.     | SMTP Server Username
  63.     |--------------------------------------------------------------------------
  64.     |
  65.     | If your SMTP server requires a username for authentication, you should
  66.     | set it here. This will get used to authenticate with your server on
  67.     | connection. You may also set the "password" value below this one.
  68.     |
  69.     */
  70.     'username' => 'mandrill_login_email',
  71.     /*
  72.     |--------------------------------------------------------------------------
  73.     | SMTP Server Password
  74.     |--------------------------------------------------------------------------
  75.     |
  76.     | Here you may set the password required by your SMTP server to send out
  77.     | messages from your application. This will be given to the server on
  78.     | connection so that the application will be able to send messages.
  79.     |
  80.     */
  81.     'password' => 'mandrill_password',
  82.     /*
  83.     |--------------------------------------------------------------------------
  84.     | Sendmail System Path
  85.     |--------------------------------------------------------------------------
  86.     |
  87.     | When using the "sendmail" driver to send e-mails, we will need to know
  88.     | the path to where Sendmail lives on this server. A default path has
  89.     | been provided here, which will work well on most of your systems.
  90.     |
  91.     */
  92.     'sendmail' => '/usr/sbin/sendmail -bs',
  93.     /*
  94.     |--------------------------------------------------------------------------
  95.     | Mail "Pretend"
  96.     |--------------------------------------------------------------------------
  97.     |
  98.     | When this option is enabled, e-mail will not actually be sent over the
  99.     | web and will instead be written to your application's logs files so
  100.     | you may inspect the message. This is great for local development.
  101.     |
  102.     */
  103.     'pretend' => false,
  104. );

Now set your api key in config/services.php


'mandrill' => [
    'secret' => 'your-mandrill-key',
],

Sending Emails

For testing, you can paste below line of code in your routes.php and check if you are able to send emails or not.

  1. \Mail::send('welcome', [], function ($message){
  2. $message->to('recipient_email_address')->subject('Expertphp.in - Testing mail');
  3. });

Phone: (+91) 8800417876
Noida, 201301