PHP Classes

File: testdriver1.php

Recommend this page to a friend!
  Classes of Chris Fortune  >  Bounce Handler  >  testdriver1.php  >  Download  
File: testdriver1.php
Role: Example script
Content type: text/plain
Description: test the code and read the documentation
Class: Bounce Handler
Parse bounced e-mail message reports
Author: By
Last change: you may need to update your code to instantiate and use the class methods, if you were using an previous version.
Date: 6 years ago
Size: 10,559 bytes


Class file image Download
<h1>bounce_driver.class.php -- Version 6</h1>
    Chris Fortune ~ <a href=""></a>
Feb 24, 2001
Hey! Class is no longer static, it is rewritten in dynamic <code>$this-></code> notation.  It's much easier to customize now.  If you are upgrading from a previous version, you will need to rewrite your method invocation code.
<a href="" onclick="alert('not ready just yet'); return false;">Download source code</a>

This bounce handler Attempts to parse Multipart reports for hard bounces, according to <a href=''>RFC1892</a> (RFC 1892 - The Multipart/Report Content Type for the Reporting of Mail System Administrative Messages) and <a href=''>RFC1894</a> (RFC 1894 - An Extensible Message Format for Delivery Status Notifications).  We can reuse this for any well-formed bounces. </P>
It handles FBL (Feedback Loop) emails, if they are in Abuse Feedback Reporting Format, <a href="">ARF</a>      (It even handles Hotmail's ridiculous attempts at FBL).  DKIM parsing is not yet implemented.
You can configure custom regular expressions to find any web beacons you may have put in your outgoing mails, in either the mail body or an x-header field.  (see source code for examples).  You can use it to track data (eg, recipient, list, mail run, etc...) by sending out unique ids, then parsing them from the bounces.  This is especially useful when parsing FBL's, because usually all recipient fields have been removed (redacted).
If the bounce is not well formed, it tries to extract some useful information anyway.  Currently Postfix and Exim are supported, partially.  You can edit the function <code>get_the_facts()</code> if you want to add a parser for your own busted MTA.  Please forward any useful & reuseable code to the keeper of this class.  <a href="">Chris Fortune</a></P>
$bouncehandler = new Bouncehandler();

// a perl regular expression to find a web beacon in the email body
$bouncehandler->web_beacon_preg_1 = "/u=([0-9a-fA-F]{32})/";
$bouncehandler->web_beacon_preg_2 = "/m=(\d*)/";

// find a web beacon in an X-header field (in the head section)
$bouncehandler->x_header_search_1 = "X-ctnlist-suid";
//$bouncehandler->x_header_search_2 = "X-sumthin-sumpin";

    echo "<HR><P><B>".$_GET['eml']."</B>  --  ";
    echo "<a href=\"testdriver1.php\">View a different bounce</a></P>";
    $bounce = file_get_contents("eml/".$_GET['eml']);
    echo "<P>Quick and dirty bounce handler:<BR>
        \$bouncehandler = new Bouncehandler();<br>
        \$multiArray = \$bouncehandler->get_the_facts(\$strEmail);  , or<br>
        \$multiArray = \$bouncehandler->parse_email(\$strEmail);</code>  (same thing)
        returns a multi-dimensional associative array of bounced recipient addresses and their SMTP status codes (if available)<br><br>

    $multiArray = $bouncehandler->get_the_facts($bounce);
    echo "<TEXTAREA COLS=100 ROWS=".(count($multiArray)*8).">";
//print_r($bouncehandler); exit;

    echo "</TEXTAREA>";

    $bounce = $bouncehandler->init_bouncehandler($bounce, 'string');
    list($head, $body) = preg_split("/\r\n\r\n/", $bounce, 2);
    print "select a bounce email to view the parse";
    if ($handle = opendir('eml')) {
       echo "<P>Files:</P>\n";

       /* This is the correct way to loop over the directory. */
       while (false !== ($file = readdir($handle))) {
           if($file=='.' || $file=='..') continue;
           echo "<a href=\"".$_SERVER['PHP_SELF']."?eml=".urlencode($file)."\">$file</a><br>\n";


echo "<P>Will return recipient's email address, the RFC1893 error code, and the action.  Action can be one of the following:
<LI>'transient'(temporary problem),
<LI>'failed' (permanent problem),
<LI>'autoreply' (a vacation auto-response), or
<LI>'' (nothing -- not classified).</UL>";

echo "<P>You could dereference the \$multiArray in a 'for loop', for example...</P>
    foreach(\$multiArray as \$the){
            case 'failed':
                //do something
            case 'transient':
                //do something else
                \$num_attempts  = delivery_attempts(\$the['recipient']);
                if(\$num_attempts  > 10){
                    insert_into_queue(\$the['recipient'], (\$num_attempts+1));
            case 'autoreply':
                //do something different
                postpone(\$the['recipient'], '7 days');
                //don't do anything

Or, if it is an FBL, you could use the class variables to access the
data (Unlike Multipart-reports, FBL's report only one bounce)
You could also use them if the output array has only one index:

    if(\$bouncehandler->type == 'fbl'
       or count(\$bouncehandler->output) == 1)
        echo \$bouncehandler->action : .... <i>$bouncehandler->action</i>
        echo \$bouncehandler->status : .... <i>$bouncehandler->status</i>
        echo \$bouncehandler->recipient : .... <i>$bouncehandler->recipient</i>
        echo \$bouncehandler->feedback_type : .... <i>$bouncehandler->feedback_type</i>

These class variables are safe for all bounce types:

    echo \$bouncehandler->type : .... <i>$bouncehandler->type</i>
    echo \$bouncehandler->subject : .... <i>$bouncehandler->subject</i>
    echo \$bouncehandler->web_beacon_1 : .... <i>$bouncehandler->web_beacon_1</i>
    echo \$bouncehandler->web_beacon_2 : .... <i>$bouncehandler->web_beacon_2</i>
    echo \$bouncehandler->x_header_beacon_1 : .... <i>$bouncehandler->x_header_beacon_1</i>
    echo \$bouncehandler->x_header_beacon_2 : .... <i>$bouncehandler->x_header_beacon_2</i>

echo "<P>That's all you need to know, but if you want to get more complicated you can.  All methods are public.  See source code.</P><BR>";

echo "<hr><h2>Here is the parsed head</h2>\n";
$head_hash = $bouncehandler->parse_head($head);
echo "<TEXTAREA COLS=100 ROWS=".(count($head_hash)*2.7).">";
echo "</TEXTAREA>";

if ($bouncehandler->is_RFC1892_multipart_report($head_hash) === TRUE){
    print "<h2><font color=red>Looks like an RFC1892 multipart report</font></H2>";
else if($bouncehandler->looks_like_an_FBL === TRUE){
    print "<h2><font color=red>It's a Feedback Loop, ";
        print " in Hotmail Doofus Format (HDF?)</font></H2>";
        print " in Abuse Feedback Reporting Format (ARF)</font></H2>";
else {
    print "<h2><font color=red>Not an RFC1892 multipart report</font></H2>";
    echo "<TEXTAREA COLS=100 ROWS=100>";
    echo "</TEXTAREA>";

echo "<h2>Here is the parsed report</h2>\n";
echo "<P>Postfix adds an appropriate X- header (X-Postfix-Sender:), so you do not need to create one via phpmailer.  RFC's call for an optional Original-recipient field, but mandatory Final-recipient field is a fair substitute.</P>";
$boundary = $head_hash['Content-type']['boundary'];
$mime_sections = $bouncehandler->parse_body_into_mime_sections($body, $boundary);
$rpt_hash = $bouncehandler->parse_machine_parsable_body_part($mime_sections['machine_parsable_body_part']);
echo "<TEXTAREA COLS=100 ROWS=".(count($rpt_hash)*16).">";
echo "</TEXTAREA>";

echo "<h2>Here is the error status code</h2>\n";
echo "<P>It's all in the status code, if you can find one.</P>";
for($i=0; $i<count($rpt_hash['per_recipient']); $i++){
    echo "<P>Report #".($i+1)."<BR>\n";
    echo $bouncehandler->find_recipient($rpt_hash['per_recipient'][$i]);
    $scode = $rpt_hash['per_recipient'][$i]['Status'];
    echo "<PRE>$scode</PRE>";
    echo $bouncehandler->fetch_status_messages($scode);
    echo "</P>\n";

echo "<h2>The Diagnostic-code</h2> <P>is not the same as the reported status code, but it seems to be more descriptive, so it should be extracted (if possible).";
for($i=0; $i<count($rpt_hash['per_recipient']); $i++){
    echo "<P>Report #".($i+1)." <BR>\n";
    echo $bouncehandler->find_recipient($rpt_hash['per_recipient'][$i]);
    $dcode = $rpt_hash['per_recipient'][$i]['Diagnostic-code']['text'];
        echo "<PRE>$dcode</PRE>";
        echo $bouncehandler->fetch_status_messages($dcode);
        echo "<PRE>couldn't decode</PRE>";
    echo "</P>\n";

echo "<H2>Grab original To: and From:</H2>\n";
echo "<P>Just in case we don't have an Original-recipient: field, or a X-Postfix-Sender: field, we can retrieve information from the (optional) returned message body part</P>\n";
$head = $bouncehandler->get_head_from_returned_message_body_part($mime_sections);
echo "<P>From: ".$head['From']."<br>To: ".$head['To']."<br>Subject: ".$head['Subject']."</P>";

echo "<h2>Here is the body in RFC1892 parts</h2>\n";
echo "<P>Three parts: [first_body_part], [machine_parsable_body_part], and [returned_message_body_part]</P>";
echo "<TEXTAREA cols=100 rows=100>";
echo "</TEXTAREA>";

                $status_code = $bouncehandler->format_status_code($rpt_hash['per_recipient'][$i]['Status']);
                $status_code_msg = $bouncehandler->fetch_status_messages($status_code['code']);
                $status_code_remote_msg = $status_code['text'];
                $diag_code = $bouncehandler->format_status_code($rpt_hash['per_recipient'][$i]['Diagnostic-code']['text']);
                $diag_code_msg = $bouncehandler->fetch_status_messages($diag_code['code']);
                $diag_code_remote_msg = $diag_code['text'];