Hi, everyone! Hope you all are good and enjoying learning our Tutorial Series on Mandrill API.
Today, we are here again with one more feature of Mandrill API.

In this tutorial, we are going to track complete email sending from your Mandrill account via Mandrill API key just like any Data Analytics Tools Where you can have a complete email analytics of your emails and can track your clients smartly.

This is useful to track Mandrill usage pattern from within your application.

We will track all the email sending activities done from a single email address [email protected]

The tracked data will include the following details:

1. Number of emails sent from your Mandrill account.
2. Number of emails opens (Total Opens and Unique Opens)
3. Number of link clicks in the email
4. Number of emails bounces
5. Complete monthly email sending activity (sorted day wise)

We will need two things for this:

  1. Mandrill API key: – Generated API key of any Mandrill account.
    Note: – If you don’t have any Mandrill API Key, don’t worry. Just follow the tutorial link Steps to Generate Mandrill API Key. We have explained a very simple way to generate Mandrill API Key.
  2. Emails address: – Any valid email address.
    Note: – You can read our article Get Sender’s Email List Using Mandrill API Key. Try its demo and get the senders email addresses to use in the script.

We will pass these two data into the Mandrill Library using the code given below.

 // Pass user provided Mandrill API key to Mandrill library
$mandrill = new Mandrill($key);
// Get Mandrill API using sender's info
$result = $mandrill->senders->info($email);

The above-mentioned code will return us all the necessary details of the email address, which we will retrieve and display as output.


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


MailGet is an online email management service which allow multiple API/SMTP integration to send bulk and drip emails. It also provide you a dashboard to manage your records.

Let’s have a look at the scripting part.

1. Download the latest Mandrill library .zip folder.

2. Create lib folder in the root folder of your application. Now, extract the Mandrill library zip file, copy & paste all the files available in the src folder of extracted zip folder into lib folder.

3. Similarly, create js and css folder. Download and insert latest jQuery.js and Bootstrap.css file in it.

4. Create index.php file in the root folder and paste the following PHP code in it.

index.php

Display a form, get input from users and send it to the Mandrill library file. Retrieve return data and generate output to show as a result.

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>Get Sender's Statistics Using Mandrill API Key</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 ("lib/Mandrill.php");
$error_msg = "";
?>
<div class="container">
<div class="row">
<div id="main">
<h1>Get Sender's Statistics Using Mandrill API Key</h1>
<div id="login">
<h2>Mandrill User's Info Search Box</h2>
<hr>
<form action="" method="POST">
<label>Mandrill API-Key : </label> <input type="text" name="mandrill_api_key" class="mandrill-api-key" placeholder="Enter Mandrill API Key"/>
<label id="sen-email-lab">Sender's Email Address : </label> <input type="email" name="sender_email" class="sender-email" placeholder="Enter Sender's Email Address"/>
<input type="submit" value="Search" id="submit"/>
<span></span>
</form>
</div>
<?php
if (isset($_POST['mandrill_api_key']) && isset($_POST['sender_email'])) {
$key = $_POST['mandrill_api_key'];
$email = $_POST['sender_email'];

// Pass user provided Mandrill API key to Mandrill library
$mandrill = new Mandrill($key);
// Get Mandrill API using sender's info
$result = $mandrill->senders->info($email);
if (isset($result['message'])) {
$error_msg = $result['message'];
echo "<script>alert('" . $error_msg . "');</script>";
} else {
?>
<!-- Result div start -->
<div class="result">
<h1 class="res">Result</h1>
<div class="user_info">
<ul class="info">
<li class="index_info"> Email Address </li><li class='val'> : <?php echo $result['address'] ?></li>
<li class="index_info"> Created At </li><li class='val'> : <?php echo $result['created_at'] ?></li>
<li class="index_info"> Total Emails Sent </li><li class='val'> : <?php echo $result['sent'] ?></li>
<li class="index_info"> Reputation </li><li class='val'> : <?php echo $result['reputation'] ?></li>
<li class="index_info"> Total Emails opens </li><li class='val'> : <?php echo $result['opens'] ?></li>
<li class="index_info"> Total Emails Unique Opens </li><li class='val'> : <?php echo $result['unique_opens'] ?></li>
<li class="index_info"> Total Emails Clicks </li><li class='val'> : <?php echo $result['clicks'] ?></li>
<li class="index_info"> Total Emails Unique Clicks </li><li class='val'> : <?php echo $result['unique_clicks'] ?></li>
<li class="index_info"> Total Emails Hard Bounced </li><li class='val'> : <?php echo $result['hard_bounces'] ?></li>
<li class="index_info"> Total Emails Soft Bounced </li><li class='val'> : <?php echo $result['soft_bounces'] ?></li>
<li class="index_info"> Total Emails Rejected </li><li class='val'> : <?php echo $result['rejects'] ?></li>
<li class="index_info"> Total Emails Complaints as Spam </li><li class='val'> : <?php echo $result['complaints'] ?></li>
<li class="index_info"> Total Emails unsubscribes </li><li class='val'> : <?php echo $result['unsubs'] ?></li>
</ul>
<div id="tabs-container">
<h2 class="msg_history">Message History</h2>
<ul class="tabs-menu">
<li class="current"><a href="#tab-1">Today</a></li>
<li><a href="#tab-2">Last 7 day's</a></li>
<li><a href="#tab-3">Last 30 day's</a></li>
<li><a href="#tab-4">Last 60 day's</a></li>
<li><a href="#tab-5">Last 90 day's</a></li>
</ul>
<div class="tab">
<div id="tab-1" class="tab-content">
<ul class="tab-info">
<?php
foreach ($result['stats']['today'] as $key => $val) {
echo "<li class='index'>" . $key . "</li><li class='val'> : " . $val . "</li><br/>";
?>
<?php } ?>
</ul>
</div>
<div id="tab-2" class="tab-content">
<ul class="tab-info">
<?php
foreach ($result['stats']['last_7_days'] as $key => $val) {
echo "<li class='index'>" . $key . "</li><li class='val'> : " . $val . "</li><br/>";
?>
<?php } ?>
</ul>
</div>
<div id="tab-3" class="tab-content">
<ul class="tab-info">
<?php
foreach ($result['stats']['last_30_days'] as $key => $val) {
echo "<li class='index'>" . $key . "</li><li class='val'> : " . $val . "</li><br/>";
?>
<?php } ?>
</ul>
</div>
<div id="tab-4" class="tab-content">
<ul class="tab-info">
<?php
foreach ($result['stats']['last_60_days'] as $key => $val) {
echo "<li class='index'>" . $key . "</li><li class='val'> : " . $val . "</li><br/>";
?>
<?php } ?>
</ul>
</div>
<div id="tab-5" class="tab-content">
<ul class="tab-info">
<?php
foreach ($result['stats']['last_90_days'] as $key => $val) {
echo "<li class='index'>" . $key . "</li><li class='val'> : " . $val . "</li><br/>";
?>
<?php } ?>
</ul>
</div>
</div>
</div>
</div>
</div>
<!-- Result div End -->
<?php
}
}
?>
</div>
</div>
</div>
<script>
jQuery(document).ready(function() {
$("#submit").click(function(event) {
var Mandrillkey = jQuery('.mandrill-api-key').val();
var MandrillEmail = jQuery('.sender-email').val();
if (Mandrillkey == "") {
event.preventDefault();
alert('Please insert Mandrill API Key!!!');
}
if (MandrillEmail == "") {
event.preventDefault();
alert('Please Enter Sender's Email!!!');
}
});
$(".tabs-menu a").click(function(event) {
event.preventDefault();
$(this).parent().addClass("current");
$(this).parent().siblings().removeClass("current");
var tab = $(this).attr("href");
$(".tab-content").not(tab).css("display", "none");
$(tab).fadeIn();
});
});
</script>
</body>
</html>

5. Create style.css file in CSS folder and paste the following code in the file.

style.css

Includes basic styling of HTML elements.

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

#main{
width:100%;
min-width: 320px;
margin:50px auto;
font-family:raleway;
}
h2{
background-color: #FEFFED;
text-align:center;
border-radius: 10px 10px 0 0;
margin: -10px -40px -20px !important;
padding: 15px;
}
hr{
border:0;
border-bottom:1px solid #ccc;
margin: 10px -40px;
margin-bottom: 30px;
}
ul{
text-decoration: none;
}
ul li{
display: inline-block;
}
#login{
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: 15px;
}
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;
}
div#main h1 {
text-align: center;
}
h1.res {
margin-top: 51px;
}
div.user_info {
width: 650px;
position: relative;
background: #FEFFED;
border: 1px solid #d4d4d1;
border-radius: 10px 10px 5px 5px;
margin: 30px auto;
padding-bottom: 0;
text-align: center;
}
ul.info {
text-align: left;
margin: 20px auto;
}
ul.info li {
margin: 0 auto;
font-size: 18px;
}
li.index_info{
width: 50%;
text-align: left;
font-weight: bold;
}
ul.tab-info {
text-align: left;
}
ul.tab-info li.val {
float: right;
margin-right: 25%;
}
li.index {
font-weight: bold;
margin-left: 15%;
}
div#tabs-container {
margin: 0 5px;
border: 1px solid #d4d4d1;
background: #fff;
padding-top: 10px;
margin-bottom: 5px;
}
.user_info h3 {
background-color: #FEFFED;
text-align: center;
padding: 15px;
margin-top: 0;
border-bottom: 1px solid #d4d4d1;
border-radius: 10px 10px 0 0;
}
.tabs-menu {
height: 30px;
float: left;
clear: both;
margin-left: 20px;
}
.tabs-menu li {
height: 30px;
line-height: 30px;
float: left;
background-color: #FFBC00;
border: 1px solid #d4d4d1;
}
.tabs-menu li.current {
position: relative;
background-color: #fff;
z-index: 5;
}
ul.tab-info li {
padding: 5px;
font-size: 16px;
}
.tabs-menu li a {
padding: 10px;
text-transform: uppercase;
color: #fff;
text-decoration: none;
}
.tabs-menu .current a {
color: #2e7da3;
}
.tab {
background-color: #fff;
margin-bottom: 5px;
width: auto;
}
.tab-content {
text-align: center;
padding: 20px;
display: none;
}
ul.tabs-menu {
width: 100%;
margin-top: 20px;
}
#tab-1 {
display: block;
}
h2.msg_history{
text-align:center;
margin-top: -10px;
margin-bottom: 0;
padding: 15px;
margin-left: -120px;
background: none;
}
@media only screen and (max-width:767px){
.tabs-menu {
margin-left: 15%;
height:auto;
}
.tabs-menu li {
width: 150px;
clear: both;
}
div.user_info {
width: 350px;
}
.tab {
margin: 0 10px;
}
li.val {
float: right;
margin-right: 45px;
}
ul.tab-info {
text-align: left;
}
ul.info li{
margin-bottom: 10px;
}
ul.info li.val {
margin-right: 15%;
}
li.index {
margin-left:0;
}
}

Run the script and have fun!!!

Conclusion:

Using this script you can easily get all the information of sender using Mandrill API Key. Must try the script and post your feedback from the space given below.

Recommended blogs –