3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2015
33 class CRM_Utils_Mail_Incoming
{
35 EMAILPROCESSOR_CREATE_INDIVIDUAL
= 1,
36 EMAILPROCESSOR_OVERRIDE
= 2,
37 EMAILPROCESSOR_IGNORE
= 3;
45 public function formatMail($mail, &$attachments) {
47 $t .= "From: " . self
::formatAddress($mail->from
) . "\n";
48 $t .= "To: " . self
::formatAddresses($mail->to
) . "\n";
49 $t .= "Cc: " . self
::formatAddresses($mail->cc
) . "\n";
50 $t .= "Bcc: " . self
::formatAddresses($mail->bcc
) . "\n";
51 $t .= 'Date: ' . date(DATE_RFC822
, $mail->timestamp
) . "\n";
52 $t .= 'Subject: ' . $mail->subject
. "\n";
53 $t .= "MessageId: " . $mail->messageId
. "\n";
55 $t .= self
::formatMailPart($mail->body
, $attachments);
65 public static function formatMailPart($part, &$attachments) {
66 if ($part instanceof ezcMail
) {
67 return self
::formatMail($part, $attachments);
70 if ($part instanceof ezcMailText
) {
71 return self
::formatMailText($part, $attachments);
74 if ($part instanceof ezcMailFile
) {
75 return self
::formatMailFile($part, $attachments);
78 if ($part instanceof ezcMailRfc822Digest
) {
79 return self
::formatMailRfc822Digest($part, $attachments);
82 if ($part instanceof ezcMailMultiPart
) {
83 return self
::formatMailMultipart($part, $attachments);
86 CRM_Core_Error
::fatal(ts("No clue about the %1", array(1 => get_class($part))));
95 public function formatMailMultipart($part, &$attachments) {
96 if ($part instanceof ezcMailMultiPartAlternative
) {
97 return self
::formatMailMultipartAlternative($part, $attachments);
100 if ($part instanceof ezcMailMultiPartDigest
) {
101 return self
::formatMailMultipartDigest($part, $attachments);
104 if ($part instanceof ezcMailMultiPartRelated
) {
105 return self
::formatMailMultipartRelated($part, $attachments);
108 if ($part instanceof ezcMailMultiPartMixed
) {
109 return self
::formatMailMultipartMixed($part, $attachments);
112 if ($part instanceof ezcMailMultipartReport
) {
113 return self
::formatMailMultipartReport($part, $attachments);
116 CRM_Core_Error
::fatal(ts("No clue about the %1", array(1 => get_class($part))));
121 * @param $attachments
125 public function formatMailMultipartMixed($part, &$attachments) {
127 foreach ($part->getParts() as $key => $alternativePart) {
128 $t .= self
::formatMailPart($alternativePart, $attachments);
135 * @param $attachments
139 public function formatMailMultipartRelated($part, &$attachments) {
141 $t .= "-RELATED MAIN PART-\n";
142 $t .= self
::formatMailPart($part->getMainPart(), $attachments);
143 foreach ($part->getRelatedParts() as $key => $alternativePart) {
144 $t .= "-RELATED PART $key-\n";
145 $t .= self
::formatMailPart($alternativePart, $attachments);
147 $t .= "-RELATED END-\n";
153 * @param $attachments
157 public function formatMailMultipartDigest($part, &$attachments) {
159 foreach ($part->getParts() as $key => $alternativePart) {
160 $t .= "-DIGEST-$key-\n";
161 $t .= self
::formatMailPart($alternativePart, $attachments);
163 $t .= "-DIGEST END---\n";
169 * @param $attachments
173 public function formatMailRfc822Digest($part, &$attachments) {
175 $t .= "-DIGEST-ITEM-\n";
177 $t .= self
::formatMailpart($part->mail
, $attachments);
178 $t .= "-DIGEST ITEM END-\n";
184 * @param $attachments
188 public function formatMailMultipartAlternative($part, &$attachments) {
190 foreach ($part->getParts() as $key => $alternativePart) {
191 $t .= "-ALTERNATIVE ITEM $key-\n";
192 $t .= self
::formatMailPart($alternativePart, $attachments);
194 $t .= "-ALTERNATIVE END-\n";
200 * @param $attachments
204 public static function formatMailText($part, &$attachments) {
205 $t = "\n{$part->text}\n";
211 * @param $attachments
215 public function formatMailMultipartReport($part, &$attachments) {
217 foreach ($part->getParts() as $key => $reportPart) {
218 $t .= "-REPORT-$key-\n";
219 $t .= self
::formatMailPart($reportPart, $attachments);
221 $t .= "-REPORT END---\n";
227 * @param $attachments
231 public function formatMailFile($part, &$attachments) {
232 $attachments[] = array(
233 'dispositionType' => $part->dispositionType
,
234 'contentType' => $part->contentType
,
235 'mimeType' => $part->mimeType
,
236 'contentID' => $part->contentId
,
237 'fullName' => $part->fileName
,
247 public function formatAddresses($addresses) {
249 foreach ($addresses as $address) {
250 $fa[] = self
::formatAddress($address);
252 return implode(', ', $fa);
260 public function formatAddress($address) {
262 if (!empty($address->name
)) {
263 $name = "{$address->name} ";
265 return $name . "<{$address->email}>";
274 public function &parse(&$file) {
276 // check that the file exists and has some content
277 if (!file_exists($file) ||
278 !trim(file_get_contents($file))
280 return CRM_Core_Error
::createAPIError(ts('%1 does not exists or is empty',
285 require_once 'ezc/Base/src/ezc_bootstrap.php';
286 require_once 'ezc/autoload/mail_autoload.php';
288 // explode email to digestable format
289 $set = new ezcMailFileSet(array($file));
290 $parser = new ezcMailParser();
291 $mail = $parser->parseMail($set);
294 return CRM_Core_Error
::createAPIError(ts('%1 could not be parsed',
299 // since we only have one fileset
302 $mailParams = self
::parseMailingObject($mail);
311 public static function parseMailingObject(&$mail) {
313 $config = CRM_Core_Config
::singleton();
315 // get ready for collecting data about this email
316 // and put it in a standardized format
317 $params = array('is_error' => 0);
319 $params['from'] = array();
320 self
::parseAddress($mail->from
, $field, $params['from'], $mail);
322 // we definitely need a contact id for the from address
323 // if we dont have one, skip this email
324 if (empty($params['from']['id'])) {
328 $emailFields = array('to', 'cc', 'bcc');
329 foreach ($emailFields as $field) {
330 $value = $mail->$field;
331 self
::parseAddresses($value, $field, $params, $mail);
334 // define other parameters
335 $params['subject'] = $mail->subject
;
336 $params['date'] = date("YmdHi00",
337 strtotime($mail->getHeader("Date"))
339 $attachments = array();
340 $params['body'] = self
::formatMailPart($mail->body
, $attachments);
342 // format and move attachments to the civicrm area
343 if (!empty($attachments)) {
344 $date = date('Ymdhis');
345 $config = CRM_Core_Config
::singleton();
346 for ($i = 0; $i < count($attachments); $i++
) {
348 $fileName = basename($attachments[$i]['fullName']);
349 $newName = CRM_Utils_File
::makeFileName($fileName);
350 $location = $config->uploadDir
. $newName;
352 // move file to the civicrm upload directory
353 rename($attachments[$i]['fullName'], $location);
355 $mimeType = "{$attachments[$i]['contentType']}/{$attachments[$i]['mimeType']}";
357 $params["attachFile_$attachNum"] = array(
360 'upload_date' => $date,
361 'location' => $location,
371 * @param array $params
375 public static function parseAddress(&$address, &$params, &$subParam, &$mail) {
377 if (empty($address->email
)) {
381 $subParam['email'] = $address->email
;
382 $subParam['name'] = $address->name
;
384 $contactID = self
::getContactID($subParam['email'],
389 $subParam['id'] = $contactID ?
$contactID : NULL;
395 * @param array $params
398 public static function parseAddresses(&$addresses, $token, &$params, &$mail) {
399 $params[$token] = array();
401 foreach ($addresses as $address) {
403 self
::parseAddress($address, $params, $subParam, $mail);
404 $params[$token][] = $subParam;
409 * Retrieve a contact ID and if not present.
411 * Create one with this email
413 * @param string $email
414 * @param string $name
415 * @param bool $create
416 * @param string $mail
420 public static function getContactID($email, $name = NULL, $create = TRUE, &$mail) {
421 $dao = CRM_Contact_BAO_Contact
::matchContactOnEmail($email, 'Individual');
425 $contactID = $dao->contact_id
;
429 CRM_Utils_Hook
::emailProcessorContact($email, $contactID, $result);
431 if (!empty($result)) {
432 if ($result['action'] == self
::EMAILPROCESSOR_IGNORE
) {
435 if ($result['action'] == self
::EMAILPROCESSOR_OVERRIDE
) {
436 return $result['contactID'];
439 // else this is now create individual
440 // so we just fall out and do what we normally do
451 // contact does not exist, lets create it
453 'contact_type' => 'Individual',
454 'email-Primary' => $email,
457 CRM_Utils_String
::extractName($name, $params);
459 return CRM_Contact_BAO_Contact
::createProfileContact($params,
460 CRM_Core_DAO
::$_nullArray