PHP – handling bounced emails

After having searched for quite a while for an effective way to handle bounced mails when sending bulk mails I found this solution.

With Ze Cheesefather’s script being the core of the solution.

First of all, setup an email account that receives all the bounces from the sent bulk mail, e.g. no-reply@example.com.

Then:

  • Download the supplied zipfile (bottom of this post)
  • Unzip it
  • Make the necessary changes to the top of index.php
  • Upload the folder, and access the script via your web browser

Below is the code of index.php. Remember to upload the entire folder contained in the zipfile, after having made your configuration changes.

// Database connection parameters
define('DB_HOST', '--YOUR DATA HERE! --');
define('DB_USER', '--YOUR DATA HERE! --');
define('DB_PASS', '--YOUR DATA HERE! --');
define('DB_NAME', '--YOUR DATA HERE! --');
// In the below definitions: use [EMAIL] as a substitute for the actual email address in the query.
// E.g.: define('SQL_QUERY',"UPDATE customers SET subscribed = 0 WHERE email LIKE '[EMAIL]'");
define('SQL_QUERY', "--YOUR DATA HERE! --"); // Primary database operation
define('SQL_QUERY2',"--YOUR DATA HERE! --"); // Optional – additional database operation. Can be commented out.
// Define variables
$mailserver="SERVER ADDRESS, EG. example.com";
$port="143/imap"; // Mail protocol
$user="no-reply@example.com"; // The email address that receives the bounced mails
$pass="********"; // Password for the email account
$delete = '5'; // Deletes emails with at least this number of failures

/* NO NEED TO CHANGE ANYTHING BELOW THIS LINE */
$conn = imap_open("{".$mailserver.":".$port."}INBOX",$user,$pass);
$db_conn = mysql_connect(DB_HOST,DB_USER,DB_PASS);
mysql_select_db(DB_NAME,$db_conn);
$num_msgs = imap_num_msg($conn);
require_once('bounce_driver.class.php');
$bouncehandler = new Bouncehandler();

$email_addresses = array();
$delete_addresses = array();
for ($n = 1;$n <= $num_msgs;$n++) { $bounce = imap_fetchheader($conn,$n).imap_body($conn, $n); //entire message $multiArray = $bouncehandler->get_the_facts($bounce);
if(!empty($multiArray[0]['action']) && !empty($multiArray[0]['status']) && !empty($multiArray[0]['recipient'])) {
if ($multiArray[0]['action']=='failed') {
$email_addresses[$multiArray[0]['recipient']]++; //increment number of failures
$delete_addresses[$multiArray[0]['recipient']][] = $n; //add message to delete array
}
}
}
$counter = 0;
foreach($email_addresses as $key => $value) { // trim($key) is email address, $value is number of failures
if($value >= $delete) {
$counter++;
$query =str_replace("[EMAIL]",trim($key),SQL_QUERY);
// Uncomment next line for debugging purposes
// echo $query.'
';
if($res = mysql_query($query,$db_conn)) {
$query2 =str_replace("[EMAIL]",trim($key),SQL_QUERY2);
if(is_defined('SQL_QUERY2')) {
$query2 = str_replace("[EMAIL]",trim($key),SQL_QUERY2);
mysql_query($query2,$db_conn);
// Uncomment next line for debugging purposes
// echo $query2.'
';
}
}
// Mark mail for deletion
foreach ($delete_addresses[$key] as $delnum) imap_delete($conn, $delnum);
}
}
imap_expunge($conn);
imap_close($conn);
echo '

Action taken on '.$counter.' email addresses.

'

Download the files in zip-format