Merge pull request #7202 from colemanw/Date
[civicrm-core.git] / CRM / Utils / Mail / EmailProcessor.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
7e9e8871 4 | CiviCRM version 4.7 |
6a488035 5 +--------------------------------------------------------------------+
e7112fa7 6 | Copyright CiviCRM LLC (c) 2004-2015 |
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
e7112fa7 31 * @copyright CiviCRM LLC (c) 2004-2015
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
TO
138 $emailActivityTypeId
139 = (defined('EMAIL_ACTIVITY_TYPE_ID') && EMAIL_ACTIVITY_TYPE_ID) ? EMAIL_ACTIVITY_TYPE_ID : CRM_Core_OptionGroup::getValue(
6a488035 140 'activity_type',
353ffa53
TO
141 'Inbound Email',
142 'name'
143 );
6a488035
TO
144
145 if (!$emailActivityTypeId) {
146 CRM_Core_Error::fatal(ts('Could not find a valid Activity Type ID for Inbound Email'));
147 }
148
353ffa53
TO
149 $config = CRM_Core_Config::singleton();
150 $verpSeperator = preg_quote($config->verpSeparator);
6a488035 151 $twoDigitStringMin = $verpSeperator . '(\d+)' . $verpSeperator . '(\d+)';
353ffa53
TO
152 $twoDigitString = $twoDigitStringMin . $verpSeperator;
153 $threeDigitString = $twoDigitString . '(\d+)' . $verpSeperator;
6a488035
TO
154
155 // FIXME: legacy regexen to handle CiviCRM 2.1 address patterns, with domain id and possible VERP part
156 $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) . '$/';
157 $subscrRegex = '/^' . preg_quote($dao->localpart) . '(s|subscribe)' . $twoDigitStringMin . '@' . preg_quote($dao->domain) . '$/';
158
159 // a common-for-all-actions regex to handle CiviCRM 2.2 address patterns
160 $regex = '/^' . preg_quote($dao->localpart) . '(b|c|e|o|r|u)' . $twoDigitString . '([0-9a-f]{16})@' . preg_quote($dao->domain) . '$/';
161
162 // a tighter regex for finding bounce info in soft bounces’ mail bodies
163 $rpRegex = '/Return-Path: ' . preg_quote($dao->localpart) . '(b)' . $twoDigitString . '([0-9a-f]{16})@' . preg_quote($dao->domain) . '/';
164
1a4d92b6 165 // a regex for finding bound info X-Header
08523e94
O
166 $rpXheaderRegex = '/X-CiviMail-Bounce: ' . preg_quote($dao->localpart) . '(b)' . $twoDigitString . '([0-9a-f]{16})@' . preg_quote($dao->domain) . '/i';
167 // CiviMail in regex and Civimail in header !!!
1a4d92b6 168
6a488035
TO
169 // retrieve the emails
170 try {
171 $store = CRM_Mailing_MailStore::getStore($dao->name);
172 }
353ffa53 173 catch (Exception$e) {
86bfa4f6 174 $message = ts('Could not connect to MailStore for ') . $dao->username . '@' . $dao->server . '<p>';
6a488035
TO
175 $message .= ts('Error message: ');
176 $message .= '<pre>' . $e->getMessage() . '</pre><p>';
177 CRM_Core_Error::fatal($message);
178 }
179
6a488035
TO
180 // process fifty at a time, CRM-4002
181 while ($mails = $store->fetchNext(MAIL_BATCH_SIZE)) {
182 foreach ($mails as $key => $mail) {
183
184 // for every addressee: match address elements if it's to CiviMail
185 $matches = array();
186 $action = NULL;
187
188 if ($usedfor == 1) {
189 foreach ($mail->to as $address) {
190 if (preg_match($regex, $address->email, $matches)) {
191 list($match, $action, $job, $queue, $hash) = $matches;
192 break;
193 // FIXME: the below elseifs should be dropped when we drop legacy support
194 }
195 elseif (preg_match($commonRegex, $address->email, $matches)) {
196 list($match, $action, $_, $job, $queue, $hash) = $matches;
197 break;
198 }
199 elseif (preg_match($subscrRegex, $address->email, $matches)) {
200 list($match, $action, $_, $job) = $matches;
201 break;
202 }
203 }
204
205 // CRM-5471: if $matches is empty, it still might be a soft bounce sent
206 // to another address, so scan the body for ‘Return-Path: …bounce-pattern…’
207 if (!$matches and preg_match($rpRegex, $mail->generateBody(), $matches)) {
208 list($match, $action, $job, $queue, $hash) = $matches;
209 }
210
1a4d92b6
DL
211 // if $matches is still empty, look for the X-CiviMail-Bounce header
212 // CRM-9855
213 if (!$matches and preg_match($rpXheaderRegex, $mail->generateBody(), $matches)) {
214 list($match, $action, $job, $queue, $hash) = $matches;
215 }
08523e94
O
216 // With Mandrilla, the X-CiviMail-Bounce header is produced by generateBody
217 // is base64 encoded
218 // Check all parts
219 if (!$matches) {
6ac3485f 220 $all_parts = $mail->fetchParts();
08523e94
O
221 foreach ($all_parts as $k_part => $v_part) {
222 if ($v_part instanceof ezcMailFile) {
223 $p_file = $v_part->__get('fileName');
6ac3485f 224 $c_file = file_get_contents($p_file);
08523e94 225 if (preg_match($rpXheaderRegex, $c_file, $matches)) {
08523e94
O
226 list($match, $action, $job, $queue, $hash) = $matches;
227 }
228 }
229 }
230 }
231
6a488035
TO
232 // if all else fails, check Delivered-To for possible pattern
233 if (!$matches and preg_match($regex, $mail->getHeader('Delivered-To'), $matches)) {
234 list($match, $action, $job, $queue, $hash) = $matches;
235 }
236 }
237
238 // preseve backward compatibility
239 if ($usedfor == 0 || !$civiMail) {
240 // if its the activities that needs to be processed ..
5fdd5f80 241 try {
83cd2236 242 $mailParams = CRM_Utils_Mail_Incoming::parseMailingObject($mail);
fad9031a 243 }
83cd2236
DH
244 catch (Exception $e) {
245 echo $e->getMessage();
246 $store->markIgnored($key);
247 continue;
248 }
6a488035 249
69c9ffb3 250 require_once 'CRM/Utils/DeprecatedUtils.php';
6a488035
TO
251 $params = _civicrm_api3_deprecated_activity_buildmailparams($mailParams, $emailActivityTypeId);
252
253 $params['version'] = 3;
254 $result = civicrm_api('activity', 'create', $params);
255
256 if ($result['is_error']) {
257 $matches = FALSE;
258 echo "Failed Processing: {$mail->subject}. Reason: {$result['error_message']}\n";
259 }
260 else {
261 $matches = TRUE;
262 echo "Processed as Activity: {$mail->subject}\n";
263 }
264
265 CRM_Utils_Hook::emailProcessor('activity', $params, $mail, $result);
266 }
267
268 // if $matches is empty, this email is not CiviMail-bound
269 if (!$matches) {
270 $store->markIgnored($key);
271 continue;
272 }
273
274 // get $replyTo from either the Reply-To header or from From
275 // FIXME: make sure it works with Reply-Tos containing non-email stuff
276 $replyTo = $mail->getHeader('Reply-To') ? $mail->getHeader('Reply-To') : $mail->from->email;
277
278 // handle the action by passing it to the proper API call
279 // FIXME: leave only one-letter cases when dropping legacy support
280 if (!empty($action)) {
281 $result = NULL;
282
283 switch ($action) {
284 case 'b':
285 case 'bounce':
286 $text = '';
287 if ($mail->body instanceof ezcMailText) {
288 $text = $mail->body->text;
289 }
290 elseif ($mail->body instanceof ezcMailMultipart) {
b606cd04 291 if ($mail->body instanceof ezcMailMultipartReport) {
9b9a8713
SL
292 $part = $mail->body->getMachinePart();
293 if ($part instanceof ezcMailDeliveryStatus) {
294 foreach ($part->recipients as $rec) {
295 if (isset($rec["Diagnostic-Code"])) {
296 $text = $rec["Diagnostic-Code"];
297 break;
298 }
0addad12
SL
299 elseif (isset($rec["Description"])) {
300 $text = $rec["Description"];
301 break;
302 }
303 // no diagnostic info present - try getting the human readable part
304 elseif (isset($rec["Status"])) {
305 $text = $rec["Status"];
306 $textpart = $mail->body->getReadablePart();
307 if ($textpart != NULL and isset($textpart->text)) {
308 $text .= " " . $textpart->text;
309 }
310 else {
311 $text .= " Delivery failed but no diagnostic code or description.";
312 }
313 break;
314 }
9b9a8713
SL
315 }
316 }
0addad12 317 elseif ($part != NULL and isset($part->text)) {
9b9a8713
SL
318 $text = $part->text;
319 }
abee52bc 320 elseif (($part = $mail->body->getReadablePart()) != NULL) {
9b9a8713
SL
321 $text = $part->text;
322 }
323 }
324 elseif ($mail->body instanceof ezcMailMultipartRelated) {
6a488035
TO
325 foreach ($mail->body->getRelatedParts() as $part) {
326 if (isset($part->subType) and $part->subType == 'plain') {
327 $text = $part->text;
328 break;
329 }
330 }
331 }
332 else {
333 foreach ($mail->body->getParts() as $part) {
334 if (isset($part->subType) and $part->subType == 'plain') {
335 $text = $part->text;
336 break;
337 }
338 }
339 }
340 }
341
342 if (
9b9a8713 343 empty($text) &&
6a488035
TO
344 $mail->subject == "Delivery Status Notification (Failure)"
345 ) {
346 // Exchange error - CRM-9361
347 foreach ($mail->body->getParts() as $part) {
348 if ($part instanceof ezcMailDeliveryStatus) {
349 foreach ($part->recipients as $rec) {
350 if ($rec["Status"] == "5.1.1") {
9b9a8713
SL
351 if (isset($rec["Description"])) {
352 $text = $rec["Description"];
353 }
354 else {
0addad12 355 $text = $rec["Status"] . " Delivery to the following recipients failed";
9b9a8713 356 }
6a488035
TO
357 break;
358 }
359 }
360 }
361 }
362 }
363
364 if (empty($text)) {
365 // If bounce processing fails, just take the raw body. Cf. CRM-11046
366 $text = $mail->generateBody();
367
368 // if text is still empty, lets fudge a blank text so the api call below will succeed
6ac9d864
DL
369 if (empty($text)) {
370 $text = ts('We could not extract the mail body from this bounce message.');
371 }
6a488035
TO
372 }
373
374 $params = array(
375 'job_id' => $job,
376 'event_queue_id' => $queue,
377 'hash' => $hash,
378 'body' => $text,
379 'version' => 3,
380 );
381 $result = civicrm_api('Mailing', 'event_bounce', $params);
382 break;
383
384 case 'c':
385 case 'confirm':
386 // CRM-7921
387 $params = array(
388 'contact_id' => $job,
389 'subscribe_id' => $queue,
390 'hash' => $hash,
391 'version' => 3,
392 );
393 $result = civicrm_api('Mailing', 'event_confirm', $params);
394 break;
395
396 case 'o':
397 case 'optOut':
398 $params = array(
399 'job_id' => $job,
400 'event_queue_id' => $queue,
401 'hash' => $hash,
402 'version' => 3,
403 );
404 $result = civicrm_api('MailingGroup', 'event_domain_unsubscribe', $params);
405 break;
406
407 case 'r':
408 case 'reply':
409 // instead of text and HTML parts (4th and 6th params) send the whole email as the last param
410 $params = array(
411 'job_id' => $job,
412 'event_queue_id' => $queue,
413 'hash' => $hash,
414 'bodyTxt' => NULL,
415 'replyTo' => $replyTo,
416 'bodyHTML' => NULL,
417 'fullEmail' => $mail->generate(),
418 'version' => 3,
419 );
420 $result = civicrm_api('Mailing', 'event_reply', $params);
421 break;
422
423 case 'e':
424 case 're':
425 case 'resubscribe':
426 $params = array(
427 'job_id' => $job,
428 'event_queue_id' => $queue,
429 'hash' => $hash,
430 'version' => 3,
431 );
432 $result = civicrm_api('MailingGroup', 'event_resubscribe', $params);
433 break;
434
435 case 's':
436 case 'subscribe':
437 $params = array(
438 'email' => $mail->from->email,
439 'group_id' => $job,
440 'version' => 3,
441 );
442 $result = civicrm_api('MailingGroup', 'event_subscribe', $params);
443 break;
444
445 case 'u':
446 case 'unsubscribe':
447 $params = array(
448 'job_id' => $job,
449 'event_queue_id' => $queue,
450 'hash' => $hash,
451 'version' => 3,
452 );
453 $result = civicrm_api('MailingGroup', 'event_unsubscribe', $params);
454 break;
455 }
456
457 if ($result['is_error']) {
458 echo "Failed Processing: {$mail->subject}, Action: $action, Job ID: $job, Queue ID: $queue, Hash: $hash. Reason: {$result['error_message']}\n";
459 }
460 else {
461 CRM_Utils_Hook::emailProcessor('mailing', $params, $mail, $result, $action);
462 }
463 }
464
465 $store->markProcessed($key);
466 }
467 // CRM-7356 – used by IMAP only
468 $store->expunge();
469 }
470 }
96025800 471
6a488035 472}