This tutorial is all about how we can fetch email contacts from Postmark API. Let me tell you first that this tutorial is not for beginners. If you don’t  know how to configure Postmark API in your PHP program, then follow this link  Send Email via PostMark API using PHP.

I hope you learn well how to configure postmark API in your app and send mail’s through it.

Now, in this tutorial we first configure postmark API in our program. Then we fetch contacts from postmark. Also, you can download all postmark contact in .xls file by clicking on download button.


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

export-postmark-contacts-using-php-content


MailGet  provides you a well managed email list management and allow you to Export PostMark’s Email List. Just connect and send with PostMark API or Any SMTP services.

Steps to fetch Email Contacts from Postmark

  • Configure your program with Postmark API.  If you don’t know how to configure Postmark API then follow this post  Send Email via PostMark API using PHP .
  • The Postmark does not provide any contact management, when user send email through postmark it will save contacts in their DB.
  • Here we are fetching those contacts.

Tutorial Scripts in detail

Below are the details of the code used in this tutorial with proper explanation.

we are creating object of postmark API :

   $client = new PostmarkClient("$apitoken"); 

we call getOutboundMessages() method through postark API object .  and receives an object which contains all contacts and messages :

 $allmessages = $client->getOutboundMessages(); 

Through our logic we fetched unique contacts from that object :



$ar = $allmessages;
$final = array();
$m = 1;
$x = 1;
foreach ($ar as $total_mail_address) {
break;
}
foreach ($ar as $data) {
if ($x == 2) {
for ($i = 0; $i < $total_mail_address; $i++) {
$len_of_dup_emails = count($data[$i]['Recipients']);
for ($j = 0; $j < $len_of_dup_emails; $j++) {
$emailid = $data[$i]['Recipients'][$j];
$finalarrlength = count($final);
for ($outer = 0; $outer < $finalarrlength; $outer++) {
$var1 = $final[$outer];
$var2 = $emailid;
if (strtolower($var1) == strtolower($var2)) {
break;
}
}
if ($outer == $finalarrlength) {
$final[$finalarrlength] = $emailid;
}
$m++;
}
}
}
$x++;
}  

saving contacts in session variable

 $_SESSION['email_address']=$final; 

when user clicks on download button . request goes to download.php and it will then print session data in .xls file

index.php

Below file is the main program file in which , we are connecting our API in the program and accessing its features like – creating session ,  creating object of postmark Client , fetching email address from Postmark API ,  and save data to session and if download button click then send session data to download.php.

<?php
session_start();
$_SESSION['email_address']=0;
require_once('./vendor/autoload.php');
use Postmark\PostmarkClient;
use Postmark\Models\PostmarkException;
?>
<html>
<head>
<title>
Export Postmark Contacts Using PHP
</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="js/jquery-1.11.3.min.js" type="text/javascript"></script>
<script src="js/bootstrap.js" type="text/javascript"></script>
<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 type="text/javascript">
$(document).ready(function() {
$("a#demo_key").click(function() {
var key = "xxxxxxxxxxx";
$("input#demo_postmark_key").val(key);
});
});
</script>
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-12">
<div id="main">
<h1>Export Postmark Contacts Using PHP</h1>
</div>
</div>
<div class="col-md-12">
<div class="matter">
<div id="login">
<h2>Get Contacts</h2>
<hr/>
<form action="index.php" method="post">
<label class="lab">Enter POSTMARK Server API token</label>
<?php
if (isset($_POST['submit'])) {
$password = $_POST['password'];
echo "<input id='demo_postmark_key' type='text' name='password' value='$password' required/>";
} else {
echo " <input id='demo_postmark_key' type='text' name='password' required/>";
}
?>
<a id="demo_key" href="#">Use Demo Server Key</a>
<input type="submit" value="Submit" name="submit"/><br />
<span></span>
</form>
</div>
</div>
</div>
</div>
<!-- Right side div -->
<?php
if (isset($_POST['submit'])) {
$password = $_POST['password'];
if ($password == "xxxxxxxxxxx") {
$apitoken = "enter your  POSTMARK Server API token";
} else {
$apitoken = $_POST['password'];
}
try {
$client = new PostmarkClient("$apitoken");
$allmessages = $client->getOutboundMessages();
$ar = $allmessages;
$final = array();
$m = 1;
$x = 1;
foreach ($ar as $total_mail_address) {
break;
}
foreach ($ar as $data) {
if ($x == 2) {
for ($i = 0; $i < $total_mail_address; $i++) {
$len_of_dup_emails = count($data[$i]['Recipients']);
for ($j = 0; $j < $len_of_dup_emails; $j++) {
$emailid = $data[$i]['Recipients'][$j];
$finalarrlength = count($final);
for ($outer = 0; $outer < $finalarrlength; $outer++) {
$var1 = $final[$outer];
$var2 = $emailid;
if (strtolower($var1) == strtolower($var2)) {
break;
}
}
if ($outer == $finalarrlength) {
$final[$finalarrlength] = $emailid;
}
$m++;
}
}
}
$x++;
}
$_SESSION['email_address']=$final;
?>
<center> <h3> Email Address List</h3></center>
<?php
echo "<div style='width:40%; margin:0 auto; '>";
echo "<form action='download.php' method='post' >";
echo "<input type='password' name='password' value='$password' hidden>";
echo "<input type='text' name='download' value='' hidden>";
echo "<input type='submit' value='Download Excel' name='submit'>";
echo "</form>";
echo "</div>";
?>
<table class="bouncetable">
<thead>
<td id="name">S.No</td>
<td>Email</td>
</thead>
<?php
for($i=0;$i<count($final);$i++){
$sno=$i+1;
echo "<tr><td>$sno</td>";
echo "<td>$final[$i]</td></tr>";
}
?>
</table>
<?php
} catch (PostmarkException $ex) {
// If client is able to communicate with the API in a timely fashion,
// but the message data is invalid, or there's a server error,
// a PostmarkException can be thrown.
echo "<script>alert('invalid api token ');</script>";
} catch (Exception $generalException) {
echo "<script>alert('ERROR ');</script>";
}
}
?>
</div>
</body>
</html>

download.php

index.php file will store contacts in session variable now , when user clicks the download button . download.php gets call  and fetch session data and save that data in .xls file to the user .

 <?php
session_start();
if(isset($_SESSION['email_address'])){
$final=$_SESSION['email_address'];
$x = 1;
foreach ($final as $email) {
$data[$x] = array('S.NO.' => "$x", 'Email-Address' => "$email");
$x++;
}
$filename = "EMAIL-ADDRESS" . ".xls";
header("Content-Type: application/vnd.ms-excel");
header("Content-Disposition: attachment; filename=\"$filename\"");
$heading = false;
if (!empty($data))
foreach ($data as $row) {
if (!$heading) {
// display field/column names as a first row
echo implode("\t", array_keys($row)) . "\n";
$heading = true;
}
echo implode("\t", array_values($row)) . "\n";
}
exit;

}
?>

style.css

The below file contains all the basic styling of page .

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

#main{
margin:50px auto;
font-family:raleway;
}
span{
color:red;
}
h2{
font-weight: 600;
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:40%;;
margin:0 auto;
display:inline-block;
border-radius: 10px;
font-family:raleway;
border: 2px solid #ccc;
padding: 10px 40px 25px;
}
textarea{
margin-top: 8px;
font-size: 16px;
font-family:raleway;
}
input[type=radio]{
margin-top: 8px;
}
input[type=text],[type=email],input[type=password]{
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;
}
#formget{
float:right;
}
h1{
font-weight: 600;
text-align: center;
display:inlne;
alignment-adjust: center;
margin:0 auto;
width:100%;
}
textarea[type=text]{
width:100%;
height:200px;
}
.matter{
alignment-adjust: central;
margin:0 auto;
text-align: center;
}
.clr{
clear:left;
}
.lab{
font-size: 110%;
float:left;
}
thead {
border-top: 3px solid #DEDEDE;
}
h3 {
margin-top: 52px !important;
}

table.css

The below file contais all the styling of table present in the program .



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;
font-size:12px;
text-shadow: 1px 1px 0px #fff;
background:#eaebec;
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;
border-radius:80px;
margin: 20px auto;
width:40%;
}
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: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);
}

Conclusion :

That’s all about how to fetch email addresses of postmark API from your PHP web app. I am sure you will try this script and add it in your few programs too. Feel free to visit our site for more interesting tutorials . you can give your feedback in the space provided below .