Nowadays, Email marketing is increasing rapidly and become one of the most demanding services in the internet business. In email marketing, emails are one of the most powerful communication media which create a strong bond between companies and their clients.

Sending emails to a large number of clients are one of the most difficult and challenging tasks for the users. They need a powerful medium to send emails to their clients.

Keeping in mind such requirement of users for sending emails, we are creating a blog to Send Email via Mandrill API Using PHP.

We are going to integrate Mandrill API/SMTP services for sending personalized and one-to-one e-commerce emails.

It can also be used to send automated transactional emails like password resets, order confirmations, and welcome messages.


Watch the live demo or download code from the link given below

send email using mandrill api in php


We have introduced a very effective and powerful online email management sevice name MailGet. Using MailGet you can easily send bulk emails through Mandrill and manage your emails at very low cost.

Steps to configure Mandrill API in an application: –

Step 1: – Download the latest Mandrill API library .zip folder.
Step 2: – Extract the downloaded .zip folder.
Step 3: – Create a lib folder in the root folder, copy & paste all the files available in the src folder of extracted zip folder.
Step 4: – Create index.php and paste the PHP code given below in it.

index.php

Provide an interactive user interface to insert important information like sender’s email, reciever’s email, message etc. These information will be stored in an array which will further passed to the Mandrill API library and send emails.

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>Send Email via Mandrill API Using PHP</title>
<link rel="stylesheet" type="text/css" href="css/style.css"/>
<script src="js/jquery.js"></script>
</head>
<body>
<?php

// include Mandrill library file
include ("lib/Mandrill.php");

// Passing Mandrill API key in the library
$mandrill = new Mandrill('<-- Mandrill API-Key -->');
$sen_name = "";
$sen_email = "";
$rec_name = "";
$rec_email = "";
$email_type = "";
$email_sub = "";
$msg_type = "";
$box_msg = "";
$message = array();
$to = array();
if (isset($_POST['sen_name'])) {
$sen_name = $_POST['sen_name'];
}
if (isset($_POST['sen_email'])) {
$sen_email = $_POST['sen_email'];
}
if (isset($_POST['rec_name'])) {
$rec_name = $_POST['rec_name'];
}
if (isset($_POST['rec_email'])) {
$rec_email = $_POST['rec_email'];
}
if (isset($_POST['email_type'])) {
$email_type = $_POST['email_type'];
}
if (isset($_POST['email_sub'])) {
$email_sub = $_POST['email_sub'];
}
if (isset($_POST['msg_type'])) {
$msg_type = $_POST['msg_type'];
}
if (isset($_POST['box_msg'])) {
$box_msg = $_POST['box_msg'];
}
$to[] = array(
'email' => $rec_email,
'name' => $rec_name,
'type' => $email_type
);
$message['subject'] = $email_sub;
$message[$msg_type] = $box_msg;
$message['from_email'] = $sen_email;
$message['from_name'] = $sen_name;
$message['to'] = $to;
if(isset($to[0]['email']) && $to[0]['email'] !== ""){
$result = $mandrill->messages->send($message);
$status = $result[0]['status'];
}
?>

<div id="main">
<h1>Send Email via Mandrill API Using PHP</h1>
<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 Name (Optional) : </label> <input type="text" name="rec_name" class="" placeholder="Enter Receiver's Name"/>
<label>Receiver's Email Address : </label> <input type="email" name="rec_email" class="rec_email" placeholder="Enter Reciever's Email Address"/>
<label>Email Type : </label>
<input type="radio" name="email_type" value="to" checked="checked" /><label> Default </label>
<input type="radio" name="email_type" value="cc"/><label> cc </label>
<input type="radio" name="email_type" value="bcc"/><label> bcc </label>
<label>Subject : </label>
<input type="text" name="email_sub" class="" placeholder="Subject"/>
<label>Message : </label>
<input type="radio" name="msg_type" value="text" checked="checked"/><label>text</label>
<input type="radio" name="msg_type" value="html"/><label>html</label>
<textarea name="box_msg" rows="10" cols="30">Write your message here...</textarea>
<input type="submit" value="Check" id="submit"/>
</form>
</div>

<div id="note">
<?php
if (isset($status)){
if($status == "sent") {
echo "<script>alert('Congratulations!!! Your Email has been sent successfully!!!')</script>";
} elseif($status == "rejected") {
echo "<script>alert('Sorry!!! Some error occurs, Please try again.')</script>";
}
}
?>
</div>
</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 style.css file and paste the css code given below in it.

style.css

Includes basic styling of HTML elements.

 

@import url(http://fonts.googleapis.com/css?family=Raleway);

#main{
width:960px;
margin:50px auto;
font-family:raleway;
}
h2{
background-color: #FEFFED;
text-align:center;
border-radius: 10px 10px 0 0;
margin: -10px -40px;
padding: 15px;
}
hr{
border:0;
border-bottom:1px solid #ccc;
margin: 10px -40px;
margin-bottom: 30px;
}
#login{
width:300px;
float: left;
border-radius: 10px;
font-family:raleway;
border: 2px solid #ccc;
padding: 10px 40px 25px;
margin-top: 70px;
}
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;
}
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%;
}
div#main h1 {
margin-left: -80px;
}

Conclusion :

Hope you have enjoyed reading the above post and will definitely integrate and use Mandrill API for sending emails. Always keep in touch with us for more interesting blogs. You can send us your feedback and share your experience about the blog in the space given below 🙂