3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
18 // we should consider moving these to the settings table
19 // before the 4.1 release
20 define('EMAIL_ACTIVITY_TYPE_ID', NULL);
21 define('MAIL_BATCH_SIZE', 50);
24 * Class CRM_Utils_Mail_EmailProcessor.
26 class CRM_Utils_Mail_EmailProcessor
{
28 const MIME_MAX_RECURSION
= 10;
31 * Process the default mailbox (ie. that is used by civiMail for the bounce)
33 * @param bool $is_create_activities
34 * Should activities be created
36 public static function processBounces($is_create_activities) {
37 $dao = new CRM_Core_DAO_MailSettings();
38 $dao->domain_id
= CRM_Core_Config
::domainID();
39 $dao->is_default
= TRUE;
42 while ($dao->fetch()) {
43 self
::_process(TRUE, $dao, $is_create_activities);
48 * Delete old files from a given directory (recursively).
51 * Directory to cleanup.
53 * Files older than this many seconds will be deleted (default: 60 days).
55 public static function cleanupDir($dir, $age = 5184000) {
56 // return early if we can’t read/write the dir
57 if (!is_writable($dir) or !is_readable($dir) or !is_dir($dir)) {
61 foreach (scandir($dir) as $file) {
63 // don’t go up the directory stack and skip new files/dirs
64 if ($file == '.' or $file == '..') {
67 if (filemtime("$dir/$file") > time() - $age) {
71 // it’s an old file/dir, so delete/recurse
72 is_dir("$dir/$file") ? self
::cleanupDir("$dir/$file", $age) : unlink("$dir/$file");
77 * Process the mailboxes that aren't default (ie. that aren't used by civiMail for the bounce).
81 * @throws CRM_Core_Exception.
83 public static function processActivities() {
84 $dao = new CRM_Core_DAO_MailSettings();
85 $dao->domain_id
= CRM_Core_Config
::domainID();
86 $dao->is_default
= FALSE;
89 while ($dao->fetch()) {
91 self
::_process(FALSE, $dao, TRUE);
94 throw new CRM_Core_Exception(ts('No mailboxes have been configured for Email to Activity Processing'));
100 * Process the mailbox for all the settings from civicrm_mail_settings.
102 * @param bool|string $civiMail if true, processing is done in CiviMail context, or Activities otherwise.
104 public static function process($civiMail = TRUE) {
105 $dao = new CRM_Core_DAO_MailSettings();
106 $dao->domain_id
= CRM_Core_Config
::domainID();
109 while ($dao->fetch()) {
110 self
::_process($civiMail, $dao);
116 * @param CRM_Core_DAO_MailSettings $dao
117 * @param bool $is_create_activities
121 * @throws CRM_Core_Exception
123 public static function _process($civiMail, $dao, $is_create_activities) {
124 // 0 = activities; 1 = bounce;
125 $usedfor = $dao->is_default
;
128 = (defined('EMAIL_ACTIVITY_TYPE_ID') && EMAIL_ACTIVITY_TYPE_ID
)
129 ? EMAIL_ACTIVITY_TYPE_ID
130 : CRM_Core_PseudoConstant
::getKey('CRM_Activity_BAO_Activity', 'activity_type_id', 'Inbound Email');
132 if (!$emailActivityTypeId) {
133 throw new CRM_Core_Exception(ts('Could not find a valid Activity Type ID for Inbound Email'));
136 $config = CRM_Core_Config
::singleton();
137 $verpSeperator = preg_quote($config->verpSeparator
);
138 $twoDigitStringMin = $verpSeperator . '(\d+)' . $verpSeperator . '(\d+)';
139 $twoDigitString = $twoDigitStringMin . $verpSeperator;
140 $threeDigitString = $twoDigitString . '(\d+)' . $verpSeperator;
142 // FIXME: legacy regexen to handle CiviCRM 2.1 address patterns, with domain id and possible VERP part
143 $commonRegex = '/^' . preg_quote($dao->localpart
) . '(b|bounce|c|confirm|o|optOut|r|reply|re|e|resubscribe|u|unsubscribe)' . $threeDigitString . '([0-9a-f]{16})(-.*)?@' . preg_quote($dao->domain
) . '$/';
144 $subscrRegex = '/^' . preg_quote($dao->localpart
) . '(s|subscribe)' . $twoDigitStringMin . '@' . preg_quote($dao->domain
) . '$/';
146 // a common-for-all-actions regex to handle CiviCRM 2.2 address patterns
147 $regex = '/^' . preg_quote($dao->localpart
) . '(b|c|e|o|r|u)' . $twoDigitString . '([0-9a-f]{16})@' . preg_quote($dao->domain
) . '$/';
149 // a tighter regex for finding bounce info in soft bounces’ mail bodies
150 $rpRegex = '/Return-Path:\s*' . preg_quote($dao->localpart
) . '(b)' . $twoDigitString . '([0-9a-f]{16})@' . preg_quote($dao->domain
) . '/';
152 // a regex for finding bound info X-Header
153 $rpXheaderRegex = '/X-CiviMail-Bounce: ' . preg_quote($dao->localpart
) . '(b)' . $twoDigitString . '([0-9a-f]{16})@' . preg_quote($dao->domain
) . '/i';
154 // CiviMail in regex and Civimail in header !!!
156 // retrieve the emails
158 $store = CRM_Mailing_MailStore
::getStore($dao->name
);
160 catch (Exception
$e) {
161 $message = ts('Could not connect to MailStore for ') . $dao->username
. '@' . $dao->server
. '<p>';
162 $message .= ts('Error message: ');
163 $message .= '<pre>' . $e->getMessage() . '</pre><p>';
164 throw new CRM_Core_Exception($message);
167 // process fifty at a time, CRM-4002
168 while ($mails = $store->fetchNext(MAIL_BATCH_SIZE
)) {
169 foreach ($mails as $key => $mail) {
171 // for every addressee: match address elements if it's to CiviMail
176 foreach ($mail->to
as $address) {
177 if (preg_match($regex, $address->email
, $matches)) {
178 list($match, $action, $job, $queue, $hash) = $matches;
180 // FIXME: the below elseifs should be dropped when we drop legacy support
182 elseif (preg_match($commonRegex, $address->email
, $matches)) {
183 list($match, $action, $_, $job, $queue, $hash) = $matches;
186 elseif (preg_match($subscrRegex, $address->email
, $matches)) {
187 list($match, $action, $_, $job) = $matches;
192 // CRM-5471: if $matches is empty, it still might be a soft bounce sent
193 // to another address, so scan the body for ‘Return-Path: …bounce-pattern…’
194 if (!$matches and preg_match($rpRegex, $mail->generateBody(), $matches)) {
195 list($match, $action, $job, $queue, $hash) = $matches;
198 // if $matches is still empty, look for the X-CiviMail-Bounce header
200 if (!$matches and preg_match($rpXheaderRegex, $mail->generateBody(), $matches)) {
201 list($match, $action, $job, $queue, $hash) = $matches;
203 // With Mandrilla, the X-CiviMail-Bounce header is produced by generateBody
207 $all_parts = $mail->fetchParts();
208 foreach ($all_parts as $k_part => $v_part) {
209 if ($v_part instanceof ezcMailFile
) {
210 $p_file = $v_part->__get('fileName');
211 $c_file = file_get_contents($p_file);
212 if (preg_match($rpXheaderRegex, $c_file, $matches)) {
213 list($match, $action, $job, $queue, $hash) = $matches;
219 // if all else fails, check Delivered-To for possible pattern
220 if (!$matches and preg_match($regex, $mail->getHeader('Delivered-To'), $matches)) {
221 list($match, $action, $job, $queue, $hash) = $matches;
225 // preseve backward compatibility
226 if ($usedfor == 0 ||
$is_create_activities) {
227 // if its the activities that needs to be processed ..
229 $mailParams = CRM_Utils_Mail_Incoming
::parseMailingObject($mail);
231 catch (Exception
$e) {
232 echo $e->getMessage();
233 $store->markIgnored($key);
237 require_once 'CRM/Utils/DeprecatedUtils.php';
238 $params = _civicrm_api3_deprecated_activity_buildmailparams($mailParams, $emailActivityTypeId);
240 $params['version'] = 3;
241 if (!empty($dao->activity_status
)) {
242 $params['status_id'] = $dao->activity_status
;
244 $result = civicrm_api('activity', 'create', $params);
246 if ($result['is_error']) {
248 echo "Failed Processing: {$mail->subject}. Reason: {$result['error_message']}\n";
252 CRM_Utils_Hook
::emailProcessor('activity', $params, $mail, $result);
253 echo "Processed as Activity: {$mail->subject}\n";
257 // if $matches is empty, this email is not CiviMail-bound
259 $store->markIgnored($key);
263 // get $replyTo from either the Reply-To header or from From
264 // FIXME: make sure it works with Reply-Tos containing non-email stuff
265 $replyTo = $mail->getHeader('Reply-To') ?
$mail->getHeader('Reply-To') : $mail->from
->email
;
267 // handle the action by passing it to the proper API call
268 // FIXME: leave only one-letter cases when dropping legacy support
269 if (!empty($action)) {
276 if ($mail->body
instanceof ezcMailText
) {
277 $text = $mail->body
->text
;
279 elseif ($mail->body
instanceof ezcMailMultipart
) {
280 $text = self
::getTextFromMultipart($mail->body
);
282 elseif ($mail->body
instanceof ezcMailFile
) {
283 $text = file_get_contents($mail->body
->__get('fileName'));
288 $mail->subject
== "Delivery Status Notification (Failure)"
290 // Exchange error - CRM-9361
291 foreach ($mail->body
->getParts() as $part) {
292 if ($part instanceof ezcMailDeliveryStatus
) {
293 foreach ($part->recipients
as $rec) {
294 if ($rec["Status"] == "5.1.1") {
295 if (isset($rec["Description"])) {
296 $text = $rec["Description"];
299 $text = $rec["Status"] . " Delivery to the following recipients failed";
309 // If bounce processing fails, just take the raw body. Cf. CRM-11046
310 $text = $mail->generateBody();
312 // if text is still empty, lets fudge a blank text so the api call below will succeed
314 $text = ts('We could not extract the mail body from this bounce message.');
320 'event_queue_id' => $queue,
324 // Setting is_transactional means it will rollback if
325 // it crashes part way through creating the bounce.
326 // If the api were standard & had a create this would be the
327 // default. Adding the standard api & deprecating this one
328 // would probably be the
329 // most consistent way to address this - but this is
331 'is_transactional' => 1,
333 $result = civicrm_api('Mailing', 'event_bounce', $params);
340 'contact_id' => $job,
341 'subscribe_id' => $queue,
345 $result = civicrm_api('Mailing', 'event_confirm', $params);
352 'event_queue_id' => $queue,
356 $result = civicrm_api('MailingGroup', 'event_domain_unsubscribe', $params);
361 // instead of text and HTML parts (4th and 6th params) send the whole email as the last param
364 'event_queue_id' => $queue,
367 'replyTo' => $replyTo,
369 'fullEmail' => $mail->generate(),
372 $result = civicrm_api('Mailing', 'event_reply', $params);
380 'event_queue_id' => $queue,
384 $result = civicrm_api('MailingGroup', 'event_resubscribe', $params);
390 'email' => $mail->from
->email
,
394 $result = civicrm_api('MailingGroup', 'event_subscribe', $params);
401 'event_queue_id' => $queue,
405 $result = civicrm_api('MailingGroup', 'event_unsubscribe', $params);
409 if ($result['is_error']) {
410 echo "Failed Processing: {$mail->subject}, Action: $action, Job ID: $job, Queue ID: $queue, Hash: $hash. Reason: {$result['error_message']}\n";
413 CRM_Utils_Hook
::emailProcessor('mailing', $params, $mail, $result, $action);
417 $store->markProcessed($key);
419 // CRM-7356 – used by IMAP only
425 * @param \ezcMailMultipart $multipart
426 * @param int $recursionLevel
430 protected static function getTextFromMultipart($multipart, $recursionLevel = 0) {
431 if ($recursionLevel >= self
::MIME_MAX_RECURSION
) {
434 $recursionLevel +
= 1;
436 if ($multipart instanceof ezcMailMultipartReport
) {
437 $text = self
::getTextFromMulipartReport($multipart, $recursionLevel);
439 elseif ($multipart instanceof ezcMailMultipartRelated
) {
440 $text = self
::getTextFromMultipartRelated($multipart, $recursionLevel);
443 foreach ($multipart->getParts() as $part) {
444 if (isset($part->subType
) and $part->subType
=== 'plain') {
447 elseif ($part instanceof ezcMailMultipart
) {
448 $text = self
::getTextFromMultipart($part, $recursionLevel);
459 * @param \ezcMailMultipartRelated $related
460 * @param int $recursionLevel
464 protected static function getTextFromMultipartRelated($related, $recursionLevel) {
466 foreach ($related->getRelatedParts() as $part) {
467 if (isset($part->subType
) and $part->subType
=== 'plain') {
470 elseif ($part instanceof ezcMailMultipart
) {
471 $text = self
::getTextFromMultipart($part, $recursionLevel);
481 * @param \ezcMailMultipartReport $multipart
482 * @param $recursionLevel
486 protected static function getTextFromMulipartReport($multipart, $recursionLevel) {
488 $part = $multipart->getMachinePart();
489 if ($part instanceof ezcMailDeliveryStatus
) {
490 foreach ($part->recipients
as $rec) {
491 if (isset($rec["Diagnostic-Code"])) {
492 $text = $rec["Diagnostic-Code"];
495 elseif (isset($rec["Description"])) {
496 $text = $rec["Description"];
499 // no diagnostic info present - try getting the human readable part
500 elseif (isset($rec["Status"])) {
501 $text = $rec["Status"];
502 $textpart = $multipart->getReadablePart();
503 if ($textpart !== NULL and isset($textpart->text
)) {
504 $text .= " " . $textpart->text
;
507 $text .= " Delivery failed but no diagnostic code or description.";
513 elseif ($part !== NULL and isset($part->text
)) {
516 elseif (($part = $multipart->getReadablePart()) !== NULL) {
517 if (isset($part->text
)) {
520 elseif ($part instanceof ezcMailMultipart
) {
521 $text = self
::getTextFromMultipart($part, $recursionLevel);