Those email which fails to reach  their destination is known as Bounced  emails. The sender gets error messages for those bounce emails. The reason for those Bounced emails are as follows:

Get Complete Detail of Hard and Soft Bounce Emails

  • Typing mistake: wrong address is given in the place for Recipient’s Address.
  • Technical Error: may be the recipient mailbox is full or the size of your message  is greater than the recipients max limit.
  • Security Error: The sender email is blocked or not comes in the preferred list and hence it returned back by the receiver’s mail server.

In this blog, we are going to tell you that how can you get List of  bounced email via Postmark API. before putting next step , If you  are new to Postmark API  then  first follow this post  –> Send Email via Postmark API Using PHP and  in that post you will learn how to configure postmark API in PHP and send mail through it .


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

Bounced Email List via PostMark API php


Use MailGet for Finding Bounced Email List, just connect and send with PostMark API or Any SMTP services.

Concept behind the script

So , from the previous post ( Send Email via Postmark API Using PHP ), you already know how to send mail . now lets understand that how can we achieve success in getting information about bounced emails.

To, get detail of bounced email , we have to send some email to wrong addressess and we fetch their details through our program . For send mail from postmark API through your PHP program follow the previous post –> Send Email via Postmark API Using PHP .

now,  i hope you already send some email to false addressess  and we are going to learn how to fetch details about those bounced emails .

steps for getting bounced emails are as follows :-

 $client = new PostmarkClient("<Server API token>"); 
  •  call getBounce()  function through client . which return you an object  and you can fetch bounced emails list with its help .
 $bounces = $client->getBounces(); 

Tutorial Scripts in detail

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

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 object of postmark Client and fetchig details of bounced emails.

<?php
require_once('./vendor/autoload.php');

use Postmark\PostmarkClient;
use Postmark\Models\PostmarkException;
?>
<html>
<head>
<title>
Find Bounced Email List via PostMark API
</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>
<div class="row">
<div class="col-md-12">
<div id="main">
<h1>Find Bounced Email List via PostMark API</h1>
</div>
</div>
<div class="col-md-12">
<div class="matter">
<div id="login">
<h2>Get Bounced Email</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 = "Insert Server API token here";
} else {
$apitoken = $_POST['password'];
}
try {
$client = new PostmarkClient("$apitoken");
$bounces = $client->getBounces();
$ar = $bounces;
?>
<center> <h3>Bounced Email List</h3></center>

<table class="bouncetable">
<thead>
<td id="name">S.No</td>
<td>Subject</td>
<td>Email</td>
<td>Bounced Time</td>
</thead>
<?php
$count = 1;
$x = 1;
foreach ($ar as $numofbounce) {
break;
}
foreach ($ar as $data) {
if ($x == 2) {
for ($i = 0; $i < $numofbounce; $i++) {
?>
<tr>
<td><?php
echo $count;
$count++;
?></td>
<link href="style.css" rel="stylesheet" type="text/css"/>
<td>
<?php echo $data[$i]['Subject']; ?>
</td>
<td> <?php echo $data[$i]['Email']; ?></td>
<td> <?php echo $data[$i]['BouncedAt']; ?></td>
</tr>
<?php
}
}
$x++;
}
?>

</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 $ex->httpStatusCode;
// echo $ex->message;
// echo $ex->postmarkApiErrorCode;
echo "<script>alert('invalid api token ');</script>";
} catch (Exception $generalException) {
// echo "welcome";
echo "<script>alert('ERROR ');</script>";
}
}
?>

</div>
</body>
</html>

Style.css

Includes basic styling of HTML elements.


@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;
//margin-top: 70px;
}
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

A separate css style sheet for table used in index.php to show bounced emails list.

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 11%;
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);
}

Conclusion :

I hope  now you understand , how we can find bounced emails list through  Postmark API, I am sure you  will try this code is your program .keep in touch with  us anytime for new coding tricks . you can give your feedback in the below space .