fix formatting
[civicrm-core.git] / CRM / Utils / Mail / EmailProcessor.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2015
32 */
33
34 // we should consider moving these to the settings table
35 // before the 4.1 release
36 define('EMAIL_ACTIVITY_TYPE_ID', NULL);
37 define('MAIL_BATCH_SIZE', 50);
38
39 /**
40 * Class CRM_Utils_Mail_EmailProcessor.
41 */
42 class CRM_Utils_Mail_EmailProcessor {
43
44 /**
45 * Process the default mailbox (ie. that is used by civiMail for the bounce)
46 *
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.
50 */
51 public static function processBounces() {
52 $dao = new CRM_Core_DAO_MailSettings();
53 $dao->domain_id = CRM_Core_Config::domainID();
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 /**
66 * Delete old files from a given directory (recursively).
67 *
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).
72 */
73 public static function cleanupDir($dir, $age = 5184000) {
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 /**
95 * Process the mailboxes that aren't default (ie. that aren't used by civiMail for the bounce).
96 */
97 public static function processActivities() {
98 $dao = new CRM_Core_DAO_MailSettings();
99 $dao->domain_id = CRM_Core_Config::domainID();
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 /**
114 * Process the mailbox for all the settings from civicrm_mail_settings.
115 *
116 * @param bool|string $civiMail if true, processing is done in CiviMail context, or Activities otherwise.
117 */
118 public static function process($civiMail = TRUE) {
119 $dao = new CRM_Core_DAO_MailSettings();
120 $dao->domain_id = CRM_Core_Config::domainID();
121 $dao->find();
122
123 while ($dao->fetch()) {
124 self::_process($civiMail, $dao);
125 }
126 }
127
128 /**
129 * @param $civiMail
130 * @param CRM_Core_DAO $dao
131 *
132 * @throws Exception
133 */
134 public static function _process($civiMail, $dao) {
135 // 0 = activities; 1 = bounce;
136 $usedfor = $dao->is_default;
137
138 $emailActivityTypeId
139 = (defined('EMAIL_ACTIVITY_TYPE_ID') && EMAIL_ACTIVITY_TYPE_ID) ? EMAIL_ACTIVITY_TYPE_ID : CRM_Core_OptionGroup::getValue(
140 'activity_type',
141 'Inbound Email',
142 'name'
143 );
144
145 if (!$emailActivityTypeId) {
146 CRM_Core_Error::fatal(ts('Could not find a valid Activity Type ID for Inbound Email'));
147 }
148
149 $config = CRM_Core_Config::singleton();
150 $verpSeperator = preg_quote($config->verpSeparator);
151 $twoDigitStringMin = $verpSeperator . '(\d+)' . $verpSeperator . '(\d+)';
152 $twoDigitString = $twoDigitStringMin . $verpSeperator;
153 $threeDigitString = $twoDigitString . '(\d+)' . $verpSeperator;
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
165 // a regex for finding bound info X-Header
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 !!!
168
169 // retrieve the emails
170 try {
171 $store = CRM_Mailing_MailStore::getStore($dao->name);
172 }
173 catch (Exception$e) {
174 $message = ts('Could not connect to MailStore for ') . $dao->username . '@' . $dao->server . '<p>';
175 $message .= ts('Error message: ');
176 $message .= '<pre>' . $e->getMessage() . '</pre><p>';
177 CRM_Core_Error::fatal($message);
178 }
179
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
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 }
216 // With Mandrilla, the X-CiviMail-Bounce header is produced by generateBody
217 // is base64 encoded
218 // Check all parts
219 if (!$matches) {
220 $all_parts = $mail->fetchParts();
221 foreach ($all_parts as $k_part => $v_part) {
222 if ($v_part instanceof ezcMailFile) {
223 $p_file = $v_part->__get('fileName');
224 $c_file = file_get_contents($p_file);
225 if (preg_match($rpXheaderRegex, $c_file, $matches)) {
226 self::_log("file match rpXheaderRegex", $matches);
227 list($match, $action, $job, $queue, $hash) = $matches;
228 }
229 }
230 }
231 }
232
233 // if all else fails, check Delivered-To for possible pattern
234 if (!$matches and preg_match($regex, $mail->getHeader('Delivered-To'), $matches)) {
235 list($match, $action, $job, $queue, $hash) = $matches;
236 }
237 }
238
239 // preseve backward compatibility
240 if ($usedfor == 0 || !$civiMail) {
241 // if its the activities that needs to be processed ..
242 try {
243 $mailParams = CRM_Utils_Mail_Incoming::parseMailingObject($mail);
244 }
245 catch (Exception $e) {
246 echo $e->getMessage();
247 $store->markIgnored($key);
248 continue;
249 }
250
251 require_once 'CRM/Utils/DeprecatedUtils.php';
252 $params = _civicrm_api3_deprecated_activity_buildmailparams($mailParams, $emailActivityTypeId);
253
254 $params['version'] = 3;
255 $result = civicrm_api('activity', 'create', $params);
256
257 if ($result['is_error']) {
258 $matches = FALSE;
259 echo "Failed Processing: {$mail->subject}. Reason: {$result['error_message']}\n";
260 }
261 else {
262 $matches = TRUE;
263 echo "Processed as Activity: {$mail->subject}\n";
264 }
265
266 CRM_Utils_Hook::emailProcessor('activity', $params, $mail, $result);
267 }
268
269 // if $matches is empty, this email is not CiviMail-bound
270 if (!$matches) {
271 $store->markIgnored($key);
272 continue;
273 }
274
275 // get $replyTo from either the Reply-To header or from From
276 // FIXME: make sure it works with Reply-Tos containing non-email stuff
277 $replyTo = $mail->getHeader('Reply-To') ? $mail->getHeader('Reply-To') : $mail->from->email;
278
279 // handle the action by passing it to the proper API call
280 // FIXME: leave only one-letter cases when dropping legacy support
281 if (!empty($action)) {
282 $result = NULL;
283
284 switch ($action) {
285 case 'b':
286 case 'bounce':
287 $text = '';
288 if ($mail->body instanceof ezcMailText) {
289 $text = $mail->body->text;
290 }
291 elseif ($mail->body instanceof ezcMailMultipart) {
292 if ($mail->body instanceof ezcMailMultipartRelated) {
293 foreach ($mail->body->getRelatedParts() as $part) {
294 if (isset($part->subType) and $part->subType == 'plain') {
295 $text = $part->text;
296 break;
297 }
298 }
299 }
300 else {
301 foreach ($mail->body->getParts() as $part) {
302 if (isset($part->subType) and $part->subType == 'plain') {
303 $text = $part->text;
304 break;
305 }
306 }
307 }
308 }
309
310 if (
311 $text == NULL &&
312 $mail->subject == "Delivery Status Notification (Failure)"
313 ) {
314 // Exchange error - CRM-9361
315 foreach ($mail->body->getParts() as $part) {
316 if ($part instanceof ezcMailDeliveryStatus) {
317 foreach ($part->recipients as $rec) {
318 if ($rec["Status"] == "5.1.1") {
319 $text = "Delivery to the following recipients failed";
320 break;
321 }
322 }
323 }
324 }
325 }
326
327 if (empty($text)) {
328 // If bounce processing fails, just take the raw body. Cf. CRM-11046
329 $text = $mail->generateBody();
330
331 // if text is still empty, lets fudge a blank text so the api call below will succeed
332 if (empty($text)) {
333 $text = ts('We could not extract the mail body from this bounce message.');
334 }
335 }
336
337 $params = array(
338 'job_id' => $job,
339 'event_queue_id' => $queue,
340 'hash' => $hash,
341 'body' => $text,
342 'version' => 3,
343 );
344 $result = civicrm_api('Mailing', 'event_bounce', $params);
345 break;
346
347 case 'c':
348 case 'confirm':
349 // CRM-7921
350 $params = array(
351 'contact_id' => $job,
352 'subscribe_id' => $queue,
353 'hash' => $hash,
354 'version' => 3,
355 );
356 $result = civicrm_api('Mailing', 'event_confirm', $params);
357 break;
358
359 case 'o':
360 case 'optOut':
361 $params = array(
362 'job_id' => $job,
363 'event_queue_id' => $queue,
364 'hash' => $hash,
365 'version' => 3,
366 );
367 $result = civicrm_api('MailingGroup', 'event_domain_unsubscribe', $params);
368 break;
369
370 case 'r':
371 case 'reply':
372 // instead of text and HTML parts (4th and 6th params) send the whole email as the last param
373 $params = array(
374 'job_id' => $job,
375 'event_queue_id' => $queue,
376 'hash' => $hash,
377 'bodyTxt' => NULL,
378 'replyTo' => $replyTo,
379 'bodyHTML' => NULL,
380 'fullEmail' => $mail->generate(),
381 'version' => 3,
382 );
383 $result = civicrm_api('Mailing', 'event_reply', $params);
384 break;
385
386 case 'e':
387 case 're':
388 case 'resubscribe':
389 $params = array(
390 'job_id' => $job,
391 'event_queue_id' => $queue,
392 'hash' => $hash,
393 'version' => 3,
394 );
395 $result = civicrm_api('MailingGroup', 'event_resubscribe', $params);
396 break;
397
398 case 's':
399 case 'subscribe':
400 $params = array(
401 'email' => $mail->from->email,
402 'group_id' => $job,
403 'version' => 3,
404 );
405 $result = civicrm_api('MailingGroup', 'event_subscribe', $params);
406 break;
407
408 case 'u':
409 case 'unsubscribe':
410 $params = array(
411 'job_id' => $job,
412 'event_queue_id' => $queue,
413 'hash' => $hash,
414 'version' => 3,
415 );
416 $result = civicrm_api('MailingGroup', 'event_unsubscribe', $params);
417 break;
418 }
419
420 if ($result['is_error']) {
421 echo "Failed Processing: {$mail->subject}, Action: $action, Job ID: $job, Queue ID: $queue, Hash: $hash. Reason: {$result['error_message']}\n";
422 }
423 else {
424 CRM_Utils_Hook::emailProcessor('mailing', $params, $mail, $result, $action);
425 }
426 }
427
428 $store->markProcessed($key);
429 }
430 // CRM-7356 – used by IMAP only
431 $store->expunge();
432 }
433 }
434
435 }