3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
35 class CRM_Utils_Mail_Incoming
{
37 EMAILPROCESSOR_CREATE_INDIVIDUAL
= 1,
38 EMAILPROCESSOR_OVERRIDE
= 2,
39 EMAILPROCESSOR_IGNORE
= 3;
41 function formatMail($mail, &$attachments) {
43 $t .= "From: " . self
::formatAddress($mail->from
) . "\n";
44 $t .= "To: " . self
::formatAddresses($mail->to
) . "\n";
45 $t .= "Cc: " . self
::formatAddresses($mail->cc
) . "\n";
46 $t .= "Bcc: " . self
::formatAddresses($mail->bcc
) . "\n";
47 $t .= 'Date: ' . date(DATE_RFC822
, $mail->timestamp
) . "\n";
48 $t .= 'Subject: ' . $mail->subject
. "\n";
49 $t .= "MessageId: " . $mail->messageId
. "\n";
51 $t .= self
::formatMailPart($mail->body
, $attachments);
55 function formatMailPart($part, &$attachments) {
56 if ($part instanceof ezcMail
) {
57 return self
::formatMail($part, $attachments);
60 if ($part instanceof ezcMailText
) {
61 return self
::formatMailText($part, $attachments);
64 if ($part instanceof ezcMailFile
) {
65 return self
::formatMailFile($part, $attachments);
68 if ($part instanceof ezcMailRfc822Digest
) {
69 return self
::formatMailRfc822Digest($part, $attachments);
72 if ($part instanceof ezcMailMultiPart
) {
73 return self
::formatMailMultipart($part, $attachments);
76 CRM_Core_Error
::fatal(ts("No clue about the %1", array(1 => get_class($part))));
79 function formatMailMultipart($part, &$attachments) {
80 if ($part instanceof ezcMailMultiPartAlternative
) {
81 return self
::formatMailMultipartAlternative($part, $attachments);
84 if ($part instanceof ezcMailMultiPartDigest
) {
85 return self
::formatMailMultipartDigest($part, $attachments);
88 if ($part instanceof ezcMailMultiPartRelated
) {
89 return self
::formatMailMultipartRelated($part, $attachments);
92 if ($part instanceof ezcMailMultiPartMixed
) {
93 return self
::formatMailMultipartMixed($part, $attachments);
96 if ($part instanceof ezcMailMultipartReport
) {
97 return self
::formatMailMultipartReport($part, $attachments);
100 CRM_Core_Error
::fatal(ts("No clue about the %1", array(1 => get_class($part))));
103 function formatMailMultipartMixed($part, &$attachments) {
105 foreach ($part->getParts() as $key => $alternativePart) {
106 $t .= self
::formatMailPart($alternativePart, $attachments);
111 function formatMailMultipartRelated($part, &$attachments) {
113 $t .= "-RELATED MAIN PART-\n";
114 $t .= self
::formatMailPart($part->getMainPart(), $attachments);
115 foreach ($part->getRelatedParts() as $key => $alternativePart) {
116 $t .= "-RELATED PART $key-\n";
117 $t .= self
::formatMailPart($alternativePart, $attachments);
119 $t .= "-RELATED END-\n";
123 function formatMailMultipartDigest($part, &$attachments) {
125 foreach ($part->getParts() as $key => $alternativePart) {
126 $t .= "-DIGEST-$key-\n";
127 $t .= self
::formatMailPart($alternativePart, $attachments);
129 $t .= "-DIGEST END---\n";
133 function formatMailRfc822Digest($part, &$attachments) {
135 $t .= "-DIGEST-ITEM-\n";
137 $t .= self
::formatMailpart($part->mail
, $attachments);
138 $t .= "-DIGEST ITEM END-\n";
142 function formatMailMultipartAlternative($part, &$attachments) {
144 foreach ($part->getParts() as $key => $alternativePart) {
145 $t .= "-ALTERNATIVE ITEM $key-\n";
146 $t .= self
::formatMailPart($alternativePart, $attachments);
148 $t .= "-ALTERNATIVE END-\n";
152 function formatMailText($part, &$attachments) {
153 $t = "\n{$part->text}\n";
157 function formatMailMultipartReport($part, &$attachments) {
159 foreach ($part->getParts() as $key => $reportPart) {
160 $t .= "-REPORT-$key-\n";
161 $t .= self
::formatMailPart($reportPart, $attachments);
163 $t .= "-REPORT END---\n";
167 function formatMailFile($part, &$attachments) {
168 $attachments[] = array(
169 'dispositionType' => $part->dispositionType
,
170 'contentType' => $part->contentType
,
171 'mimeType' => $part->mimeType
,
172 'contentID' => $part->contentId
,
173 'fullName' => $part->fileName
,
178 function formatAddresses($addresses) {
180 foreach ($addresses as $address) {
181 $fa[] = self
::formatAddress($address);
183 return implode(', ', $fa);
186 function formatAddress($address) {
188 if (!empty($address->name
)) {
189 $name = "{$address->name} ";
191 return $name . "<{$address->email}>";
194 function &parse(&$file) {
196 // check that the file exists and has some content
197 if (!file_exists($file) ||
198 !trim(file_get_contents($file))
200 return CRM_Core_Error
::createAPIError(ts('%1 does not exists or is empty',
205 require_once 'ezc/Base/src/ezc_bootstrap.php';
206 require_once 'ezc/autoload/mail_autoload.php';
208 // explode email to digestable format
209 $set = new ezcMailFileSet(array($file));
210 $parser = new ezcMailParser();
211 $mail = $parser->parseMail($set);
214 return CRM_Core_Error
::createAPIError(ts('%1 could not be parsed',
219 // since we only have one fileset
222 $mailParams = self
::parseMailingObject($mail);
226 function parseMailingObject(&$mail) {
228 $config = CRM_Core_Config
::singleton();
230 // get ready for collecting data about this email
231 // and put it in a standardized format
232 $params = array('is_error' => 0);
234 $params['from'] = array();
235 self
::parseAddress($mail->from
, $field, $params['from'], $mail);
237 // we definitely need a contact id for the from address
238 // if we dont have one, skip this email
239 if (empty($params['from']['id'])) {
243 $emailFields = array('to', 'cc', 'bcc');
244 foreach ($emailFields as $field) {
245 $value = $mail->$field;
246 self
::parseAddresses($value, $field, $params, $mail);
249 // define other parameters
250 $params['subject'] = $mail->subject
;
251 $params['date'] = date("YmdHi00",
252 strtotime($mail->getHeader("Date"))
254 $attachments = array();
255 $params['body'] = self
::formatMailPart($mail->body
, $attachments);
257 // format and move attachments to the civicrm area
258 if (!empty($attachments)) {
259 $date = date('Ymdhis');
260 $config = CRM_Core_Config
::singleton();
261 for ($i = 0; $i < count($attachments); $i++
) {
263 $fileName = basename($attachments[$i]['fullName']);
264 $newName = CRM_Utils_File
::makeFileName($fileName);
265 $location = $config->uploadDir
. $newName;
267 // move file to the civicrm upload directory
268 rename($attachments[$i]['fullName'], $location);
270 $mimeType = "{$attachments[$i]['contentType']}/{$attachments[$i]['mimeType']}";
272 $params["attachFile_$attachNum"] = array(
275 'upload_date' => $date,
276 'location' => $location,
284 function parseAddress(&$address, &$params, &$subParam, &$mail) {
286 if (empty($address->email
)) {
290 $subParam['email'] = $address->email
;
291 $subParam['name'] = $address->name
;
294 $contactID = self
::getContactID($subParam['email'],
299 $subParam['id'] = $contactID ?
$contactID : NULL;
302 function parseAddresses(&$addresses, $token, &$params, &$mail) {
303 $params[$token] = array();
305 foreach ($addresses as $address) {
307 self
::parseAddress($address, $params, $subParam, $mail);
308 $params[$token][] = $subParam;
313 * retrieve a contact ID and if not present
314 * create one with this email
316 function getContactID($email, $name = NULL, $create = TRUE, &$mail) {
317 $dao = CRM_Contact_BAO_Contact
::matchContactOnEmail($email, 'Individual');
321 $contactID = $dao->contact_id
;
325 CRM_Utils_Hook
::emailProcessorContact($email, $contactID, $result);
327 if (!empty($result)) {
328 if ($result['action'] == self
::EMAILPROCESSOR_IGNORE
) {
331 if ($result['action'] == self
::EMAILPROCESSOR_OVERRIDE
) {
332 return $result['contactID'];
335 // else this is now create individual
336 // so we just fall out and do what we normally do
347 // contact does not exist, lets create it
349 'contact_type' => 'Individual',
350 'email-Primary' => $email,
353 CRM_Utils_String
::extractName($name, $params);
355 return CRM_Contact_BAO_Contact
::createProfileContact($params,
356 CRM_Core_DAO
::$_nullArray