From 08523e941113b71cff700e51e7823005c27cb5dc Mon Sep 17 00:00:00 2001 From: Olivier Date: Fri, 3 Jul 2015 10:05:41 -0400 Subject: [PATCH] CRM-16584 - Improve bounce management with Mandrilla SMTP --- CRM/Utils/Mail/EmailProcessor.php | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/CRM/Utils/Mail/EmailProcessor.php b/CRM/Utils/Mail/EmailProcessor.php index 3d469222b4..663da9f796 100644 --- a/CRM/Utils/Mail/EmailProcessor.php +++ b/CRM/Utils/Mail/EmailProcessor.php @@ -171,7 +171,8 @@ class CRM_Utils_Mail_EmailProcessor { $rpRegex = '/Return-Path: ' . preg_quote($dao->localpart) . '(b)' . $twoDigitString . '([0-9a-f]{16})@' . preg_quote($dao->domain) . '/'; // a regex for finding bound info X-Header - $rpXheaderRegex = '/X-CiviMail-Bounce: ' . preg_quote($dao->localpart) . '(b)' . $twoDigitString . '([0-9a-f]{16})@' . preg_quote($dao->domain) . '/'; + $rpXheaderRegex = '/X-CiviMail-Bounce: ' . preg_quote($dao->localpart) . '(b)' . $twoDigitString . '([0-9a-f]{16})@' . preg_quote($dao->domain) . '/i'; + // CiviMail in regex and Civimail in header !!! // retrieve the emails try { @@ -220,6 +221,23 @@ class CRM_Utils_Mail_EmailProcessor { if (!$matches and preg_match($rpXheaderRegex, $mail->generateBody(), $matches)) { list($match, $action, $job, $queue, $hash) = $matches; } + // With Mandrilla, the X-CiviMail-Bounce header is produced by generateBody + // is base64 encoded + // Check all parts + if (!$matches) { + $all_parts=$mail->fetchParts(); + foreach ($all_parts as $k_part => $v_part) { + if ($v_part instanceof ezcMailFile) { + $p_file = $v_part->__get('fileName'); + $c_file=file_get_contents($p_file); + if (preg_match($rpXheaderRegex, $c_file, $matches)) { + self::_log("file match rpXheaderRegex", $matches); + list($match, $action, $job, $queue, $hash) = $matches; + } + } + } + } + // if all else fails, check Delivered-To for possible pattern if (!$matches and preg_match($regex, $mail->getHeader('Delivered-To'), $matches)) { -- 2.25.1