How to integrate Mandrill mail server in Laravel – onlinecode

How to integrate Mandrill mail server in Laravel – onlinecode

In this post we will give you information about How to integrate Mandrill mail server in Laravel – onlinecode. Hear we will give you detail about How to integrate Mandrill mail server in Laravel – onlinecodeAnd how to use it also give you demo for it if it is necessary.

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. returnarray(
  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. );
<?php

return array(

	/*
	|--------------------------------------------------------------------------
	| Mail Driver
	|--------------------------------------------------------------------------
	|
	| Laravel supports both SMTP and PHP's "mail" function as drivers for the
	| sending of e-mail. You may specify which one you're using throughout
	| your application here. By default, Laravel is setup for SMTP mail.
	|
	| Supported: "smtp", "mail", "sendmail", "mailgun", "mandrill", "log"
	|
	*/

	'driver' => 'mandrill',

	/*
	|--------------------------------------------------------------------------
	| SMTP Host Address
	|--------------------------------------------------------------------------
	|
	| Here you may provide the host address of the SMTP server used by your
	| applications. A default option is provided that is compatible with
	| the Mailgun mail service which will provide reliable deliveries.
	|
	*/

	'host' => 'smtp.mandrillapp.com',

	/*
	|--------------------------------------------------------------------------
	| SMTP Host Port
	|--------------------------------------------------------------------------
	|
	| This is the SMTP port used by your application to deliver e-mails to
	| users of the application. Like the host we have set this value to
	| stay compatible with the Mailgun e-mail application by default.
	|
	*/

	'port' => 587,

	/*
	|--------------------------------------------------------------------------
	| Global "From" Address
	|--------------------------------------------------------------------------
	|
	| You may wish for all e-mails sent by your application to be sent from
	| the same address. Here, you may specify a name and address that is
	| used globally for all e-mails that are sent by your application.
	|
	*/

	'from' => array('address' => 'from_email_address', 'name' => 'from_name'),

	/*
	|--------------------------------------------------------------------------
	| E-Mail Encryption Protocol
	|--------------------------------------------------------------------------
	|
	| Here you may specify the encryption protocol that should be used when
	| the application send e-mail messages. A sensible default using the
	| transport layer security protocol should provide great security.
	|
	*/

	'encryption' => 'tls',

	/*
	|--------------------------------------------------------------------------
	| SMTP Server Username
	|--------------------------------------------------------------------------
	|
	| If your SMTP server requires a username for authentication, you should
	| set it here. This will get used to authenticate with your server on
	| connection. You may also set the "password" value below this one.
	|
	*/

	'username' => 'mandrill_login_email',

	/*
	|--------------------------------------------------------------------------
	| SMTP Server Password
	|--------------------------------------------------------------------------
	|
	| Here you may set the password required by your SMTP server to send out
	| messages from your application. This will be given to the server on
	| connection so that the application will be able to send messages.
	|
	*/

	'password' => 'mandrill_password',

	/*
	|--------------------------------------------------------------------------
	| Sendmail System Path
	|--------------------------------------------------------------------------
	|
	| When using the "sendmail" driver to send e-mails, we will need to know
	| the path to where Sendmail lives on this server. A default path has
	| been provided here, which will work well on most of your systems.
	|
	*/

	'sendmail' => '/usr/sbin/sendmail -bs',

	/*
	|--------------------------------------------------------------------------
	| Mail "Pretend"
	|--------------------------------------------------------------------------
	|
	| When this option is enabled, e-mail will not actually be sent over the
	| web and will instead be written to your application's logs files so
	| you may inspect the message. This is great for local development.
	|
	*/

	'pretend' => false,

);

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. });
Mail::send('welcome', [], function ($message){
    $message->to('recipient_email_address')->subject('Expertphp.in - Testing mail');
});

Mandrill is now trusted by more than 500,000 customers and it can deliver your email in milliseconds with servers all over the world

Hope this code and post will helped you for implement How to integrate Mandrill mail server in Laravel – onlinecode. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

Leave a Comment

Your email address will not be published. Required fields are marked *

  +  25  =  32

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US