PageRenderTime 61ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

/e107_handlers/bounce_handler.php

https://github.com/CasperGemini/e107
PHP | 837 lines | 599 code | 139 blank | 99 comment | 177 complexity | 9313cb34030423e015501d8c91becece MD5 | raw file
Possible License(s): GPL-2.0
  1. #!/usr/bin/php -q
  2. <?php
  3. // WARNING, any echoed output from this script will be returned to the sender as a bounce message.
  4. $_E107['debug'] = FALSE;
  5. if (!defined('e107_INIT'))
  6. {
  7. $_E107['cli'] = TRUE;
  8. $class2 = realpath(dirname(__FILE__)."/../")."/class2.php";
  9. require_once($class2);
  10. }
  11. $bnc = new e107Bounce;
  12. $process = (varset($_GET['eml']) && $_E107['debug']) ? $_GET['eml'].".eml" : FALSE;
  13. $bnc->process($process);
  14. class e107Bounce
  15. {
  16. function process($source='')
  17. {
  18. global $_E107,$pref;
  19. e107::getCache()->CachePageMD5 = '_';
  20. e107::getCache()->set('emailLastBounce',time(),TRUE,FALSE,TRUE);
  21. $strEmail= (!$source) ? $this->mailRead(-1) : file_get_contents(e_HANDLER."eml/".$source);
  22. if(!$strEmail)
  23. {
  24. return;
  25. }
  26. $multiArray = Bouncehandler::get_the_facts($strEmail);
  27. $head = BounceHandler::parse_head($strEmail);
  28. $e107_userid = (isset($head['X-e107-id'])) ? intval($head['X-e107-id']) : $this->getHeader($strEmail,'X-e107-id');
  29. if($_E107['debug'])
  30. {
  31. require_once(e_HANDLER."mail.php");
  32. $message = "Your Bounce Handler is working. The data of the email you sent is displayed below.<br />";
  33. if($e107_userid)
  34. {
  35. $message .= "A user-id was detected in the email you sent: <b>".$e107_userid."</b><br />";
  36. }
  37. $message .= "<br />";
  38. $message .= "<pre>".print_r($multiArray,TRUE). "</pre>";
  39. $message .= "<pre>".$strEmail. "</pre>";
  40. sendemail($pref['siteadminemail'], SITENAME." :: Bounce-Handler.", $message, $pref['siteadmin'],$pref['siteadminemail'], $pref['siteadmin']);
  41. }
  42. if($e107_userid && ($this->setUser_Bounced($e107_userid)==TRUE))
  43. {
  44. return;
  45. }
  46. /* echo "<pre>";
  47. print_r($multiArray);
  48. echo "</pre>";
  49. */
  50. foreach($multiArray as $the)
  51. {
  52. $the['user_id'] = $head['X-e107-id'];
  53. $the['user_email'] = $the['recipient'];
  54. unset($the['recipient']);
  55. switch($the['action'])
  56. {
  57. case 'failed':
  58. e107::getEvent()->trigger('email-bounce-failed', $the);
  59. $this->setUser_Bounced($the['user_email']);
  60. break;
  61. case 'transient':
  62. // $num_attempts = delivery_attempts($the['user_email']);
  63. e107::getEvent()->trigger('email-bounce-transient', $the);
  64. if($num_attempts > 10)
  65. {
  66. $this->setUser_Bounced($the['user_email'], $the['user_id']);
  67. }
  68. else
  69. {
  70. // insert_into_queue($the['user_email'], ($num_attempts+1));
  71. }
  72. break;
  73. case 'autoreply':
  74. e107::getEvent()->trigger('email-bounce-autoreply', $the);
  75. // postpone($the['user_email'], '7 days');
  76. break;
  77. default:
  78. //don't do anything
  79. break;
  80. }
  81. }
  82. }
  83. function getHeader($message, $id='X-e107-id')
  84. {
  85. $tmp = explode("\n",$message);
  86. foreach($tmp as $val)
  87. {
  88. if(strpos($val,$id.":")!==FALSE)
  89. {
  90. return intval(str_replace($id.":","",$val));
  91. }
  92. }
  93. }
  94. function setUser_Bounced($email, $bounceString = '')
  95. {
  96. if(!$email && !$bounceString){ return; }
  97. // echo "Email bounced ID: ".$id_or_email;
  98. require_once(e_HANDLER.'mail_manager_class.php');
  99. $mailHandler = new e107MailManager();
  100. if ($mailManager->markBounce($bounceString, $email))
  101. { // Success
  102. }
  103. // Failure
  104. // $query = (is_numeric($id_or_email)) ? "user_ban = 3 WHERE user_id = ".intval($id_or_email)." LIMIT 1" : "user_ban = 3 WHERE user_email = '".$id_or_email."' ";
  105. // return e107::getDb()->db_Update('user',$query);
  106. }
  107. /**
  108. * Read the Mail.
  109. * @param object $iKlimit [optional]
  110. * @return string mail message
  111. */
  112. function mailRead($iKlimit = 4096)
  113. {
  114. $sErrorSTDINFail = "Error - failed to read mail from STDIN!";
  115. $fp = fopen("php://stdin", "r");
  116. if (!$fp)
  117. {
  118. mail("adminaddress@something.com","Bounce-Processing - Bounce-Processing",$sErrorSTDINFail,$headers);
  119. exit();
  120. }
  121. // Create empty string for storing message
  122. $sEmail = "";
  123. if ($iKlimit == -1) {
  124. while (!feof($fp)) {
  125. $sEmail .= fread($fp, 1024);
  126. }
  127. } else {
  128. while (!feof($fp) && $i_limit < $iKlimit) {
  129. $sEmail .= fread($fp, 1024);
  130. $i_limit++;
  131. }
  132. }
  133. fclose($fp);
  134. return $sEmail;
  135. }
  136. }
  137. //error_reporting(E_ALL);
  138. /* BOUNCE HANDLER Class, Version 5.1
  139. * Description: "chops up the bounce into associative arrays"
  140. * ~ http://www.phpclasses.org/browse/file/11665.html
  141. */
  142. /* Debugging / Contributers:
  143. * "Kanon"
  144. * Jamie McClelland http://mayfirst.org
  145. * Michael Cooper
  146. * Thomas Seifert
  147. * Tim Petrowsky http://neuecouch.de
  148. * Willy T. Koch http://apeland.no
  149. */
  150. /*
  151. The BSD License
  152. Copyright (c) 2006, Chris Fortune http://cfortune.kics.bc.ca
  153. All rights reserved.
  154. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
  155. * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  156. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
  157. * Neither the name of the BounceHandler nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
  158. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  159. */
  160. class BounceHandler{
  161. var $status_code_classes;
  162. var $status_code_subclasses;
  163. function BounceHandler()
  164. {
  165. $this->status_code_classes['2']['title'] = "Success";
  166. $this->status_code_classes['2']['descr'] = "Success specifies that the DSN is reporting a positive delivery action. Detail sub-codes may provide notification of transformations required for delivery.";
  167. $this->status_code_classes['4']['title'] = "Persistent Transient Failure";
  168. $this->status_code_classes['4']['descr'] = "A persistent transient failure is one in which the message as sent is valid, but some temporary event prevents the successful sending of the message. Sending in the future may be successful.";
  169. $this->status_code_classes['5']['title'] = "Permanent Failure";
  170. $this->status_code_classes['5']['descr'] = "A permanent failure is one which is not likely to be resolved by resending the message in the current form. Some change to the message or the destination must be made for successful delivery.";
  171. $this->status_code_subclasses['0.0']['title'] = "Other undefined Status";
  172. $this->status_code_subclasses['0.0']['descr'] = "Other undefined status is the only undefined error code. It should be used for all errors for which only the class of the error is known.";
  173. $this->status_code_subclasses['1.0']['title'] = "Other address status";
  174. $this->status_code_subclasses['0.0']['descr'] = "Something about the address specified in the message caused this DSN.";
  175. $this->status_code_subclasses['1.1']['title'] = "Bad destination mailbox address";
  176. $this->status_code_subclasses['1.1']['descr'] = "The mailbox specified in the address does not exist. For Internet mail names, this means the address portion to the left of the @ sign is invalid. This code is only useful for permanent failures.";
  177. $this->status_code_subclasses['1.2']['title'] = "Bad destination system address";
  178. $this->status_code_subclasses['1.2']['descr'] = "The destination system specified in the address does not exist or is incapable of accepting mail. For Internet mail names, this means the address portion to the right of the @ is invalid for mail. This codes is only useful for permanent failures.";
  179. $this->status_code_subclasses['1.3']['title'] = "Bad destination mailbox address syntax";
  180. $this->status_code_subclasses['1.3']['descr'] = "The destination address was syntactically invalid. This can apply to any field in the address. This code is only useful for permanent failures.";
  181. $this->status_code_subclasses['1.4']['title'] = "Destination mailbox address ambiguous";
  182. $this->status_code_subclasses['1.4']['descr'] = "The mailbox address as specified matches one or more recipients on the destination system. This may result if a heuristic address mapping algorithm is used to map the specified address to a local mailbox name.";
  183. $this->status_code_subclasses['1.5']['title'] = "Destination address valid";
  184. $this->status_code_subclasses['1.5']['descr'] = "This mailbox address as specified was valid. This status code should be used for positive delivery reports.";
  185. $this->status_code_subclasses['1.6']['title'] = "Destination mailbox has moved, No forwarding address";
  186. $this->status_code_subclasses['1.6']['descr'] = "The mailbox address provided was at one time valid, but mail is no longer being accepted for that address. This code is only useful for permanent failures.";
  187. $this->status_code_subclasses['1.7']['title'] = "Bad sender's mailbox address syntax";
  188. $this->status_code_subclasses['1.7']['descr'] = "The sender's address was syntactically invalid. This can apply to any field in the address.";
  189. $this->status_code_subclasses['1.8']['title'] = "Bad sender's system address";
  190. $this->status_code_subclasses['1.8']['descr'] = "The sender's system specified in the address does not exist or is incapable of accepting return mail. For domain names, this means the address portion to the right of the @ is invalid for mail. ";
  191. $this->status_code_subclasses['2.0']['title'] = "Other or undefined mailbox status";
  192. $this->status_code_subclasses['2.0']['descr'] = "The mailbox exists, but something about the destination mailbox has caused the sending of this DSN.";
  193. $this->status_code_subclasses['2.1']['title'] = "Mailbox disabled, not accepting messages";
  194. $this->status_code_subclasses['2.1']['descr'] = "The mailbox exists, but is not accepting messages. This may be a permanent error if the mailbox will never be re-enabled or a transient error if the mailbox is only temporarily disabled.";
  195. $this->status_code_subclasses['2.2']['title'] = "Mailbox full";
  196. $this->status_code_subclasses['2.2']['descr'] = "The mailbox is full because the user has exceeded a per-mailbox administrative quota or physical capacity. The general semantics implies that the recipient can delete messages to make more space available. This code should be used as a persistent transient failure.";
  197. $this->status_code_subclasses['2.3']['title'] = "Message length exceeds administrative limit";
  198. $this->status_code_subclasses['2.2']['descr'] = "A per-mailbox administrative message length limit has been exceeded. This status code should be used when the per-mailbox message length limit is less than the general system limit. This code should be used as a permanent failure.";
  199. $this->status_code_subclasses['2.4']['title'] = "Mailing list expansion problem";
  200. $this->status_code_subclasses['2.3']['descr'] = "The mailbox is a mailing list address and the mailing list was unable to be expanded. This code may represent a permanent failure or a persistent transient failure. ";
  201. $this->status_code_subclasses['3.0']['title'] = "Other or undefined mail system status";
  202. $this->status_code_subclasses['3.0']['descr'] = "The destination system exists and normally accepts mail, but something about the system has caused the generation of this DSN.";
  203. $this->status_code_subclasses['3.1']['title'] = "Mail system full";
  204. $this->status_code_subclasses['3.1']['descr'] = "Mail system storage has been exceeded. The general semantics imply that the individual recipient may not be able to delete material to make room for additional messages. This is useful only as a persistent transient error.";
  205. $this->status_code_subclasses['3.2']['title'] = "System not accepting network messages";
  206. $this->status_code_subclasses['3.2']['descr'] = "The host on which the mailbox is resident is not accepting messages. Examples of such conditions include an immanent shutdown, excessive load, or system maintenance. This is useful for both permanent and permanent transient errors. ";
  207. $this->status_code_subclasses['3.3']['title'] = "System not capable of selected features";
  208. $this->status_code_subclasses['3.3']['descr'] = "Selected features specified for the message are not supported by the destination system. This can occur in gateways when features from one domain cannot be mapped onto the supported feature in another.";
  209. $this->status_code_subclasses['3.4']['title'] = "Message too big for system";
  210. $this->status_code_subclasses['3.4']['descr'] = "The message is larger than per-message size limit. This limit may either be for physical or administrative reasons. This is useful only as a permanent error.";
  211. $this->status_code_subclasses['3.5']['title'] = "System incorrectly configured";
  212. $this->status_code_subclasses['3.5']['descr'] = "The system is not configured in a manner which will permit it to accept this message.";
  213. $this->status_code_subclasses['4.0']['title'] = "Other or undefined network or routing status";
  214. $this->status_code_subclasses['4.0']['descr'] = "Something went wrong with the networking, but it is not clear what the problem is, or the problem cannot be well expressed with any of the other provided detail codes.";
  215. $this->status_code_subclasses['4.1']['title'] = "No answer from host";
  216. $this->status_code_subclasses['4.1']['descr'] = "The outbound connection attempt was not answered, either because the remote system was busy, or otherwise unable to take a call. This is useful only as a persistent transient error.";
  217. $this->status_code_subclasses['4.2']['title'] = "Bad connection";
  218. $this->status_code_subclasses['4.2']['descr'] = "The outbound connection was established, but was otherwise unable to complete the message transaction, either because of time-out, or inadequate connection quality. This is useful only as a persistent transient error.";
  219. $this->status_code_subclasses['4.3']['title'] = "Directory server failure";
  220. $this->status_code_subclasses['4.3']['descr'] = "The network system was unable to forward the message, because a directory server was unavailable. This is useful only as a persistent transient error. The inability to connect to an Internet DNS server is one example of the directory server failure error. ";
  221. $this->status_code_subclasses['4.4']['title'] = "Unable to route";
  222. $this->status_code_subclasses['4.4']['descr'] = "The mail system was unable to determine the next hop for the message because the necessary routing information was unavailable from the directory server. This is useful for both permanent and persistent transient errors. A DNS lookup returning only an SOA (Start of Administration) record for a domain name is one example of the unable to route error.";
  223. $this->status_code_subclasses['4.5']['title'] = "Mail system congestion";
  224. $this->status_code_subclasses['4.5']['descr'] = "The mail system was unable to deliver the message because the mail system was congested. This is useful only as a persistent transient error.";
  225. $this->status_code_subclasses['4.6']['title'] = "Routing loop detected";
  226. $this->status_code_subclasses['4.6']['descr'] = "A routing loop caused the message to be forwarded too many times, either because of incorrect routing tables or a user forwarding loop. This is useful only as a persistent transient error.";
  227. $this->status_code_subclasses['4.7']['title'] = "Delivery time expired";
  228. $this->status_code_subclasses['4.7']['descr'] = "The message was considered too old by the rejecting system, either because it remained on that host too long or because the time-to-live value specified by the sender of the message was exceeded. If possible, the code for the actual problem found when delivery was attempted should be returned rather than this code. This is useful only as a persistent transient error.";
  229. $this->status_code_subclasses['5.0']['title'] = "Other or undefined protocol status";
  230. $this->status_code_subclasses['5.0']['descr'] = "Something was wrong with the protocol necessary to deliver the message to the next hop and the problem cannot be well expressed with any of the other provided detail codes.";
  231. $this->status_code_subclasses['5.1']['title'] = "Invalid command";
  232. $this->status_code_subclasses['5.1']['descr'] = "A mail transaction protocol command was issued which was either out of sequence or unsupported. This is useful only as a permanent error.";
  233. $this->status_code_subclasses['5.2']['title'] = "Syntax error";
  234. $this->status_code_subclasses['5.2']['descr'] = "A mail transaction protocol command was issued which could not be interpreted, either because the syntax was wrong or the command is unrecognized. This is useful only as a permanent error.";
  235. $this->status_code_subclasses['5.3']['title'] = "Too many recipients";
  236. $this->status_code_subclasses['5.3']['descr'] = "More recipients were specified for the message than could have been delivered by the protocol. This error should normally result in the segmentation of the message into two, the remainder of the recipients to be delivered on a subsequent delivery attempt. It is included in this list in the event that such segmentation is not possible.";
  237. $this->status_code_subclasses['5.4']['title'] = "Invalid command arguments";
  238. $this->status_code_subclasses['5.4']['descr'] = "A valid mail transaction protocol command was issued with invalid arguments, either because the arguments were out of range or represented unrecognized features. This is useful only as a permanent error. ";
  239. $this->status_code_subclasses['5.5']['title'] = "Wrong protocol version";
  240. $this->status_code_subclasses['5.5']['descr'] = "A protocol version mis-match existed which could not be automatically resolved by the communicating parties.";
  241. $this->status_code_subclasses['6.0']['title'] = "Other or undefined media error";
  242. $this->status_code_subclasses['6.0']['descr'] = "Something about the content of a message caused it to be considered undeliverable and the problem cannot be well expressed with any of the other provided detail codes. ";
  243. $this->status_code_subclasses['6.1']['title'] = "Media not supported";
  244. $this->status_code_subclasses['6.1']['descr'] = "The media of the message is not supported by either the delivery protocol or the next system in the forwarding path. This is useful only as a permanent error.";
  245. $this->status_code_subclasses['6.2']['title'] = "Conversion required and prohibited";
  246. $this->status_code_subclasses['6.2']['descr'] = "The content of the message must be converted before it can be delivered and such conversion is not permitted. Such prohibitions may be the expression of the sender in the message itself or the policy of the sending host.";
  247. $this->status_code_subclasses['6.3']['title'] = "Conversion required but not supported";
  248. $this->status_code_subclasses['6.3']['descr'] = "The message content must be converted to be forwarded but such conversion is not possible or is not practical by a host in the forwarding path. This condition may result when an ESMTP gateway supports 8bit transport but is not able to downgrade the message to 7 bit as required for the next hop.";
  249. $this->status_code_subclasses['6.4']['title'] = "Conversion with loss performed";
  250. $this->status_code_subclasses['6.4']['descr'] = "This is a warning sent to the sender when message delivery was successfully but when the delivery required a conversion in which some data was lost. This may also be a permanant error if the sender has indicated that conversion with loss is prohibited for the message.";
  251. $this->status_code_subclasses['6.5']['title'] = "Conversion Failed";
  252. $this->status_code_subclasses['6.5']['descr'] = "A conversion was required but was unsuccessful. This may be useful as a permanent or persistent temporary notification.";
  253. $this->status_code_subclasses['7.0']['title'] = "Other or undefined security status";
  254. $this->status_code_subclasses['7.0']['descr'] = "Something related to security caused the message to be returned, and the problem cannot be well expressed with any of the other provided detail codes. This status code may also be used when the condition cannot be further described because of security policies in force.";
  255. $this->status_code_subclasses['7.1']['title'] = "Delivery not authorized, message refused";
  256. $this->status_code_subclasses['7.1']['descr'] = "The sender is not authorized to send to the destination. This can be the result of per-host or per-recipient filtering. This memo does not discuss the merits of any such filtering, but provides a mechanism to report such. This is useful only as a permanent error.";
  257. $this->status_code_subclasses['7.2']['title'] = "Mailing list expansion prohibited";
  258. $this->status_code_subclasses['7.2']['descr'] = "The sender is not authorized to send a message to the intended mailing list. This is useful only as a permanent error.";
  259. $this->status_code_subclasses['7.3']['title'] = "Security conversion required but not possible";
  260. $this->status_code_subclasses['7.3']['descr'] = "A conversion from one secure messaging protocol to another was required for delivery and such conversion was not possible. This is useful only as a permanent error. ";
  261. $this->status_code_subclasses['7.4']['title'] = "Security features not supported";
  262. $this->status_code_subclasses['7.4']['descr'] = "A message contained security features such as secure authentication which could not be supported on the delivery protocol. This is useful only as a permanent error.";
  263. $this->status_code_subclasses['7.5']['title'] = "Cryptographic failure";
  264. $this->status_code_subclasses['7.5']['descr'] = "A transport system otherwise authorized to validate or decrypt a message in transport was unable to do so because necessary information such as key was not available or such information was invalid.";
  265. $this->status_code_subclasses['7.6']['title'] = "Cryptographic algorithm not supported";
  266. $this->status_code_subclasses['7.6']['descr'] = "A transport system otherwise authorized to validate or decrypt a message was unable to do so because the necessary algorithm was not supported. ";
  267. $this->status_code_subclasses['7.7']['title'] = "Message integrity failure";
  268. $this->status_code_subclasses['7.7']['descr'] = "A transport system otherwise authorized to validate a message was unable to do so because the message was corrupted or altered. This may be useful as a permanent, transient persistent, or successful delivery code.";
  269. }
  270. // this is the most commonly used public method
  271. // quick and dirty
  272. // useage: $multiArray = Bouncehandler::get_the_facts($strEmail);
  273. function get_the_facts($eml){
  274. // fluff up the email
  275. $bounce = BounceHandler::init_bouncehandler($eml);
  276. list($head, $body) = preg_split("/\r\n\r\n/", $bounce, 2);
  277. $head_hash = BounceHandler::parse_head($head);
  278. // initialize output variable
  279. $output[0]['recipient'] = "";
  280. $output[0]['status'] = "";
  281. $output[0]['action'] = "";
  282. // sanity check.
  283. if(!BounceHandler::is_a_bounce($head_hash)){
  284. return $output;
  285. }
  286. // parse the email into data structures
  287. $boundary = $head_hash['Content-type']['boundary'];
  288. $mime_sections = BounceHandler::parse_body_into_mime_sections($body, $boundary);
  289. $arrBody = explode("\r\n", $body);
  290. // now we try all our weird text parsing methods
  291. if (preg_match("/auto.{0,20}reply|vacation|(out|away|on holiday).*office/i", $head_hash['Subject'])){
  292. // looks like a vacation autoreply, ignoring
  293. $output[0]['action'] = 'autoreply';
  294. }
  295. else if (BounceHandler::is_RFC1892_multipart_report($head_hash) === TRUE){
  296. $rpt_hash = BounceHandler::parse_machine_parsable_body_part($mime_sections['machine_parsable_body_part']);
  297. for($i=0; $i<count($rpt_hash['per_recipient']); $i++){
  298. $output[$i]['recipient'] = BounceHandler::get_recipient($rpt_hash['per_recipient'][$i]);
  299. $output[$i]['status'] = $rpt_hash['per_recipient'][$i]['Status'];
  300. $output[$i]['action'] = $rpt_hash['per_recipient'][$i]['Action'];
  301. }
  302. }
  303. else if(isset($head_hash['X-failed-recipients'])) {
  304. // Busted Exim MTA
  305. // Up to 50 email addresses can be listed on each header.
  306. // There can be multiple X-Failed-Recipients: headers. - (not supported)
  307. $arrFailed = explode(',', $head_hash['X-failed-recipients']);
  308. for($j=0; $j<count($arrFailed); $j++){
  309. $output[$j]['recipient'] = trim($arrFailed[$j]);
  310. $output[$j]['status'] = BounceHandler::get_status_code_from_text($output[$j]['recipient'], $arrBody,0);
  311. $output[$j]['action'] = BounceHandler::get_action_from_status_code($output[$j]['status']);
  312. }
  313. }
  314. else if(!empty($boundary) && BounceHandler::is_a_bounce($head_hash)){
  315. // oh god it could be anything, but at least it has mime parts, so let's try anyway
  316. $arrFailed = BounceHandler::find_email_addresses($mime_sections['first_body_part']);
  317. for($j=0; $j<count($arrFailed); $j++){
  318. $output[$j]['recipient'] = trim($arrFailed[$j]);
  319. $output[$j]['status'] = BounceHandler::get_status_code_from_text($output[$j]['recipient'], $arrBody,0);
  320. $output[$j]['action'] = BounceHandler::get_action_from_status_code($output[$j]['status']);
  321. }
  322. }
  323. else if(BounceHandler::is_a_bounce($head_hash)){
  324. // last ditch attempt
  325. // could possibly produce erroneous output, or be very resource consuming,
  326. // so be careful. You should comment out this section if you are very concerned
  327. // about 100% accuracy or if you want very fast performance.
  328. // Leave it turned on if you know that all messages to be analyzed are bounces.
  329. $arrFailed = BounceHandler::find_email_addresses($body);
  330. for($j=0; $j<count($arrFailed); $j++){
  331. $output[$j]['recipient'] = trim($arrFailed[$j]);
  332. $output[$j]['status'] = BounceHandler::get_status_code_from_text($output[$j]['recipient'], $arrBody,0);
  333. $output[$j]['action'] = BounceHandler::get_action_from_status_code($output[$j]['status']);
  334. }
  335. }
  336. // else if()..... add a parser for your busted-ass MTA here
  337. return $output;
  338. }
  339. // general purpose recursive heuristic function
  340. // to try to extract useful info from the bounces produced by busted MTAs
  341. function get_status_code_from_text($recipient, $arrBody, $index){
  342. for($i=$index; $i<count($arrBody); $i++){
  343. $line = trim($arrBody[$i]);
  344. /******** recurse into the email if you find the recipient ********/
  345. if(stristr($line, $recipient)!==FALSE){
  346. // the status code MIGHT be in the next few lines after the recipient line,
  347. // depending on the message from the foreign host... What a laugh riot!
  348. $output = BounceHandler::get_status_code_from_text($recipient, $arrBody, $i+1);
  349. if($output){
  350. return $output;
  351. }
  352. }
  353. /******** exit conditions ********/
  354. // if it's the end of the human readable part in this stupid bounce
  355. if(stristr($line, '------ This is a copy of the message')!==FALSE){
  356. return '';
  357. }
  358. //if we see an email address other than our current recipient's,
  359. if(count(BounceHandler::find_email_addresses($line))>=1
  360. && stristr($line, $recipient)===FALSE
  361. && strstr($line, 'FROM:<')===FALSE){ // Kanon added this line because Hotmail puts the e-mail address too soon and there actually is error message stuff after it.
  362. return '';
  363. }
  364. /******** pattern matching ********/
  365. if( stristr($line, 'no such address')!==FALSE
  366. || stristr($line, 'Recipient address rejected')!==FALSE
  367. || stristr($line, 'User unknown in virtual alias table')!==FALSE){
  368. return '5.1.1';
  369. }
  370. else if(stristr($line, 'unrouteable mail domain')!==FALSE
  371. || stristr($line, 'Esta casilla ha expirado por falta de uso')!==FALSE){
  372. return '5.1.2';
  373. }
  374. else if(stristr($line, 'mailbox is full')!==FALSE
  375. || stristr($line, 'Mailbox quota usage exceeded')!==FALSE
  376. || stristr($line, 'User mailbox exceeds allowed size')!==FALSE){
  377. return '4.2.2';
  378. }
  379. else if(stristr($line, 'not yet been delivered')!==FALSE){
  380. return '4.2.0';
  381. }
  382. else if(stristr($line, 'mailbox unavailable')!==FALSE){
  383. return '5.2.0';
  384. }
  385. else if(stristr($line, 'Unrouteable address')!==FALSE){
  386. return '5.4.4';
  387. }
  388. else if(stristr($line, 'retry timeout exceeded')!==FALSE){
  389. return '4.4.7';
  390. }
  391. else if(stristr($line, 'The account or domain may not exist, they may be blacklisted, or missing the proper dns entries.')!==FALSE){ // Kanon added
  392. return '5.2.0'; // I guess.... seems like 5.1.1, 5.1.2, or 5.4.4 would fit too, but 5.2.0 seemed most generic
  393. }
  394. else if(stristr($line, '554 TRANSACTION FAILED')!==FALSE){ // Kanon added
  395. return '5.5.4'; // I think this should be 5.7.1. "SMTP error from remote mail server after end of data: ... (HVU:B1) http://postmaster.info.aol.com/errors/554hvub1.html" -- AOL rejects messages that have links to certain sites in them.
  396. }
  397. else if(stristr($line, 'Status: 4.4.1')!==FALSE
  398. || stristr($line, 'delivery temporarily suspended')!==FALSE){ // Kanon added
  399. return '4.4.1';
  400. }
  401. else if(stristr($line, '550 OU-002')!==FALSE
  402. || stristr($line, 'Mail rejected by Windows Live Hotmail for policy reasons')!==FALSE){ // Kanon added
  403. return '5.5.0'; // Again, why isn't this 5.7.1 instead?
  404. }
  405. else if(stristr($line, 'PERM_FAILURE: DNS Error: Domain name not found')!==FALSE){ // Kanon added
  406. return '5.1.2'; // Not sure if this is right code. Just copied from above.
  407. }
  408. else if(stristr($line, 'Delivery attempts will continue to be made for')!==FALSE){ // Kanon added. From Symantec_AntiVirus_for_SMTP_Gateways@uqam.ca
  409. return '4.2.0'; // I'm not sure why Symantec delayed this message, but x.2.x means something to do with the mailbox, which seemed appropriate. x.5.x (protocol) or x.7.x (security) also seem possibly appropriate. It seems a lot of times it's x.5.x when it seems to me it should be x.7.x, so maybe x.5.x is standard when mail is rejected due to spam-like characteristics instead of x.7.x like I think it should be.
  410. }
  411. else if(stristr($line, '554 delivery error:')!==FALSE){
  412. return '5.5.4'; // rogers.com
  413. }
  414. else if(strstr ($line, '550-5.1.1')!==FALSE
  415. || stristr($line, 'This Gmail user does not exist.')!==FALSE){ // Kanon added
  416. return '5.1.1'; // Or should it be 5.5.0?
  417. }
  418. else{
  419. // end strstr tests
  420. }
  421. // rfc1893 return code
  422. if(preg_match('/([245]\.[01234567]\.[012345678])/', $line, $matches)){
  423. $mycode = str_replace('.', '', $matches[1]);
  424. $mycode = Bouncehandler::format_status_code($mycode);
  425. return implode('.', $mycode['code']);
  426. }
  427. // search for RFC821 return code
  428. // thanks to mark.tolman@gmail.com
  429. // Maybe at some point it should have it's own place within the main parsing scheme (at line 88)
  430. if(preg_match('/\]?: ([45][01257][012345]) /', $line, $matches)
  431. || preg_match('/^([45][01257][012345]) (?:.*?)(?:denied|inactive|deactivated|rejected|disabled|unknown|no such|not (?:our|activated|a valid))+/i', $line, $matches))
  432. {
  433. $mycode = $matches[1];
  434. // map common codes to new rfc values
  435. if($mycode == '450' || $mycode == '550' || $mycode == '551' || $mycode == '554'){
  436. $mycode = '511';
  437. } else if($mycode == '452' || $mycode == '552'){
  438. $mycode = '422';
  439. } else if ($mycode == '421'){
  440. $mycode = '432';
  441. }
  442. $mycode = Bouncehandler::format_status_code($mycode);
  443. return implode('.', $mycode['code']);
  444. }
  445. }
  446. return '';
  447. }
  448. function init_bouncehandler($blob, $format='string'){
  449. if($format=='xml_array'){
  450. $strEmail = "";
  451. $out = "";
  452. for($i=0; $i<$blob; $i++){
  453. $out = preg_replace("/<HEADER>/i", "", $blob[$i]);
  454. $out = preg_replace("/</HEADER>/i", "", $out);
  455. $out = preg_replace("/<MESSAGE>/i", "", $out);
  456. $out = preg_replace("/</MESSAGE>/i", "", $out);
  457. $out = rtrim($out) . "\r\n";
  458. $strEmail .= $out;
  459. }
  460. }
  461. else if($format=='string'){
  462. $strEmail = str_replace("\r\n", "\n", $blob);
  463. $strEmail = str_replace("\n", "\r\n", $strEmail);
  464. }
  465. else if($format=='array'){
  466. $strEmail = "";
  467. for($i=0; $i<$blob; $i++){
  468. $strEmail .= rtrim($blob[$i]) . "\r\n";
  469. }
  470. }
  471. return $strEmail;
  472. }
  473. function is_RFC1892_multipart_report($head_hash){
  474. return $head_hash['Content-type']['type']=='multipart/report'
  475. && $head_hash['Content-type']['report-type']=='delivery-status'
  476. && $head_hash['Content-type']['boundary']!=='';
  477. }
  478. function parse_head($headers){
  479. if(!is_array($headers)) $headers = explode("\r\n", $headers);
  480. $hash = BounceHandler::standard_parser($headers);
  481. // get a little more complex
  482. $arrRec = explode('|', $hash['Received']);
  483. $hash['Received']= $arrRec;
  484. if($hash['Content-type']){//preg_match('/Multipart\/Report/i', $hash['Content-type'])){
  485. $multipart_report = explode (';', $hash['Content-type']);
  486. $hash['Content-type']='';
  487. $hash['Content-type']['type'] = strtolower($multipart_report[0]);
  488. foreach($multipart_report as $mr){
  489. if(preg_match('/([^=.]*?)=(.*)/i', $mr, $matches)){
  490. // didn't work when the content-type boundary ID contained an equal sign,
  491. // that exists in bounces from many Exchange servers
  492. //if(preg_match('/([a-z]*)=(.*)?/i', $mr, $matches)){
  493. $hash['Content-type'][strtolower(trim($matches[1]))]= str_replace('"','',$matches[2]);
  494. }
  495. }
  496. }
  497. return $hash;
  498. }
  499. function parse_body_into_mime_sections($body, $boundary){
  500. if(!$boundary) return array();
  501. if(is_array($body)) $body = implode("\r\n", $body);
  502. $body = explode($boundary, $body);
  503. $mime_sections['first_body_part'] = $body[1];
  504. $mime_sections['machine_parsable_body_part'] = $body[2];
  505. $mime_sections['returned_message_body_part'] = $body[3];
  506. return $mime_sections;
  507. }
  508. function standard_parser($content){ // associative array orstr
  509. // receives email head as array of lines
  510. // simple parse (Entity: value\n)
  511. $entity = "";
  512. if(!is_array($content)) $content = explode("\r\n", $content);
  513. foreach($content as $line){
  514. if(preg_match('/([^\s.]*):\s(.*)/', $line, $array)){
  515. $entity = ucfirst(strtolower($array[1]));
  516. if(empty($hash[$entity]))
  517. {
  518. $hash[$entity] = trim($array[2]);
  519. }
  520. else if($hash['Received']){
  521. // grab extra Received headers :(
  522. // pile it on with pipe delimiters,
  523. // oh well, SMTP is broken in this way
  524. if ($entity and $array[2] and $array[2] != $hash[$entity]){
  525. $hash[$entity] .= "|" . trim($array[2]);
  526. }
  527. }
  528. }
  529. else{
  530. if ($entity){
  531. $hash[$entity] .= " $line";
  532. }
  533. }
  534. }
  535. return $hash;
  536. }
  537. function parse_machine_parsable_body_part($str){
  538. //Per-Message DSN fields
  539. $hash = BounceHandler::parse_dsn_fields($str);
  540. $hash['mime_header'] = BounceHandler::standard_parser($hash['mime_header']);
  541. $hash['per_message'] = BounceHandler::standard_parser($hash['per_message']);
  542. if(isset($hash['per_message']['X-postfix-sender']) && $hash['per_message']['X-postfix-sender'] ){
  543. $arr = explode (';', $hash['per_message']['X-postfix-sender']);
  544. $hash['per_message']['X-postfix-sender']='';
  545. $hash['per_message']['X-postfix-sender']['type'] = trim($arr[0]);
  546. $hash['per_message']['X-postfix-sender']['addr'] = trim($arr[1]);
  547. }
  548. if($hash['per_message']['Reporting-mta']){
  549. $arr = explode (';', $hash['per_message']['Reporting-mta']);
  550. $hash['per_message']['Reporting-mta']='';
  551. $hash['per_message']['Reporting-mta']['type'] = trim($arr[0]);
  552. $hash['per_message']['Reporting-mta']['addr'] = trim($arr[1]);
  553. }
  554. //Per-Recipient DSN fields
  555. for($i=0; $i<count($hash['per_recipient']); $i++){
  556. $temp = BounceHandler::standard_parser(explode("\r\n", $hash['per_recipient'][$i]));
  557. $arr = explode (';', $temp['Final-recipient']);
  558. $temp['Final-recipient']='';
  559. $temp['Final-recipient']['type'] = trim($arr[0]);
  560. $temp['Final-recipient']['addr'] = trim($arr[1]);
  561. $arr = explode (';', $temp['Original-recipient']);
  562. $temp['Original-recipient']='';
  563. $temp['Original-recipient']['type'] = trim($arr[0]);
  564. $temp['Original-recipient']['addr'] = trim($arr[1]);
  565. $arr = explode (';', $temp['Diagnostic-code']);
  566. $temp['Diagnostic-code']='';
  567. $temp['Diagnostic-code']['type'] = trim($arr[0]);
  568. $temp['Diagnostic-code']['text'] = trim($arr[1]);
  569. // now this is wierd: plenty of times you see the status code is a permanent failure,
  570. // but the diagnostic code is a temporary failure. So we will assert the most general
  571. // temporary failure in this case.
  572. $ddc=''; $judgement='';
  573. $ddc = BounceHandler::decode_diagnostic_code($temp['Diagnostic-code']['text']);
  574. $judgement = BounceHandler::get_action_from_status_code($ddc);
  575. if($judgement == 'transient'){
  576. if(stristr($temp['Action'],'failed')!==FALSE){
  577. $temp['Action']='transient';
  578. $temp['Status']='4.3.0';
  579. }
  580. }
  581. $hash['per_recipient'][$i]='';
  582. $hash['per_recipient'][$i]=$temp;
  583. }
  584. return $hash;
  585. }
  586. function get_head_from_returned_message_body_part($mime_sections){
  587. $temp = explode("\r\n\r\n", $mime_sections[returned_message_body_part]);
  588. $head = BounceHandler::standard_parser($temp[1]);
  589. $head['From'] = BounceHandler::extract_address($head['From']);
  590. $head['To'] = BounceHandler::extract_address($head['To']);
  591. return $head;
  592. }
  593. function extract_address($str){
  594. $from_stuff = preg_split('/[ \"\'\<\>:\(\)\[\]]/', $str);
  595. foreach ($from_stuff as $things){
  596. if (strpos($things, '@')!==FALSE){$from = $things;}
  597. }
  598. return $from;
  599. }
  600. function get_recipient($per_rcpt){
  601. if($per_rcpt['Original-recipient']['addr'] !== ''){
  602. $recipient = $per_rcpt['Original-recipient']['addr'];
  603. }
  604. else if($per_rcpt['Final-recipient']['addr'] !== ''){
  605. $recipient = $per_rcpt['Final-recipient']['addr'];
  606. }
  607. $recipient = str_replace('<', '', $recipient);
  608. $recipient = str_replace('>', '', $recipient);
  609. return $recipient;
  610. }
  611. function parse_dsn_fields($dsn_fields){
  612. if(!is_array($dsn_fields)) $dsn_fields = explode("\r\n\r\n", $dsn_fields);
  613. $j = 0;
  614. reset($dsn_fields);
  615. for($i=0; $i<count($dsn_fields); $i++){
  616. if($i==0)
  617. $hash['mime_header'] = $dsn_fields[0];
  618. elseif($i==1 && !preg_match('/(Final|Original)-Recipient/',$dsn_fields[1])) {
  619. // some mta's don't output the per_message part, which means
  620. // the second element in the array should really be
  621. // per_recipient - test with Final-Recipient - which should always
  622. // indicate that the part is a per_recipient part
  623. $hash['per_message'] = $dsn_fields[1];
  624. }
  625. else {
  626. if($dsn_fields[$i] == '--') continue;
  627. $hash['per_recipient'][$j] = $dsn_fields[$i];
  628. $j++;
  629. }
  630. }
  631. return $hash;
  632. }
  633. function format_status_code($code){
  634. $ret = "";
  635. if(preg_match('/([245]\.[01234567]\.[012345678])(.*)/', $code, $matches)){
  636. $ret['code'] = $matches[1];
  637. $ret['text'] = $matches[2];
  638. }
  639. else if(preg_match('/([245][01234567][012345678])(.*)/', $code, $matches)){
  640. preg_match_all("/./", $matches[1], $out);
  641. $ret['code'] = $out[0];
  642. $ret['text'] = $matches[2];
  643. }
  644. return $ret;
  645. }
  646. function fetch_status_messages($code){
  647. include_once ("rfc1893.error.codes.php");
  648. $ret = BounceHandler::format_status_code($code);
  649. $arr = explode('.', $ret['code']);
  650. $str = "<P><B>". $this->status_code_classes[$arr[0]]['title'] . "</B> - " .$this->status_code_classes[$arr[0]]['descr']. " <B>". $this->status_code_subclasses[$arr[1].".".$arr[2]]['title'] . "</B> - " .$this->status_code_subclasses[$arr[1].".".$arr[2]]['descr']. "</P>";
  651. return $str;
  652. }
  653. function get_action_from_status_code($code){
  654. if($code=='') return '';
  655. $ret = BounceHandler::format_status_code($code);
  656. $stat = $ret['code'][0];
  657. switch($stat){
  658. case(2):
  659. return 'success';
  660. break;
  661. case(4):
  662. return 'transient';
  663. break;
  664. case(5):
  665. return 'failed';
  666. break;
  667. default:
  668. return '';
  669. break;
  670. }
  671. }
  672. function decode_diagnostic_code($dcode){
  673. if(preg_match("/(\d\.\d\.\d)\s/", $dcode, $array)){
  674. return $array[1];
  675. }
  676. else if(preg_match("/(\d\d\d)\s/", $dcode, $array)){
  677. return $array[1];
  678. }
  679. }
  680. function is_a_bounce($head_hash){
  681. if(preg_match("/(mail delivery failed|failure notice|warning: message|delivery status notif|delivery failure|delivery problem|spam eater|returned mail|undeliverable|returned mail|delivery errors|mail status report|mail system error|failure delivery|delivery notification|delivery has failed|undelivered mail|returned email|returning message to sender|returned to sender|message delayed|mdaemon notification|mailserver notification|mail delivery system|nondeliverable mail|mail transaction failed)|auto.{0,20}reply|vacation|(out|away|on holiday).*office/i", $head_hash['Subject'])) return true;
  682. if(preg_match('/auto_reply/',$head_hash['Precedence'])) return true;
  683. if(preg_match("/^(postmaster|mailer-daemon)\@?/i", $head_hash['From'])) return true;
  684. return false;
  685. }
  686. function find_email_addresses($first_body_part){
  687. // not finished yet. This finds only one address.
  688. if(preg_match("/\b([A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4})\b/i", $first_body_part, $matches)){
  689. return array($matches[1]);
  690. }
  691. else
  692. return array();
  693. }
  694. }/** END class BounceHandler **/
  695. ?>