示例#1
0
/**
 * Convert an email file to an activity
 */
function civicrm_activity_processemail($file, $activityTypeID, $result = array())
{
    // do not parse if result array already passed (towards EmailProcessor..)
    if (empty($result)) {
        // might want to check that email is ok here
        if (!file_exists($file) || !is_readable($file)) {
            return CRM_Core_Error::createAPIError(ts('File %1 does not exist or is not readable', array(1 => $file)));
        }
    }
    require_once 'CRM/Utils/Mail/Incoming.php';
    $result = CRM_Utils_Mail_Incoming::parse($file);
    if ($result['is_error']) {
        return $result;
    }
    $params = _civicrm_activity_buildmailparams($result, $activityTypeID);
    return civicrm_activity_create($params);
}
 static function _process($civiMail, $dao)
 {
     // 0 = activities; 1 = bounce;
     $usedfor = $dao->is_default;
     require_once 'CRM/Core/OptionGroup.php';
     $emailActivityTypeId = defined('EMAIL_ACTIVITY_TYPE_ID') && EMAIL_ACTIVITY_TYPE_ID ? EMAIL_ACTIVITY_TYPE_ID : CRM_Core_OptionGroup::getValue('activity_type', 'Inbound Email', 'name');
     if (!$emailActivityTypeId) {
         CRM_Core_Error::fatal(ts('Could not find a valid Activity Type ID for Inbound Email'));
     }
     // FIXME: legacy regexen to handle CiviCRM 2.1 address patterns, with domain id and possible VERP part
     $commonRegex = '/^' . preg_quote($dao->localpart) . '(b|bounce|c|confirm|o|optOut|r|reply|re|e|resubscribe|u|unsubscribe)\\.(\\d+)\\.(\\d+)\\.(\\d+)\\.([0-9a-f]{16})(-.*)?@' . preg_quote($dao->domain) . '$/';
     $subscrRegex = '/^' . preg_quote($dao->localpart) . '(s|subscribe)\\.(\\d+)\\.(\\d+)@' . preg_quote($dao->domain) . '$/';
     // a common-for-all-actions regex to handle CiviCRM 2.2 address patterns
     $regex = '/^' . preg_quote($dao->localpart) . '(b|c|e|o|r|u)\\.(\\d+)\\.(\\d+)\\.([0-9a-f]{16})@' . preg_quote($dao->domain) . '$/';
     // retrieve the emails
     require_once 'CRM/Mailing/MailStore.php';
     $store = CRM_Mailing_MailStore::getStore($dao->name);
     require_once 'api/v2/Mailer.php';
     // process fifty at a time, CRM-4002
     while ($mails = $store->fetchNext(MAIL_BATCH_SIZE)) {
         foreach ($mails as $key => $mail) {
             // for every addressee: match address elements if it's to CiviMail
             $matches = array();
             if ($usedfor == 1) {
                 foreach ($mail->to as $address) {
                     if (preg_match($regex, $address->email, $matches)) {
                         list($match, $action, $job, $queue, $hash) = $matches;
                         break;
                         // FIXME: the below elseifs should be dropped when we drop legacy support
                     } elseif (preg_match($commonRegex, $address->email, $matches)) {
                         list($match, $action, $_, $job, $queue, $hash) = $matches;
                         break;
                     } elseif (preg_match($subscrRegex, $address->email, $matches)) {
                         list($match, $action, $_, $job) = $matches;
                         break;
                     }
                 }
             }
             // preseve backward compatibility
             if ($usedfor == 0 || !$civiMail) {
                 // if its the activities that needs to be processed ..
                 require_once 'CRM/Utils/Mail/Incoming.php';
                 $mailParams = CRM_Utils_Mail_Incoming::parseMailingObject($mail, $dao->name);
                 require_once 'api/v2/Activity.php';
                 $params = _civicrm_activity_buildmailparams($mailParams, $emailActivityTypeId);
                 $result = civicrm_activity_create($params);
                 if ($result['is_error']) {
                     $matches = false;
                     echo "Failed Processing: {$mail->subject}. Reason: {$result['error_message']}\n";
                 } else {
                     $matches = true;
                     echo "Processed as Activity: {$mail->subject}\n";
                 }
             }
             // if $matches is empty, this email is not CiviMail-bound
             if (!$matches) {
                 $store->markIgnored($key);
                 continue;
             }
             // get $replyTo from either the Reply-To header or from From
             // FIXME: make sure it works with Reply-Tos containing non-email stuff
             $replyTo = $mail->getHeader('Reply-To') ? $mail->getHeader('Reply-To') : $mail->from->email;
             // handle the action by passing it to the proper API call
             // FIXME: leave only one-letter cases when dropping legacy support
             if (!empty($action)) {
                 switch ($action) {
                     case 'b':
                     case 'bounce':
                         $text = '';
                         if ($mail->body instanceof ezcMailText) {
                             $text = $mail->body->text;
                         } elseif ($mail->body instanceof ezcMailMultipart) {
                             foreach ($mail->body->getParts() as $part) {
                                 if (isset($part->subType) and $part->subType == 'plain') {
                                     $text = $part->text;
                                     break;
                                 }
                             }
                         }
                         $params = array('job_id' => $job, 'event_queue_id' => $queue, 'hash' => $hash, 'body' => $text);
                         civicrm_mailer_event_bounce($params);
                         break;
                     case 'c':
                     case 'confirm':
                         $params = array('job_id' => $job, 'event_queue_id' => $queue, 'hash' => $hash);
                         civicrm_mailer_event_confirm($params);
                         break;
                     case 'o':
                     case 'optOut':
                         $params = array('job_id' => $job, 'event_queue_id' => $queue, 'hash' => $hash);
                         civicrm_mailer_event_domain_unsubscribe($params);
                         break;
                     case 'r':
                     case 'reply':
                         // instead of text and HTML parts (4th and 6th params) send the whole email as the last param
                         $params = array('job_id' => $job, 'event_queue_id' => $queue, 'hash' => $hash, 'bodyTxt' => null, 'replyTo' => $replyTo, 'bodyHTML' => null, 'fullEmail' => $mail->generate());
                         civicrm_mailer_event_reply($params);
                         break;
                     case 'e':
                     case 're':
                     case 'resubscribe':
                         $params = array('job_id' => $job, 'event_queue_id' => $queue, 'hash' => $hash);
                         civicrm_mailer_event_resubscribe($params);
                         break;
                     case 's':
                     case 'subscribe':
                         $params = array('email' => $mail->from->email, 'group_id' => $job);
                         civicrm_mailer_event_subscribe($params);
                         break;
                     case 'u':
                     case 'unsubscribe':
                         civicrm_mailer_event_unsubscribe($job, $queue, $hash);
                         break;
                 }
             }
             $store->markProcessed($key);
         }
     }
 }