Example #1
0
 static function get_the_facts($eml)
 {
     // fluff up the email
     $bounce = BounceHandler::init_bouncehandler($eml);
     list($head, $body) = preg_split("/\r\n\r\n/", $bounce, 2);
     $head_hash = BounceHandler::parse_head($head);
     // initialize output variable
     $output[0]['recipient'] = "";
     $output[0]['status'] = "";
     $output[0]['action'] = "";
     // sanity check.
     if (!BounceHandler::is_a_bounce($head_hash)) {
         return $output;
     }
     // parse the email into data structures
     $boundary = $head_hash['Content-type']['boundary'];
     $mime_sections = BounceHandler::parse_body_into_mime_sections($body, $boundary);
     $arrBody = explode("\r\n", $body);
     // now we try all our weird text parsing methods
     if (preg_match("/auto.{0,20}reply|vacation|(out|away|on holiday).*office/i", $head_hash['Subject'])) {
         // looks like a vacation autoreply, ignoring
         $output[0]['action'] = 'autoreply';
     } else {
         if (BounceHandler::is_RFC1892_multipart_report($head_hash) === TRUE) {
             $rpt_hash = BounceHandler::parse_machine_parsable_body_part($mime_sections['machine_parsable_body_part']);
             for ($i = 0; $i < count($rpt_hash['per_recipient']); $i++) {
                 $output[$i]['recipient'] = BounceHandler::get_recipient($rpt_hash['per_recipient'][$i]);
                 $output[$i]['status'] = $rpt_hash['per_recipient'][$i]['Status'];
                 $output[$i]['action'] = $rpt_hash['per_recipient'][$i]['Action'];
             }
         } else {
             if (isset($head_hash['X-failed-recipients'])) {
                 //  Busted Exim MTA
                 //  Up to 50 email addresses can be listed on each header.
                 //  There can be multiple X-Failed-Recipients: headers. - (not supported)
                 $arrFailed = explode(',', $head_hash['X-failed-recipients']);
                 for ($j = 0; $j < count($arrFailed); $j++) {
                     $output[$j]['recipient'] = trim($arrFailed[$j]);
                     $output[$j]['status'] = BounceHandler::get_status_code_from_text($output[$j]['recipient'], $arrBody, 0);
                     $output[$j]['action'] = BounceHandler::get_action_from_status_code($output[$j]['status']);
                 }
             } else {
                 if (!empty($boundary) && BounceHandler::is_a_bounce($head_hash)) {
                     // oh god it could be anything, but at least it has mime parts, so let's try anyway
                     $arrFailed = BounceHandler::find_email_addresses($mime_sections['first_body_part']);
                     for ($j = 0; $j < count($arrFailed); $j++) {
                         $output[$j]['recipient'] = trim($arrFailed[$j]);
                         $output[$j]['status'] = BounceHandler::get_status_code_from_text($output[$j]['recipient'], $arrBody, 0);
                         $output[$j]['action'] = BounceHandler::get_action_from_status_code($output[$j]['status']);
                     }
                 } else {
                     if (BounceHandler::is_a_bounce($head_hash)) {
                         // last ditch attempt
                         // could possibly produce erroneous output, or be very resource consuming,
                         // so be careful.  You should comment out this section if you are very concerned
                         // about 100% accuracy or if you want very fast performance.
                         // Leave it turned on if you know that all messages to be analyzed are bounces.
                         $arrFailed = BounceHandler::find_email_addresses($body);
                         for ($j = 0; $j < count($arrFailed); $j++) {
                             $output[$j]['recipient'] = trim($arrFailed[$j]);
                             $output[$j]['status'] = BounceHandler::get_status_code_from_text($output[$j]['recipient'], $arrBody, 0);
                             $output[$j]['action'] = BounceHandler::get_action_from_status_code($output[$j]['status']);
                         }
                     }
                 }
             }
         }
     }
     // else if()..... add a parser for your busted-ass MTA here
     return $output;
 }