Mandrill allows users to send transactional emails at low cost along with enhanced inbox deliverability.

This makes Mandrill SMTP an ideal choice for developers for sending emails for their applications.

In this tutorial, we are going to cover, how you can use Mandrill in your PHP application along with PHPMailer for sending emails.

Before I start, let me briefly describe the basic concepts used for email sending application:

1. PHPMailer: PHPMailer is a code library which contains a PHP class that provides a package of different functions used to send emails.

2. Mandrill: Mandrill is an email infrastructure service designed to help users to send transactional emails like emails for password resets, order confirmations, welcome messages and any other emails required by the application.

To send emails directly through PHP, it requires you to have good knowledge about SMTP protocol and know how of how to inject emails for sending.

Using PHPMailer and Mandrill is one of the best solutions for sending spam-free emails along with better inbox deliverability using PHP.


Watch the live demo or download code from the link given below.
send email using phpmailer & mandrill


We have introduced an online email sevice name MailGet. You can send your emails easily by using Mandrill SMTP credentials. It also supports multiple API/SMTP integration for sending emails.

Let’s begin learning step by step: –
Step 1: – Download latest PHPMailer library’s .zip folder.
Step 2: – Create a lib folder in the root folder of your application.Now, extract the downloaded PHPMailer’s library zip folder. Copy & paste PHPMailerAutoload.php, class.phpmailer.php and class.smtp.php file in the lib folder.
Step 3: Require a Mandrill username and Mandrill API Key.


Note: – If you don’t have any account in Mandrill, just follow the easy steps mentioned in the tutorial link Steps to Generate Mandrill API Key and get your username and API key.

Step 4: – Create index.php file in the root folder of the application and write the code given below in the file.

index.php

Display a message box containing sender’s email, receiver’s email, message etc. When a use enter all the value and click on Send button, the submitted value then passed through the library file and email is send to the sender’s address.

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>Send Email via PHPMailer and Mandrill's SMTP</title>
<link rel="stylesheet" type="text/css" href="css/style.css"/>
<link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
<script src="js/jquery.js"></script>
</head>
<body>
<?php
//include PHPMailerAutoload library file
include ("lib/PHPMailerAutoload.php");
$sen_name = "";
$sen_email = "";
$rec_email = "";
$email_sub = "";
$box_msg = "";

$mail = new PHPMailer();
// Telling the class to use SMTP
$mail->IsSMTP();
$mail->Mailer = "smtp";
$mail->Host = "smtp.mandrillapp.com";
$mail->Port = 587;
// Turn on SMTP authentication
$mail->SMTPAuth = true;
// SMTP Username
$mail->Username = "<-- Mandrill Username -->";
// SMTP Password ( i.e. Any valid Mandrill API key)
$mail->Password = "<-- Any valid Mandrill API Key -->";

// Retrieving & storing user's submitted information
if (isset($_POST['sen_name'])) {
$sen_name = $_POST['sen_name'];
}
if (isset($_POST['sen_email'])) {
$sen_email = $_POST['sen_email'];
}
if (isset($_POST['rec_email'])) {
$rec_email = $_POST['rec_email'];
}
if (isset($_POST['email_sub'])) {
$email_sub = $_POST['email_sub'];
}
if (isset($_POST['box_msg'])) {
$box_msg = $_POST['box_msg'];
}

$mail->From = $sen_email;
$mail->FromName = $sen_name;
$mail->AddAddress($rec_email);
$mail->Subject = $email_sub;
$mail->Body = $box_msg;
$mail->WordWrap = 50;

if (($sen_email != "") && ($rec_email != "" )) {
// Sending Email
$status = $mail->Send();
}
?>
<h1>Send Email via PHPMailer and Mandrill's SMTP</h1>
<div id="main" class="col-sm-12 col-md-6 col-lg-6">
<div id="login">
<h2>Message Box</h2>
<hr>
<form action="" method="POST">
<h3>From : </h3>
<label>Sender's Name (Optional) : </label> <input type="text" name="" class="" placeholder="Enter Sender's Name"/>
<label>Sender's Email Address : </label> <input type="email" name="sen_email" class="sen_email" placeholder="Enter Sender's Email Address"/>
<h3>To : </h3>
<label>Receiver's Email Address : </label> <input type="email" name="rec_email" class="rec_email" placeholder="Enter Reciever's Email Address"/>
<label>Subject : </label>
<input type="text" name="email_sub" class="" placeholder="Subject"/>
<label>Message : </label>
<textarea name="box_msg" rows="10" cols="30">Write your message here...</textarea>
<input type="submit" value="Send" id="submit"/>
</form>
</div>
<?php
if (isset($status)) {
if ($status == 1) {
echo "<script>alert('Congratulations!!! Your Email has been sent successfully!!!')</script>";
} else {
echo "<script>alert('Sorry!!! Message was not sent. Mailer error: " . $mail->ErrorInfo . ")</script>";
}
}
?>
</div>
<script>
jQuery(document).ready(function() {
jQuery("#submit").click(function() {
var sen_email = jQuery('.sen_email').val();
var rec_email = jQuery('.rec_email').val();
if (sen_email == "") {
alert('Sender\'s Email Address cannot be empty.');
}
if (rec_email == "") {
alert('Receiver\'s Email Address cannot be empty.');
}
});
});
</script>
</body>
</html>

Step 5: – Create a CSS folder in the root folder and create a style.css file containing the following CSS code.

style.css

Includes basic styling of HTML elements.

@import url(http://fonts.googleapis.com/css?family=Raleway);
body{
padding: 0;
}
#main{
float: none;
margin: 0 auto;
font-family:raleway;
}
h1 {
text-align: center;
}
h2{
background-color: #FEFFED;
text-align:center;
border-radius: 10px 10px 0 0;
margin: -10px -40px -20px !important;
padding: 15px;
}
#login{
width:50%;
min-width: 320px;
border-radius: 10px;
font-family:raleway;
border: 2px solid #ccc;
padding: 10px 40px 25px;
margin: 70px auto 0 auto;
}
input[type=text],input[type=email]{
width:100%;
padding: 10px;
margin-top: 8px;
border: 1px solid #ccc;
padding-left: 5px;
font-size: 16px;
font-family:raleway;
margin-bottom: 10px;
}
input[type=submit]{
width: 100%;
background-color:#FFBC00;
color: white;
border: 2px solid #FFCB00;
padding: 10px;
font-size:20px;
cursor:pointer;
border-radius: 5px;
margin-bottom: 15px;
}
textarea{
width: 100%;
margin-top: 10px !important;
margin-bottom: 15px !important;
}
@media only screen and (max-width:480px){
div#login{
width: 300px;
}
}

Now, insert the latest bootstrap.css file in the CSS folder. Also, create a js folder in root folder and insert the latest jQuery.js file in it.
Now, your application is ready to send email. Run the application and enjoy sending emails…

Conclusion:-

Hope you enjoyed the article. Must try it and send us your feedback from the space given below.