In this tutorial, we will learn to find the email address of all users who sent emails using Mandrill API Key.

We will tell you about email list management by generating a list of email address of the senders who used a Mandrill Key to send the email to another user. The generated list can be download as exported CSV file.

For this, we will require a valid Mandrill API key.

Note: – If you don’t have any Mandrill API Key, don’t worry. Just follow the tutorial link Steps to Generate Mandrill API KeyWe have explained all the steps in a very simple way so that anyone can generate Mandrill API Key easily.

The mandrill key will be passed to the Mandrill library file using the code given below, which will provide the detailed information about the sender.

//Pass user provided Mandrill API key to Mandrill library
$mandrill = new Mandrill($key);

//Get Mandrill API-Used Sender's Info
$result = $mandrill->senders->getList();

The email address of senders will then retrieved from the details and processed further to create an exported CSV file.


Watch the live demo or download code from the link given below
get senders email list using mandrill api key


MailGet is an online Email management service. You can send bulk and drip emails using this service. It also provide a dashboard for easy mailing list management.

Let’s have a detailed look at the whole process.

1> Download the latest Mandrill library .zip folder.

2> Create lib folder in the root folder of your project. Now, extract the Mandrill library folder, 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 PHP code given below in it.

index.php

Provide a form to enter a Mandrill API Key. Send the entered API key to Mandrill library file and generate the email list from the returned sender’s details.

Also, send the email lists to CSVDownload.php file to generate downloaded CSV file.

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>Get sender's Email List 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 Mandrill library file
include ("lib/Mandrill.php");
$error_msg = "";
$lists = "";
?>
<div class="container">
<div class="row">
<div id="main">
<h1>Get sender's Email List Using Mandrill API Key</h1>
<div id="login">
<h2>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"/>
<input type="submit" value="Get Sender's Email" id="submit"/>
<span></span>
</form>
</div>
<?php
if (isset($_POST['mandrill_api_key'])) {
$key = $_POST['mandrill_api_key'];

//Pass user provided Mandrill API key to Mandrill library
$mandrill = new Mandrill($key);

//Get Mandrill API-Used Sender's Info
$result = $mandrill->senders->getList();

if (isset($result['message'])) {
$error_msg = $result['message'];
echo "<script>alert('" . $error_msg . "');</script>";
} else {
?>
<div class="result" class="col-xs-10 col-md-6">
<!-- Result div start -->
<h1 class="res">Result</h1>
<div class="user_info">
<h3>Sender's Email Addresses</h3>
<ul class="info">
<?php
// Retrieving Sender's Email Address
$senLists = array();
$i = 1;
foreach ($result as $value) {
echo "<li class='index'>" . $i . "> " . $value['address'] . "</li><br/>";
$senLists[] = $value['address'];
$i++;
}
echo $lists;
$lists = json_encode($senLists);
?>
<!-- Download CSV Section -->
<form action="CSVDownload.php" method="post" >
<input type="hidden" name="lists" value="<?php echo htmlspecialchars($lists); ?>" />
<input type="submit" value="Export CSV" class="csv_download" />
</form>
</ul>
</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();
if (Mandrillkey == "") {
event.preventDefault();
alert('Please insert Mandrill API Key!!!');
}
});
});
</script>
</body>
</html>

5> Create CSVDownload.php file and paste the following code in it.

CSVDownload.php

Generate exported CSV file from the email list send by index.php file.

<?php
$sen_lists = array();
$sen_lists = json_decode($_POST['lists']);
foreach ($sen_lists as $sen_emails) {
$res[][] = $sen_emails;
}
header("Content-type: text/csv");
header("Content-Disposition: attachment; filename=Sender's Lists.csv");
header("Pragma: no-cache");
header("Expires: 0");
$file = fopen('php://output', 'w');
fputcsv($file, array('Sender\'s Email Lists'));
if(!empty($res)){
foreach ($res as $row) {
fputcsv($file, $row);
}
}
exit();
?>

6> Create style.css file in CSS folder and paste the code given below 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:300px;
border-radius: 10px;
font-family:raleway;
border: 2px solid #ccc;
padding: 10px 40px 25px;
margin: 70px auto 0 auto;
}
input[type=text]{
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 {
max-width: 400px;
position: relative;
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-bottom: 60px;
}
.info li {
padding: 5px;
font-size: 18px;
}
li.index_info{
width: 50%;
text-align: left;
font-weight: bold;
}
li.index {
font-weight: bold;
}
.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;
}
input.csv_download {
width: 125px;
background: none;
border: none;
outline:none;
color: blue;
text-decoration: underline;
position: absolute;
font-size: 16px;
bottom: 0;
right: 0;
}

Run the script and enjoy!!!

Conclusion:

Hope you have enjoyed the script and understand the concept. Please send us your feedback from the space given below. We will be back soon with a new trick.