Managing contacts is an integral part of your marketing campaigns life-cycle. There is a time when you feel like to switch email service providers and the question arises as – ” How to transfer your contacts safely into the new service and keep them intact? “

Well,
Today I’ll show you to export your SendGrid Contacts using SendGrid’s Marketing Email API.

Where,
SendGrid is a transactional email delivery and management service. The service manages various types of email including shipping notifications, friend requests, sign-up confirmations, and email newsletters.
SendGrid creates the lists for identification add recipients email addresses and name in it.

Now,
This tutorial demonstrate on – How to export your SendGrid contact lists. Here, after inserting your SendGrid credentials i.e. your user id and password, all the contacts from different recipient list will be displayed. You can then download this list in the form of excel sheet through the download link.


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


Use MailGet – Email Service Provider for Export SendGrid’s  Emails List, just connect and send with SendGrid API or SMTP services.

Steps to Export Contacts From SendGrid: –

Step 1 :– Download the latest SendGrid Marketing Email API library.
Step 2 :- Extract the downloaded .zip folder.
Step 3 :- Create a CSS folder within root folder to place style.css and table.css files.
Step 4 :- Create index.php into your root folder and copy the code present under index.php section. Similarly create csvdownload.php  and copy the code present under  csvdownload.php section.

index.php

It provides an interface for user to fill the SendGrid account details like user name and password which will go to SendGrid’s Marketing Email API for further processing which will export all the contacts present in different recipient lists.



<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="css/bootstrap.css" rel="stylesheet" type="text/css"/>
<link href="css/style.css" rel="stylesheet" type="text/css"/>
<link href="css/table.css" rel="stylesheet" type="text/css"/>
<script src="js/jquery.min.js" type="text/javascript"></script>
<title>Export Contacts From SendGrid Via Marketing Email API</title>
<script type = "text/javascript" >
$(document).ready(function() {
$('#demo_keys').click(function() {
$('#dusername').val('ssssssssss');
$('#dpassword').val('xxxxxxxxxxx');
});
});
</script>

<script type="text/javascript">
function validate()
{

if (document.myForm.dusername.value == "")
{
alert("Please enter your User Name!");
document.myForm.dusername.focus();
return false;
}
if (document.myForm.dpassword.value == "")
{
alert("Please enter password!");
document.myForm.dpassword.focus();
return false;
}

return(true);
}
</script>

</head>
<body>
<div class="container">
<div class="row">

</div>
<div class="row">
<div class="col-md-12">
<div id="main">
<h1><b>Export Contacts From SendGrid's Marketing Email API</b></h1>
<a href="#" id="demo_keys" style="float:right; margin:39px 26% -13px 12px;font-weight: bold;font-size: 20px;">Use Demo API Key and Secret Key</a>
<br/><br/>
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
<div id="content">
<div id="login">
<h2>Enter Your SendGrid Credentials</h2><hr/>
<form name="myForm" action="" method="post" onsubmit="return validate();" >
<label><h3>User Name:</h3> </label> <br/>
<input type="text" name="dusername" id="dusername" placeholder="Enter User Name"/>
<label><h3>Password:</h3> </label> <br/>
<input type="password" name="dpassword" id="dpassword" placeholder="Enter Password"/><br/><br/>
<input type="submit" value="Submit" name="dsubmit" />
<span></span>
</form>
</div>
<br/>
<?php
if (isset($_POST['dsubmit'])) {
error_reporting(0);
require_once "sendgrid/newsletter.php";
if ($_POST['dusername'] == 'ssssssssss') {

$sg_user = "Enter Your SenGrid UserName";
} else {
$sg_user = $_POST['dusername'];
}
if ($_POST['dpassword'] == 'xxxxxxxxxxx') {

$sg_api_key = "Enter Your SenGrid Password";
} else {
$sg_api_key = $_POST['dpassword'];
}
$sendgrid = new sendgridNewsletter($sg_user, $sg_api_key);
$contact1['Tags'] = $sendgrid->newsletter_lists_get($list = '');
foreach ($contact1['Tags'] as $value) {
$contact2['Tags'] = $sendgrid->newsletter_lists_email_get($value['list'], '');
$data[$value['list']] = $contact2['Tags'];
}
echo "<div id="login">";
$data['contact1'] = $contact1['Tags'];
echo " <h2>Contact Lists</h2><hr/>";?>
<a href = "csvdownload.php?<?php echo http_build_query($data); ?>" ><img src = "image/csv.gif" alt = "" style = "float:right; margin: -86px 0px 0px 0px;" />
</a >
<?php echo "<table>";
foreach ($contact1['Tags'] as $value) {
echo "<tr><th>";
echo "Recipient Lists: " . $value['list'];
echo "</th></tr>";
echo "<tr><th>";
echo "Email Addresses";
echo "</th></tr>";

$contact2['Tags'] = $sendgrid->newsletter_lists_email_get($value['list'], '');
foreach ($contact2['Tags'] as $value) {
echo "<tr><td>";
$b = $value['email'];
echo $value['email'];
echo "</td></tr>";
}
}
echo "</table>";
echo "</div>";
}
?>
</div>
</div>
</div>
</body>
</html>

Step 5:- Create csvdownload.php into your root folder and copy the code present under the csvdownload.php section.

csvdownload.php

This code will download the SendGrid account contacts in excel sheet which we are getting in the form of array.



<?php
error_reporting(0);
if (isset($_GET)) {
$data =($_GET);
header("Content-type: text/csv");
header("Content-Disposition: attachment; filename=Contacts.csv");
header("Pragma: no-cache");
header("Expires: 0");
$file = fopen('php://output', 'w');
fputcsv($file, array('List','Email'));
foreach ($data as $value) {
if(key($data) != 'contact1'){
$list = key($data);
foreach ($value as $index){
foreach($index as $email){
$a = $index['email'];
}
fputcsv($file,array($list,$a));
}
next($data);
}
}
exit();
}
?>

Step 6:- Create style.css into your css folder created within the root folder. Once you finished this task just copy css code and paste it into style.css.

style.css

This make front page attractive and look good.



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

.error{
color: #FF0000;
}

body{
//background-color:#D8C092;
//margin:100px,50px;
//padding:100px,50px;
}

#main{margin:50px auto;
font-family:raleway;
}

span{
color:red;
}

#main h1{
text-align:center;
align:center;
word-spacing:5px;
}

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:50%;
margin:0 auto;
//display:inline-block;
border-radius: 10px;
font-family:raleway;
border: 2px solid #ccc;
padding: 10px 40px 25px;
}

input[type=text],input[type=password],input[type=email]{
width:99.5%;
padding: 10px;
margin-top: 8px;
border: 1px solid #ccc;
padding-left: 5px;
font-size: 16px;
font-family:raleway;
}

#textarea{
width:99.5%;
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;
}

#profile{
padding:50px;
border:1px dashed grey;
font-size:20px;
background-color:#DCE6F7;
}

#logout{
float:right;
padding:5px;
border:dashed 1px gray;
}

a{
text-decoration:none;
color: cornflowerblue;
}

i{
color: cornflowerblue;
}

#note{
clear: left;
padding-top: 20px;
margin-left: 20px;
font-size: 18px;
text-align: center;
}

#sidebar{
float:left;
}

#content{
alignment-adjust: central;
margin:0 auto;
text-align: left;
}

Step 7:- Create table.css into your css folder created within the root folder. Once you finished this task just copy css code and paste it into table.css.

table.css



table a:link {
color: #666;
font-weight: bold;
text-decoration:none;
}
table a:visited {
color: #999999;
font-weight:bold;
text-decoration:none;
}
table a:active,
table a:hover {
color: #bd5a35;
text-decoration:underline;
}
table {
font-family: 'Raleway', sans-serif;
//color:#666;
font-size:12px;
text-shadow: 1px 1px 0px #fff;
background:#eaebec;
//margin:20px;
border:#ccc 1px solid;
font-size: 16px;
-moz-border-radius:3px;
-webkit-border-radius:3px;
border-radius:3px;

-moz-box-shadow: 0 1px 2px #d1d1d1;
-webkit-box-shadow: 0 1px 2px #d1d1d1;
box-shadow: 0 1px 2px #d1d1d1;
margin: 50px 50px;
width: 85%;

}
table th {
padding:21px 25px 22px 25px;
border-top:1px solid #fafafa;
border-bottom:1px solid #e0e0e0;

background: #ededed;
background: -webkit-gradient(linear, left top, left bottom, from(#ededed), to(#ebebeb));
background: -moz-linear-gradient(top, #ededed, #ebebeb);
}
table th:first-child {
text-indent: center;
padding-left:20px;
}
table tr:first-child th:first-child {
-moz-border-radius-topleft:3px;
-webkit-border-top-left-radius:3px;
border-top-left-radius:3px;
}
table tr:first-child th:last-child {
-moz-border-radius-topright:3px;
-webkit-border-top-right-radius:3px;
border-top-right-radius:3px;
}
table tr {
text-indent: center;
padding-left:20px;
}
table td:first-child {
text-align: center;
padding-left:20px;
border-left: 0;
}
table td {
padding:18px;
border-top: 1px solid #ffffff;
border-bottom:1px solid #e0e0e0;
border-left: 1px solid #e0e0e0;

background: #fafafa;
background: -webkit-gradient(linear, left top, left bottom, from(#fbfbfb), to(#fafafa));
background: -moz-linear-gradient(top, #fbfbfb, #fafafa);
}
table tr.even td {
background: #f6f6f6;
background: -webkit-gradient(linear, left top, left bottom, from(#f8f8f8), to(#f6f6f6));
background: -moz-linear-gradient(top, #f8f8f8, #f6f6f6);
}
table tr:last-child td {
//border-bottom:0;
}
table tr:last-child td:first-child {
-moz-border-radius-bottomleft:3px;
-webkit-border-bottom-left-radius:3px;
border-bottom-left-radius:3px;
}
table tr:last-child td:last-child {
-moz-border-radius-bottomright:3px;
-webkit-border-bottom-right-radius:3px;
border-bottom-right-radius:3px;
}
table tr:hover td {
background: #f2f2f2;
background: -webkit-gradient(linear, left top, left bottom, from(#f2f2f2), to(#f0f0f0));
background: -moz-linear-gradient(top, #f2f2f2, #f0f0f0);
}
thead {
border-top: 2px solid #E8E8E8;
}

Conclusion :

Thus using SendGrid’s Marketing Email API you can export the contacts and download it. Hope you like it, keep reading our other blogs and share your experience in the space given below 🙂

Get more related information here –