Merge pull request #9954 from colemanw/CRM-20235
[civicrm-core.git] / CRM / Utils / Mail / EmailProcessor.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
7e9e8871 4 | CiviCRM version 4.7 |
6a488035 5 +--------------------------------------------------------------------+
0f03f337 6 | Copyright CiviCRM LLC (c) 2004-2017 |
6a488035
TO
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
29 *
30 * @package CRM
0f03f337 31 * @copyright CiviCRM LLC (c) 2004-2017
6a488035
TO
32 */
33
34// we should consider moving these to the settings table
35// before the 4.1 release
36define('EMAIL_ACTIVITY_TYPE_ID', NULL);
37define('MAIL_BATCH_SIZE', 50);
5bc392e6
EM
38
39/**
b8c71ffa 40 * Class CRM_Utils_Mail_EmailProcessor.
5bc392e6 41 */
6a488035
TO
42class CRM_Utils_Mail_EmailProcessor {
43
44 /**
45 * Process the default mailbox (ie. that is used by civiMail for the bounce)
46 *
ae5ffbb7
TO
47 * @return bool
48 * Always returns true (for the api). at a later stage we should
49 * fix this to return true on success / false on failure etc.
6a488035 50 */
00be9182 51 public static function processBounces() {
ae5ffbb7 52 $dao = new CRM_Core_DAO_MailSettings();
353ffa53 53 $dao->domain_id = CRM_Core_Config::domainID();
6a488035
TO
54 $dao->is_default = TRUE;
55 $dao->find();
56
57 while ($dao->fetch()) {
58 self::_process(TRUE, $dao);
59 }
60
61 // always returns true, i.e. never fails :)
62 return TRUE;
63 }
64
65 /**
b8c71ffa 66 * Delete old files from a given directory (recursively).
6a488035 67 *
77855840
TO
68 * @param string $dir
69 * Directory to cleanup.
70 * @param int $age
71 * Files older than this many seconds will be deleted (default: 60 days).
6a488035 72 */
00be9182 73 public static function cleanupDir($dir, $age = 5184000) {
6a488035
TO
74 // return early if we can’t read/write the dir
75 if (!is_writable($dir) or !is_readable($dir) or !is_dir($dir)) {
76 return;
77 }
78
79 foreach (scandir($dir) as $file) {
80
81 // don’t go up the directory stack and skip new files/dirs
82 if ($file == '.' or $file == '..') {
83 continue;
84 }
85 if (filemtime("$dir/$file") > time() - $age) {
86 continue;
87 }
88
89 // it’s an old file/dir, so delete/recurse
90 is_dir("$dir/$file") ? self::cleanupDir("$dir/$file", $age) : unlink("$dir/$file");
91 }
92 }
93
94 /**
b8c71ffa 95 * Process the mailboxes that aren't default (ie. that aren't used by civiMail for the bounce).
6a488035 96 */
00be9182 97 public static function processActivities() {
ae5ffbb7 98 $dao = new CRM_Core_DAO_MailSettings();
353ffa53 99 $dao->domain_id = CRM_Core_Config::domainID();
6a488035
TO
100 $dao->is_default = FALSE;
101 $dao->find();
102 $found = FALSE;
103 while ($dao->fetch()) {
104 $found = TRUE;
105 self::_process(FALSE, $dao);
106 }
107 if (!$found) {
108 CRM_Core_Error::fatal(ts('No mailboxes have been configured for Email to Activity Processing'));
109 }
110 return $found;
111 }
112
113 /**
fe482240 114 * Process the mailbox for all the settings from civicrm_mail_settings.
6a488035 115 *
fd31fa4c 116 * @param bool|string $civiMail if true, processing is done in CiviMail context, or Activities otherwise.
6a488035 117 */
00be9182 118 public static function process($civiMail = TRUE) {
ae5ffbb7 119 $dao = new CRM_Core_DAO_MailSettings();
6a488035
TO
120 $dao->domain_id = CRM_Core_Config::domainID();
121 $dao->find();
122
123 while ($dao->fetch()) {
124 self::_process($civiMail, $dao);
125 }
126 }
127
5bc392e6
EM
128 /**
129 * @param $civiMail
c490a46a 130 * @param CRM_Core_DAO $dao
5bc392e6
EM
131 *
132 * @throws Exception
133 */
00be9182 134 public static function _process($civiMail, $dao) {
6a488035
TO
135 // 0 = activities; 1 = bounce;
136 $usedfor = $dao->is_default;
137
ae5ffbb7 138 $emailActivityTypeId
d15a97f4 139 = (defined('EMAIL_ACTIVITY_TYPE_ID') && EMAIL_ACTIVITY_TYPE_ID)
140 ? EMAIL_ACTIVITY_TYPE_ID
141 : CRM_Core_OptionGroup::getValue('activity_type', 'Inbound Email', 'name');
6a488035
TO
142
143 if (!$emailActivityTypeId) {
144 CRM_Core_Error::fatal(ts('Could not find a valid Activity Type ID for Inbound Email'));
145 }
146
353ffa53
TO
147 $config = CRM_Core_Config::singleton();
148 $verpSeperator = preg_quote($config->verpSeparator);
6a488035 149 $twoDigitStringMin = $verpSeperator . '(\d+)' . $verpSeperator . '(\d+)';
353ffa53
TO
150 $twoDigitString = $twoDigitStringMin . $verpSeperator;
151 $threeDigitString = $twoDigitString . '(\d+)' . $verpSeperator;
6a488035
TO
152
153 // FIXME: legacy regexen to handle CiviCRM 2.1 address patterns, with domain id and possible VERP part
154 $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) . '$/';
155 $subscrRegex = '/^' . preg_quote($dao->localpart) . '(s|subscribe)' . $twoDigitStringMin . '@' . preg_quote($dao->domain) . '$/';
156
157 // a common-for-all-actions regex to handle CiviCRM 2.2 address patterns
158 $regex = '/^' . preg_quote($dao->localpart) . '(b|c|e|o|r|u)' . $twoDigitString . '([0-9a-f]{16})@' . preg_quote($dao->domain) . '$/';
159
160 // a tighter regex for finding bounce info in soft bounces’ mail bodies
619526f6 161 $rpRegex = '/Return-Path:\s*' . preg_quote($dao->localpart) . '(b)' . $twoDigitString . '([0-9a-f]{16})@' . preg_quote($dao->domain) . '/';
6a488035 162
1a4d92b6 163 // a regex for finding bound info X-Header
08523e94
O
164 $rpXheaderRegex = '/X-CiviMail-Bounce: ' . preg_quote($dao->localpart) . '(b)' . $twoDigitString . '([0-9a-f]{16})@' . preg_quote($dao->domain) . '/i';
165 // CiviMail in regex and Civimail in header !!!
1a4d92b6 166
6a488035
TO
167 // retrieve the emails
168 try {
169 $store = CRM_Mailing_MailStore::getStore($dao->name);
170 }
353ffa53 171 catch (Exception$e) {
86bfa4f6 172 $message = ts('Could not connect to MailStore for ') . $dao->username . '@' . $dao->server . '<p>';
6a488035
TO
173 $message .= ts('Error message: ');
174 $message .= '<pre>' . $e->getMessage() . '</pre><p>';
175 CRM_Core_Error::fatal($message);
176 }
177
6a488035
TO
178 // process fifty at a time, CRM-4002
179 while ($mails = $store->fetchNext(MAIL_BATCH_SIZE)) {
180 foreach ($mails as $key => $mail) {
181
182 // for every addressee: match address elements if it's to CiviMail
183 $matches = array();
184 $action = NULL;
185
186 if ($usedfor == 1) {
187 foreach ($mail->to as $address) {
188 if (preg_match($regex, $address->email, $matches)) {
189 list($match, $action, $job, $queue, $hash) = $matches;
190 break;
191 // FIXME: the below elseifs should be dropped when we drop legacy support
192 }
193 elseif (preg_match($commonRegex, $address->email, $matches)) {
194 list($match, $action, $_, $job, $queue, $hash) = $matches;
195 break;
196 }
197 elseif (preg_match($subscrRegex, $address->email, $matches)) {
198 list($match, $action, $_, $job) = $matches;
199 break;
200 }
201 }
202
203 // CRM-5471: if $matches is empty, it still might be a soft bounce sent
204 // to another address, so scan the body for ‘Return-Path: …bounce-pattern…’
205 if (!$matches and preg_match($rpRegex, $mail->generateBody(), $matches)) {
206 list($match, $action, $job, $queue, $hash) = $matches;
207 }
208
1a4d92b6
DL
209 // if $matches is still empty, look for the X-CiviMail-Bounce header
210 // CRM-9855
211 if (!$matches and preg_match($rpXheaderRegex, $mail->generateBody(), $matches)) {
212 list($match, $action, $job, $queue, $hash) = $matches;
213 }
08523e94
O
214 // With Mandrilla, the X-CiviMail-Bounce header is produced by generateBody
215 // is base64 encoded
216 // Check all parts
217 if (!$matches) {
6ac3485f 218 $all_parts = $mail->fetchParts();
08523e94
O
219 foreach ($all_parts as $k_part => $v_part) {
220 if ($v_part instanceof ezcMailFile) {
221 $p_file = $v_part->__get('fileName');
6ac3485f 222 $c_file = file_get_contents($p_file);
08523e94 223 if (preg_match($rpXheaderRegex, $c_file, $matches)) {
08523e94
O
224 list($match, $action, $job, $queue, $hash) = $matches;
225 }
226 }
227 }
228 }
229
6a488035
TO
230 // if all else fails, check Delivered-To for possible pattern
231 if (!$matches and preg_match($regex, $mail->getHeader('Delivered-To'), $matches)) {
232 list($match, $action, $job, $queue, $hash) = $matches;
233 }
234 }
235
236 // preseve backward compatibility
237 if ($usedfor == 0 || !$civiMail) {
238 // if its the activities that needs to be processed ..
5fdd5f80 239 try {
83cd2236 240 $mailParams = CRM_Utils_Mail_Incoming::parseMailingObject($mail);
fad9031a 241 }
83cd2236
DH
242 catch (Exception $e) {
243 echo $e->getMessage();
244 $store->markIgnored($key);
245 continue;
246 }
6a488035 247
69c9ffb3 248 require_once 'CRM/Utils/DeprecatedUtils.php';
6a488035
TO
249 $params = _civicrm_api3_deprecated_activity_buildmailparams($mailParams, $emailActivityTypeId);
250
251 $params['version'] = 3;
252 $result = civicrm_api('activity', 'create', $params);
253
254 if ($result['is_error']) {
255 $matches = FALSE;
256 echo "Failed Processing: {$mail->subject}. Reason: {$result['error_message']}\n";
257 }
258 else {
259 $matches = TRUE;
7349cf08 260 CRM_Utils_Hook::emailProcessor('activity', $params, $mail, $result);
6a488035
TO
261 echo "Processed as Activity: {$mail->subject}\n";
262 }
6a488035
TO
263 }
264
265 // if $matches is empty, this email is not CiviMail-bound
266 if (!$matches) {
267 $store->markIgnored($key);
268 continue;
269 }
270
271 // get $replyTo from either the Reply-To header or from From
272 // FIXME: make sure it works with Reply-Tos containing non-email stuff
273 $replyTo = $mail->getHeader('Reply-To') ? $mail->getHeader('Reply-To') : $mail->from->email;
274
275 // handle the action by passing it to the proper API call
276 // FIXME: leave only one-letter cases when dropping legacy support
277 if (!empty($action)) {
278 $result = NULL;
279
280 switch ($action) {
281 case 'b':
282 case 'bounce':
283 $text = '';
284 if ($mail->body instanceof ezcMailText) {
285 $text = $mail->body->text;
286 }
287 elseif ($mail->body instanceof ezcMailMultipart) {
b606cd04 288 if ($mail->body instanceof ezcMailMultipartReport) {
9b9a8713
SL
289 $part = $mail->body->getMachinePart();
290 if ($part instanceof ezcMailDeliveryStatus) {
291 foreach ($part->recipients as $rec) {
292 if (isset($rec["Diagnostic-Code"])) {
293 $text = $rec["Diagnostic-Code"];
294 break;
295 }
0addad12
SL
296 elseif (isset($rec["Description"])) {
297 $text = $rec["Description"];
298 break;
299 }
300 // no diagnostic info present - try getting the human readable part
301 elseif (isset($rec["Status"])) {
302 $text = $rec["Status"];
303 $textpart = $mail->body->getReadablePart();
304 if ($textpart != NULL and isset($textpart->text)) {
305 $text .= " " . $textpart->text;
306 }
307 else {
308 $text .= " Delivery failed but no diagnostic code or description.";
309 }
310 break;
311 }
9b9a8713
SL
312 }
313 }
0addad12 314 elseif ($part != NULL and isset($part->text)) {
9b9a8713
SL
315 $text = $part->text;
316 }
abee52bc 317 elseif (($part = $mail->body->getReadablePart()) != NULL) {
9b9a8713
SL
318 $text = $part->text;
319 }
320 }
321 elseif ($mail->body instanceof ezcMailMultipartRelated) {
6a488035
TO
322 foreach ($mail->body->getRelatedParts() as $part) {
323 if (isset($part->subType) and $part->subType == 'plain') {
324 $text = $part->text;
325 break;
326 }
327 }
328 }
329 else {
330 foreach ($mail->body->getParts() as $part) {
331 if (isset($part->subType) and $part->subType == 'plain') {
332 $text = $part->text;
333 break;
334 }
335 }
336 }
337 }
338
339 if (
9b9a8713 340 empty($text) &&
6a488035
TO
341 $mail->subject == "Delivery Status Notification (Failure)"
342 ) {
343 // Exchange error - CRM-9361
344 foreach ($mail->body->getParts() as $part) {
345 if ($part instanceof ezcMailDeliveryStatus) {
346 foreach ($part->recipients as $rec) {
347 if ($rec["Status"] == "5.1.1") {
9b9a8713
SL
348 if (isset($rec["Description"])) {
349 $text = $rec["Description"];
350 }
351 else {
0addad12 352 $text = $rec["Status"] . " Delivery to the following recipients failed";
9b9a8713 353 }
6a488035
TO
354 break;
355 }
356 }
357 }
358 }
359 }
360
361 if (empty($text)) {
362 // If bounce processing fails, just take the raw body. Cf. CRM-11046
363 $text = $mail->generateBody();
364
365 // if text is still empty, lets fudge a blank text so the api call below will succeed
6ac9d864
DL
366 if (empty($text)) {
367 $text = ts('We could not extract the mail body from this bounce message.');
368 }
6a488035
TO
369 }
370
371 $params = array(
372 'job_id' => $job,
373 'event_queue_id' => $queue,
374 'hash' => $hash,
375 'body' => $text,
376 'version' => 3,
cdc5c450 377 // Setting is_transactional means it will rollback if
378 // it crashes part way through creating the bounce.
379 // If the api were standard & had a create this would be the
380 // default. Adding the standard api & deprecating this one
381 // would probably be the
382 // most consistent way to address this - but this is
383 // a quick hack.
384 'is_transactional' => 1,
6a488035
TO
385 );
386 $result = civicrm_api('Mailing', 'event_bounce', $params);
387 break;
388
389 case 'c':
390 case 'confirm':
391 // CRM-7921
392 $params = array(
393 'contact_id' => $job,
394 'subscribe_id' => $queue,
395 'hash' => $hash,
396 'version' => 3,
397 );
398 $result = civicrm_api('Mailing', 'event_confirm', $params);
399 break;
400
401 case 'o':
402 case 'optOut':
403 $params = array(
404 'job_id' => $job,
405 'event_queue_id' => $queue,
406 'hash' => $hash,
407 'version' => 3,
408 );
409 $result = civicrm_api('MailingGroup', 'event_domain_unsubscribe', $params);
410 break;
411
412 case 'r':
413 case 'reply':
414 // instead of text and HTML parts (4th and 6th params) send the whole email as the last param
415 $params = array(
416 'job_id' => $job,
417 'event_queue_id' => $queue,
418 'hash' => $hash,
419 'bodyTxt' => NULL,
420 'replyTo' => $replyTo,
421 'bodyHTML' => NULL,
422 'fullEmail' => $mail->generate(),
423 'version' => 3,
424 );
425 $result = civicrm_api('Mailing', 'event_reply', $params);
426 break;
427
428 case 'e':
429 case 're':
430 case 'resubscribe':
431 $params = array(
432 'job_id' => $job,
433 'event_queue_id' => $queue,
434 'hash' => $hash,
435 'version' => 3,
436 );
437 $result = civicrm_api('MailingGroup', 'event_resubscribe', $params);
438 break;
439
440 case 's':
441 case 'subscribe':
442 $params = array(
443 'email' => $mail->from->email,
444 'group_id' => $job,
445 'version' => 3,
446 );
447 $result = civicrm_api('MailingGroup', 'event_subscribe', $params);
448 break;
449
450 case 'u':
451 case 'unsubscribe':
452 $params = array(
453 'job_id' => $job,
454 'event_queue_id' => $queue,
455 'hash' => $hash,
456 'version' => 3,
457 );
458 $result = civicrm_api('MailingGroup', 'event_unsubscribe', $params);
459 break;
460 }
461
462 if ($result['is_error']) {
463 echo "Failed Processing: {$mail->subject}, Action: $action, Job ID: $job, Queue ID: $queue, Hash: $hash. Reason: {$result['error_message']}\n";
464 }
465 else {
466 CRM_Utils_Hook::emailProcessor('mailing', $params, $mail, $result, $action);
467 }
468 }
469
470 $store->markProcessed($key);
471 }
472 // CRM-7356 – used by IMAP only
473 $store->expunge();
474 }
475 }
96025800 476
6a488035 477}