Пример #1
0
/**
 * Process a bounce event by passing through to the BAOs.
 *
 * @param int $job          ID of the job that caused this bounce
 * @param int $queue        ID of the queue event that bounced
 * @param string $hash      Security hash
 * @param string $body      Body of the bounce message
 * @return boolean
 */
function crm_mailer_event_bounce($job, $queue, $hash, $body)
{
    $params = CRM_Mailing_BAO_BouncePattern::match($body);
    $params += array('job_id' => $job, 'event_queue_id' => $queue, 'hash' => $hash);
    CRM_Mailing_Event_BAO_Bounce::create($params);
    return true;
}
Пример #2
0
 /**
  * Build the static pattern array.
  */
 public static function buildPatterns()
 {
     self::$_patterns = array();
     $bp = new CRM_Mailing_BAO_BouncePattern();
     $bp->find();
     while ($bp->fetch()) {
         self::$_patterns[$bp->bounce_type_id][] = $bp->pattern;
     }
     foreach (self::$_patterns as $type => $patterns) {
         if (count($patterns) == 1) {
             self::$_patterns[$type] = '{(' . $patterns[0] . ')}im';
         } else {
             self::$_patterns[$type] = '{(' . implode(')|(', $patterns) . ')}im';
         }
     }
 }
/**
 * Process a bounce event by passing through to the BAOs.
 *
 * @param array $params
 *
 * @return array
 */
function civicrm_api3_mailing_event_bounce($params)
{
    civicrm_api3_verify_mandatory($params, 'CRM_Mailing_Event_DAO_Bounce', array('job_id', 'event_queue_id', 'hash', 'body'), FALSE);
    $body = $params['body'];
    unset($params['body']);
    $params += CRM_Mailing_BAO_BouncePattern::match($body);
    if (CRM_Mailing_Event_BAO_Bounce::create($params)) {
        return civicrm_api3_create_success($params);
    } else {
        return civicrm_api3_create_error('Queue event could not be found');
    }
}
Пример #4
0
/**
 * Process a bounce event by passing through to the BAOs.
 *
 * @param array $params
 *
 * @return array
 */
function civicrm_mailer_event_bounce($params)
{
    $errors = _civicrm_mailer_check_params($params, array('job_id', 'event_queue_id', 'hash', 'body'));
    if (!empty($errors)) {
        return $errors;
    }
    $body = $params['body'];
    unset($params['body']);
    $params += CRM_Mailing_BAO_BouncePattern::match($body);
    if (CRM_Mailing_Event_BAO_Bounce::create($params)) {
        return civicrm_create_success();
    }
    return civicrm_create_error(ts('Queue event could not be found'));
}
Пример #5
0
 /**
  * @param $fields
  * @param $mailing
  * @param $mailer
  * @param $job_date
  * @param $attachments
  *
  * @return bool|null
  * @throws Exception
  */
 public function deliverGroup(&$fields, &$mailing, &$mailer, &$job_date, &$attachments)
 {
     static $smtpConnectionErrors = 0;
     if (!is_object($mailer) || empty($fields)) {
         CRM_Core_Error::fatal();
     }
     // get the return properties
     $returnProperties = $mailing->getReturnProperties();
     $params = $targetParams = $deliveredParams = array();
     $count = 0;
     /**
      * CRM-15702: Sending bulk sms to contacts without e-mail address fails.
      * Solution is to skip checking for on hold
      */
     $skipOnHold = TRUE;
     //do include a statement to check wether e-mail address is on hold
     if ($mailing->sms_provider_id) {
         $skipOnHold = FALSE;
         //do not include a statement to check wether e-mail address is on hold
     }
     foreach ($fields as $key => $field) {
         $params[] = $field['contact_id'];
     }
     $details = CRM_Utils_Token::getTokenDetails($params, $returnProperties, $skipOnHold, TRUE, NULL, $mailing->getFlattenedTokens(), get_class($this), $this->id);
     $config = CRM_Core_Config::singleton();
     foreach ($fields as $key => $field) {
         $contactID = $field['contact_id'];
         if (!array_key_exists($contactID, $details[0])) {
             $details[0][$contactID] = array();
         }
         /* Compose the mailing */
         $recipient = $replyToEmail = NULL;
         $replyValue = strcmp($mailing->replyto_email, $mailing->from_email);
         if ($replyValue) {
             $replyToEmail = $mailing->replyto_email;
         }
         $message =& $mailing->compose($this->id, $field['id'], $field['hash'], $field['contact_id'], $field['email'], $recipient, FALSE, $details[0][$contactID], $attachments, FALSE, NULL, $replyToEmail);
         if (empty($message)) {
             // lets keep the message in the queue
             // most likely a permissions related issue with smarty templates
             // or a bad contact id? CRM-9833
             continue;
         }
         /* Send the mailing */
         $body =& $message->get();
         $headers =& $message->headers();
         if ($mailing->sms_provider_id) {
             $provider = CRM_SMS_Provider::singleton(array('mailing_id' => $mailing->id));
             $body = $provider->getMessage($message, $field['contact_id'], $details[0][$contactID]);
             $headers = $provider->getRecipientDetails($field, $details[0][$contactID]);
         }
         // make $recipient actually be the *encoded* header, so as not to baffle Mail_RFC822, CRM-5743
         $recipient = $headers['To'];
         $result = NULL;
         // disable error reporting on real mailings (but leave error reporting for tests), CRM-5744
         if ($job_date) {
             $errorScope = CRM_Core_TemporaryErrorScope::ignoreException();
         }
         $result = $mailer->send($recipient, $headers, $body, $this->id);
         if ($job_date) {
             unset($errorScope);
         }
         if (is_a($result, 'PEAR_Error') && !$mailing->sms_provider_id) {
             // CRM-9191
             $message = $result->getMessage();
             if (strpos($message, 'Failed to write to socket') !== FALSE || strpos($message, 'Failed to set sender') !== FALSE) {
                 // lets log this message and code
                 $code = $result->getCode();
                 CRM_Core_Error::debug_log_message("SMTP Socket Error or failed to set sender error. Message: {$message}, Code: {$code}");
                 // these are socket write errors which most likely means smtp connection errors
                 // lets skip them
                 $smtpConnectionErrors++;
                 if ($smtpConnectionErrors <= 5) {
                     continue;
                 }
                 // seems like we have too many of them in a row, we should
                 // write stuff to disk and abort the cron job
                 $this->writeToDB($deliveredParams, $targetParams, $mailing, $job_date);
                 CRM_Core_Error::debug_log_message("Too many SMTP Socket Errors. Exiting");
                 CRM_Utils_System::civiExit();
             }
             /* Register the bounce event */
             $params = array('event_queue_id' => $field['id'], 'job_id' => $this->id, 'hash' => $field['hash']);
             $params = array_merge($params, CRM_Mailing_BAO_BouncePattern::match($result->getMessage()));
             CRM_Mailing_Event_BAO_Bounce::create($params);
         } elseif (is_a($result, 'PEAR_Error') && $mailing->sms_provider_id) {
             // Handle SMS errors: CRM-15426
             $job_id = intval($this->id);
             $mailing_id = intval($mailing->id);
             CRM_Core_Error::debug_log_message("Failed to send SMS message. Vars: mailing_id: {$mailing_id}, job_id: {$job_id}. Error message follows.");
             CRM_Core_Error::debug_log_message($result->getMessage());
         } else {
             /* Register the delivery event */
             $deliveredParams[] = $field['id'];
             $targetParams[] = $field['contact_id'];
             $count++;
             if ($count % CRM_Core_DAO::BULK_MAIL_INSERT_COUNT == 0) {
                 $this->writeToDB($deliveredParams, $targetParams, $mailing, $job_date);
                 $count = 0;
                 // hack to stop mailing job at run time, CRM-4246.
                 // to avoid making too many DB calls for this rare case
                 // lets do it when we snapshot
                 $status = CRM_Core_DAO::getFieldValue('CRM_Mailing_DAO_MailingJob', $this->id, 'status', 'id', TRUE);
                 if ($status != 'Running') {
                     return FALSE;
                 }
             }
         }
         unset($result);
         // seems like a successful delivery or bounce, lets decrement error count
         // only if we have smtp connection errors
         if ($smtpConnectionErrors > 0) {
             $smtpConnectionErrors--;
         }
         // If we have enabled the Throttle option, this is the time to enforce it.
         if (isset($config->mailThrottleTime) && $config->mailThrottleTime > 0) {
             usleep((int) $config->mailThrottleTime);
         }
     }
     $result = $this->writeToDB($deliveredParams, $targetParams, $mailing, $job_date);
     return $result;
 }
Пример #6
0
 /**
  * Create a new forward event, create a new contact if necessary
  */
 static function &forward($job_id, $queue_id, $hash, $forward_email, $fromEmail = null, $comment = null)
 {
     $q =& CRM_Mailing_Event_BAO_Queue::verify($job_id, $queue_id, $hash);
     $successfulForward = false;
     if (!$q) {
         return $successfulForward;
     }
     /* Find the email address/contact, if it exists */
     $contact = CRM_Contact_BAO_Contact::getTableName();
     $location = CRM_Core_BAO_Location::getTableName();
     $email = CRM_Core_BAO_Email::getTableName();
     $queueTable = CRM_Mailing_Event_BAO_Queue::getTableName();
     $job = CRM_Mailing_BAO_Job::getTableName();
     $mailing = CRM_Mailing_BAO_Mailing::getTableName();
     $forward = self::getTableName();
     $domain =& CRM_Core_BAO_Domain::getDomain();
     $dao =& new CRM_Core_Dao();
     $dao->query("\n                SELECT      {$contact}.id as contact_id,\n                            {$email}.id as email_id,\n                            {$contact}.do_not_email as do_not_email,\n                            {$queueTable}.id as queue_id\n                FROM        ({$email}, {$job} as temp_job)\n                INNER JOIN  {$contact}\n                        ON  {$email}.contact_id = {$contact}.id\n                LEFT JOIN   {$queueTable}\n                        ON  {$email}.id = {$queueTable}.email_id\n                LEFT JOIN   {$job}\n                        ON  {$queueTable}.job_id = {$job}.id\n                        AND temp_job.mailing_id = {$job}.mailing_id\n                WHERE       {$queueTable}.job_id = {$job_id}\n                    AND     {$email}.email = '" . CRM_Utils_Type::escape($forward_email, 'String') . "'");
     $dao->fetch();
     require_once 'CRM/Core/Transaction.php';
     $transaction = new CRM_Core_Transaction();
     if (isset($dao->queue_id) || $dao->do_not_email == 1) {
         /* We already sent this mailing to $forward_email, or we should
          * never email this contact.  Give up. */
         return $successfulForward;
     }
     require_once 'api/v2/Contact.php';
     $contact_params = array('email' => $forward_email);
     $count = civicrm_contact_search_count($contact_params);
     if ($count == 0) {
         require_once 'CRM/Core/BAO/LocationType.php';
         /* If the contact does not exist, create one. */
         $formatted = array('contact_type' => 'Individual');
         $locationType = CRM_Core_BAO_LocationType::getDefault();
         $value = array('email' => $forward_email, 'location_type_id' => $locationType->id);
         _civicrm_add_formatted_param($value, $formatted);
         require_once 'CRM/Import/Parser.php';
         $formatted['onDuplicate'] = CRM_Import_Parser::DUPLICATE_SKIP;
         $formatted['fixAddress'] = true;
         $contact =& civicrm_contact_format_create($formatted);
         if (civicrm_error($contact, CRM_Core_Error)) {
             return $successfulForward;
         }
         $contact_id = $contact['id'];
     }
     $email =& new CRM_Core_DAO_Email();
     $email->email = $forward_email;
     $email->find(true);
     $email_id = $email->id;
     if (!$contact_id) {
         $contact_id = $email->contact_id;
     }
     /* Create a new queue event */
     $queue_params = array('email_id' => $email_id, 'contact_id' => $contact_id, 'job_id' => $job_id);
     $queue =& CRM_Mailing_Event_BAO_Queue::create($queue_params);
     $forward =& new CRM_Mailing_Event_BAO_Forward();
     $forward->time_stamp = date('YmdHis');
     $forward->event_queue_id = $queue_id;
     $forward->dest_queue_id = $queue->id;
     $forward->save();
     $dao->reset();
     $dao->query("   SELECT  {$job}.mailing_id as mailing_id \n                        FROM    {$job}\n                        WHERE   {$job}.id = " . CRM_Utils_Type::escape($job_id, 'Integer'));
     $dao->fetch();
     $mailing_obj =& new CRM_Mailing_BAO_Mailing();
     $mailing_obj->id = $dao->mailing_id;
     $mailing_obj->find(true);
     $config =& CRM_Core_Config::singleton();
     $mailer =& $config->getMailer();
     $recipient = null;
     $attachments = null;
     $message =& $mailing_obj->compose($job_id, $queue->id, $queue->hash, $queue->contact_id, $forward_email, $recipient, false, null, $attachments, true, $fromEmail);
     //append comment if added while forwarding.
     if (count($comment)) {
         $message->_txtbody = $comment['body_text'] . $message->_txtbody;
         if (CRM_Utils_Array::value('body_html', $comment)) {
             $message->_htmlbody = $comment['body_html'] . '<br />---------------Original message---------------------<br />' . $message->_htmlbody;
         }
     }
     $body = $message->get();
     $headers = $message->headers();
     PEAR::setErrorHandling(PEAR_ERROR_CALLBACK, array('CRM_Core_Error', 'nullHandler'));
     $result = null;
     if (is_object($mailer)) {
         $result = $mailer->send($recipient, $headers, $body);
         CRM_Core_Error::setCallback();
     }
     $params = array('event_queue_id' => $queue->id, 'job_id' => $job_id, 'hash' => $queue->hash);
     if (is_a($result, PEAR_Error)) {
         /* Register the bounce event */
         $params = array_merge($params, CRM_Mailing_BAO_BouncePattern::match($result->getMessage()));
         CRM_Mailing_Event_BAO_Bounce::create($params);
     } else {
         $successfulForward = true;
         /* Register the delivery event */
         CRM_Mailing_Event_BAO_Delivered::create($params);
     }
     $transaction->commit();
     return $successfulForward;
 }
Пример #7
0
/**
 * Process a bounce event by passing through to the BAOs.
 *
 * @param array $params
 *
 * @throws API_Exception
 * @return array
 */
function civicrm_api3_mailing_event_bounce($params)
{
    $body = $params['body'];
    unset($params['body']);
    $params += CRM_Mailing_BAO_BouncePattern::match($body);
    if (CRM_Mailing_Event_BAO_Bounce::create($params)) {
        return civicrm_api3_create_success($params);
    } else {
        throw new API_Exception(ts('Queue event could not be found'), 'no_queue_event');
    }
}
Пример #8
0
 /**
  * Send the mailing
  *
  * @param object $mailer        A Mail object to send the messages
  * @return void
  * @access public
  */
 function deliver(&$mailer)
 {
     require_once 'CRM/Mailing/BAO/Mailing.php';
     $mailing =& new CRM_Mailing_BAO_Mailing();
     $mailing->id = $this->mailing_id;
     $mailing->find(true);
     $eq =& new CRM_Mailing_Event_BAO_Queue();
     $eqTable = CRM_Mailing_Event_BAO_Queue::getTableName();
     $emailTable = CRM_Core_BAO_Email::getTableName();
     $contactTable = CRM_Contact_BAO_Contact::getTableName();
     $edTable = CRM_Mailing_Event_BAO_Delivered::getTableName();
     $ebTable = CRM_Mailing_Event_BAO_Bounce::getTableName();
     $query = "  SELECT      {$eqTable}.id,\n                                {$emailTable}.email as email,\n                                {$eqTable}.contact_id,\n                                {$eqTable}.hash\n                    FROM        {$eqTable}\n                    INNER JOIN  {$emailTable}\n                            ON  {$eqTable}.email_id = {$emailTable}.id\n                    LEFT JOIN   {$edTable}\n                            ON  {$eqTable}.id = {$edTable}.event_queue_id\n                    LEFT JOIN   {$ebTable}\n                            ON  {$eqTable}.id = {$ebTable}.event_queue_id\n                    WHERE       {$eqTable}.job_id = " . $this->id . "\n                        AND     {$edTable}.id IS null\n                        AND     {$ebTable}.id IS null";
     $eq->query($query);
     while ($eq->fetch()) {
         /* Compose the mailing */
         $recipient = null;
         $message = $mailing->compose($this->id, $eq->id, $eq->hash, $eq->contact_id, $eq->email, $recipient);
         /* Send the mailing */
         $body = $message->get();
         $headers = $message->headers();
         /* TODO: when we separate the content generator from the delivery
          * engine, maybe we should dump the messages into a table */
         PEAR::setErrorHandling(PEAR_ERROR_CALLBACK, array('CRM_Mailing_BAO_Mailing', 'catchSMTP'));
         $result = $mailer->send($recipient, $headers, $body);
         CRM_Core_Error::setCallback();
         $params = array('event_queue_id' => $eq->id, 'job_id' => $this->id, 'hash' => $eq->hash);
         if (is_a($result, PEAR_Error)) {
             /* Register the bounce event */
             require_once 'CRM/Mailing/BAO/BouncePattern.php';
             require_once 'CRM/Mailing/Event/BAO/Bounce.php';
             $params = array_merge($params, CRM_Mailing_BAO_BouncePattern::match($result->getMessage()));
             CRM_Mailing_Event_BAO_Bounce::create($params);
         } else {
             /* Register the delivery event */
             CRM_Mailing_Event_BAO_Delivered::create($params);
         }
     }
 }
Пример #9
0
 /**
  * Try to match the string to a bounce type.
  *
  * @param string $message       The message to be matched
  * @return array                Tuple (bounce_type, bounce_reason)
  * @access public
  * @static
  */
 function &match(&$message)
 {
     if ($GLOBALS['_CRM_MAILING_BAO_BOUNCEPATTERN']['_patterns'] == null) {
         CRM_Mailing_BAO_BouncePattern::buildPatterns();
     }
     foreach ($GLOBALS['_CRM_MAILING_BAO_BOUNCEPATTERN']['_patterns'] as $type => $re) {
         if (preg_match($re, $message, $matches)) {
             return array('bounce_type_id' => $type, 'bounce_reason' => $matches[0]);
         }
     }
     return array('bounce_type_id' => null, 'bounce_reason' => null);
 }
Пример #10
0
 public function deliverGroup(&$fields, &$mailing, &$mailer, &$job_date, &$attachments)
 {
     // get the return properties
     $returnProperties = $mailing->getReturnProperties();
     $params = array();
     $targetParams = array();
     foreach ($fields as $key => $field) {
         $params[] = $field['contact_id'];
     }
     $details = $mailing->getDetails($params, $returnProperties);
     foreach ($fields as $key => $field) {
         $contactID = $field['contact_id'];
         /* Compose the mailing */
         $recipient = null;
         $message =& $mailing->compose($this->id, $field['id'], $field['hash'], $field['contact_id'], $field['email'], $recipient, false, $details[0][$contactID], $attachments);
         /* Send the mailing */
         $body =& $message->get();
         $headers =& $message->headers();
         // make $recipient actually be the *encoded* header, so as not to baffle Mail_RFC822, CRM-5743
         $recipient = $headers['To'];
         $result = null;
         /* TODO: when we separate the content generator from the delivery
          * engine, maybe we should dump the messages into a table */
         // disable error reporting on real mailings (but leave error reporting for tests), CRM-5744
         if ($job_date) {
             CRM_Core_Error::ignoreException();
         }
         if (is_object($mailer)) {
             // hack to stop mailing job at run time, CRM-4246.
             $mailingJob = new CRM_Mailing_DAO_Job();
             $mailingJob->mailing_id = $mailing->id;
             if ($mailingJob->find(true)) {
                 // mailing have been canceled at run time.
                 if ($mailingJob->status == 'Canceled') {
                     return false;
                 }
             } else {
                 // mailing have been deleted at run time.
                 return false;
             }
             $mailingJob->free();
             $result = $mailer->send($recipient, $headers, $body, $this->id);
             CRM_Core_Error::setCallback();
         }
         $params = array('event_queue_id' => $field['id'], 'job_id' => $this->id, 'hash' => $field['hash']);
         if (is_a($result, 'PEAR_Error')) {
             /* Register the bounce event */
             require_once 'CRM/Mailing/BAO/BouncePattern.php';
             require_once 'CRM/Mailing/Event/BAO/Bounce.php';
             $params = array_merge($params, CRM_Mailing_BAO_BouncePattern::match($result->getMessage()));
             CRM_Mailing_Event_BAO_Bounce::create($params);
         } else {
             /* Register the delivery event */
             CRM_Mailing_Event_BAO_Delivered::create($params);
         }
         $targetParams[] = $field['contact_id'];
         unset($result);
     }
     if (!empty($targetParams)) {
         // add activity record for every mail that is send
         $activityTypeID = CRM_Core_OptionGroup::getValue('activity_type', 'Bulk Email', 'name');
         $activity = array('source_contact_id' => $mailing->scheduled_id, 'target_contact_id' => $targetParams, 'activity_type_id' => $activityTypeID, 'source_record_id' => $this->mailing_id, 'activity_date_time' => $job_date, 'subject' => $mailing->subject, 'status_id' => 2, 'deleteActivityTarget' => false);
         //check whether activity is already created for this mailing.
         //if yes then create only target contact record.
         $query = "\nSELECT id \nFROM   civicrm_activity\nWHERE  civicrm_activity.activity_type_id = %1\nAND    civicrm_activity.source_record_id = %2";
         $queryParams = array(1 => array($activityTypeID, 'Integer'), 2 => array($this->mailing_id, 'Integer'));
         $activityID = CRM_Core_DAO::singleValueQuery($query, $queryParams);
         if ($activityID) {
             $activity['id'] = $activityID;
         }
         require_once 'api/v2/Activity.php';
         if (is_a(civicrm_activity_create($activity, 'Email'), 'CRM_Core_Error')) {
             return false;
         }
     }
     return true;
 }
Пример #11
0
 /**
  * Create a new forward event, create a new contact if necessary
  *
  * @param $job_id
  * @param $queue_id
  * @param $hash
  * @param $forward_email
  * @param null $fromEmail
  * @param null $comment
  *
  * @return bool
  */
 public static function &forward($job_id, $queue_id, $hash, $forward_email, $fromEmail = NULL, $comment = NULL)
 {
     $q = CRM_Mailing_Event_BAO_Queue::verify($job_id, $queue_id, $hash);
     $successfulForward = FALSE;
     $contact_id = NULL;
     if (!$q) {
         return $successfulForward;
     }
     /* Find the email address/contact, if it exists */
     $contact = CRM_Contact_BAO_Contact::getTableName();
     $location = CRM_Core_BAO_Location::getTableName();
     $email = CRM_Core_BAO_Email::getTableName();
     $queueTable = CRM_Mailing_Event_BAO_Queue::getTableName();
     $job = CRM_Mailing_BAO_MailingJob::getTableName();
     $mailing = CRM_Mailing_BAO_Mailing::getTableName();
     $forward = self::getTableName();
     $domain = CRM_Core_BAO_Domain::getDomain();
     $dao = new CRM_Core_Dao();
     $dao->query("\n                SELECT      {$contact}.id as contact_id,\n                            {$email}.id as email_id,\n                            {$contact}.do_not_email as do_not_email,\n                            {$queueTable}.id as queue_id\n                FROM        ({$email}, {$job} as temp_job)\n                INNER JOIN  {$contact}\n                        ON  {$email}.contact_id = {$contact}.id\n                LEFT JOIN   {$queueTable}\n                        ON  {$email}.id = {$queueTable}.email_id\n                LEFT JOIN   {$job}\n                        ON  {$queueTable}.job_id = {$job}.id\n                        AND temp_job.mailing_id = {$job}.mailing_id\n                WHERE       {$queueTable}.job_id = {$job_id}\n                    AND     {$email}.email = '" . CRM_Utils_Type::escape($forward_email, 'String') . "'");
     $dao->fetch();
     $transaction = new CRM_Core_Transaction();
     if (isset($dao->queue_id) || isset($dao->do_not_email) && $dao->do_not_email == 1) {
         /* We already sent this mailing to $forward_email, or we should
          * never email this contact.  Give up. */
         return $successfulForward;
     }
     require_once 'api/api.php';
     $contactParams = array('email' => $forward_email, 'version' => 3);
     $contactValues = civicrm_api('contact', 'get', $contactParams);
     $count = $contactValues['count'];
     if ($count == 0) {
         /* If the contact does not exist, create one. */
         $formatted = array('contact_type' => 'Individual', 'version' => 3);
         $locationType = CRM_Core_BAO_LocationType::getDefault();
         $value = array('email' => $forward_email, 'location_type_id' => $locationType->id);
         require_once 'CRM/Utils/DeprecatedUtils.php';
         _civicrm_api3_deprecated_add_formatted_param($value, $formatted);
         $formatted['onDuplicate'] = CRM_Import_Parser::DUPLICATE_SKIP;
         $formatted['fixAddress'] = TRUE;
         $contact = civicrm_api('contact', 'create', $formatted);
         if (civicrm_error($contact)) {
             return $successfulForward;
         }
         $contact_id = $contact['id'];
     }
     $email = new CRM_Core_DAO_Email();
     $email->email = $forward_email;
     $email->find(TRUE);
     $email_id = $email->id;
     if (!$contact_id) {
         $contact_id = $email->contact_id;
     }
     /* Create a new queue event */
     $queue_params = array('email_id' => $email_id, 'contact_id' => $contact_id, 'job_id' => $job_id);
     $queue = CRM_Mailing_Event_BAO_Queue::create($queue_params);
     $forward = new CRM_Mailing_Event_BAO_Forward();
     $forward->time_stamp = date('YmdHis');
     $forward->event_queue_id = $queue_id;
     $forward->dest_queue_id = $queue->id;
     $forward->save();
     $dao->reset();
     $dao->query("   SELECT  {$job}.mailing_id as mailing_id\n                        FROM    {$job}\n                        WHERE   {$job}.id = " . CRM_Utils_Type::escape($job_id, 'Integer'));
     $dao->fetch();
     $mailing_obj = new CRM_Mailing_BAO_Mailing();
     $mailing_obj->id = $dao->mailing_id;
     $mailing_obj->find(TRUE);
     $config = CRM_Core_Config::singleton();
     $mailer = $config->getMailer();
     $recipient = NULL;
     $attachments = NULL;
     $message = $mailing_obj->compose($job_id, $queue->id, $queue->hash, $queue->contact_id, $forward_email, $recipient, FALSE, NULL, $attachments, TRUE, $fromEmail);
     //append comment if added while forwarding.
     if (count($comment)) {
         $message->_txtbody = CRM_Utils_Array::value('body_text', $comment) . $message->_txtbody;
         if (!empty($comment['body_html'])) {
             $message->_htmlbody = $comment['body_html'] . '<br />---------------Original message---------------------<br />' . $message->_htmlbody;
         }
     }
     $body = $message->get();
     $headers = $message->headers();
     $result = NULL;
     if (is_object($mailer)) {
         $errorScope = CRM_Core_TemporaryErrorScope::ignoreException();
         $result = $mailer->send($recipient, $headers, $body);
         unset($errorScope);
     }
     $params = array('event_queue_id' => $queue->id, 'job_id' => $job_id, 'hash' => $queue->hash);
     if (is_a($result, 'PEAR_Error')) {
         /* Register the bounce event */
         $params = array_merge($params, CRM_Mailing_BAO_BouncePattern::match($result->getMessage()));
         CRM_Mailing_Event_BAO_Bounce::create($params);
     } else {
         $successfulForward = TRUE;
         /* Register the delivery event */
         CRM_Mailing_Event_BAO_Delivered::create($params);
     }
     $transaction->commit();
     return $successfulForward;
 }
Пример #12
0
 /**
  * Create a new forward event, create a new contact if necessary
  */
 function &forward($job_id, $queue_id, $hash, $forward_email)
 {
     $q =& CRM_Mailing_Event_BAO_Queue::verify($job_id, $queue_id, $hash);
     if (!$q) {
         return null;
     }
     /* Find the email address/contact, if it exists */
     $contact = CRM_Contact_BAO_Contact::getTableName();
     $location = CRM_Core_BAO_Location::getTableName();
     $email = CRM_Core_BAO_Email::getTableName();
     $queueTable = CRM_Mailing_Event_BAO_Queue::getTableName();
     $job = CRM_Mailing_BAO_Job::getTableName();
     $mailing = CRM_Mailing_BAO_Mailing::getTableName();
     $forward = CRM_Mailing_Event_BAO_Forward::getTableName();
     $domain =& CRM_Mailing_Event_BAO_Queue::getDomain($queue_id);
     $dao =& new CRM_Core_Dao();
     $dao->query("\n                SELECT      {$contact}.id as contact_id,\n                            {$email}.id as email_id,\n                            {$contact}.do_not_email as do_not_email,\n                            {$queueTable}.id as queue_id\n                FROM        {$email}, {$job} as temp_job\n                INNER JOIN  {$location}\n                        ON  {$email}.location_id = {$location}.id\n                INNER JOIN  {$contact}\n                        ON  {$location}.entity_table = '{$contact}'\n                        AND {$location}.entity_id = {$contact}.id\n                LEFT JOIN   {$queueTable}\n                        ON  {$email}.id = {$queueTable}.email_id\n                LEFT JOIN   {$job}\n                        ON  {$queueTable}.job_id = {$job}.id\n                        AND temp_job.mailing_id = {$job}.mailing_id\n                WHERE       temp_job.id = {$job_id}\n                    AND     {$email}.email = '" . CRM_Utils_Type::escape($forward_email, 'String') . "'");
     $dao->fetch();
     CRM_Core_DAO::transaction('BEGIN');
     if (isset($dao->queue_id) || $dao->do_not_email == 1) {
         /* We already sent this mailing to $forward_email, or we should
          * never email this contact.  Give up. */
         return false;
     } elseif (empty($dao->contact_id)) {
         /* No contact found, we'll have to create a new one */
         $contact_params = array('email' => $forward_email);
         $contact =& crm_create_contact($contact_params);
         if (is_a($contact, 'CRM_Core_Error')) {
             return false;
         }
         /* This is an ugly hack, but the API doesn't really support
          * overriding the domain ID any other way */
         $contact->domain_id = $domain->id;
         $contact->save();
         $contact_id = $contact->id;
         $email_id = $contact->location[1]->email[1]->id;
     } else {
         $contact_id = $dao->contact_id;
         $email_id = $dao->email_id;
     }
     /* Create a new queue event */
     $queue_params = array('email_id' => $email_id, 'contact_id' => $contact_id, 'job_id' => $job_id);
     $queue =& CRM_Mailing_Event_BAO_Queue::create($queue_params);
     $forward =& new CRM_Mailing_Event_BAO_Forward();
     $forward->time_stamp = date('YmdHis');
     $forward->event_queue_id = $queue_id;
     $forward->dest_queue_id = $queue->id;
     $forward->save();
     $dao->reset();
     $dao->query("   SELECT  {$job}.mailing_id as mailing_id \n                        FROM    {$job}\n                        WHERE   {$job}.id = " . CRM_Utils_Type::escape($job_id, 'Integer'));
     $dao->fetch();
     $mailing_obj =& new CRM_Mailing_BAO_Mailing();
     $mailing_obj->id = $dao->mailing_id;
     $mailing_obj->find(true);
     $config =& CRM_Core_Config::singleton();
     $mailer =& $config->getMailer();
     $recipient = null;
     $message =& $mailing_obj->compose($job_id, $queue->id, $queue->hash, $queue->contact_id, $forward_email, $recipient);
     $body = $message->get();
     $headers = $message->headers();
     PEAR::setErrorHandling(PEAR_ERROR_CALLBACK, array('CRM_Mailing_BAO_Mailing', 'catchSMTP'));
     $result = $mailer->send($recipient, $headers, $body);
     CRM_Core_Error::setCallback();
     $params = array('event_queue_id' => $queue->id, 'job_id' => $job_id, 'hash' => $queue->hash);
     if (is_a($result, PEAR_Error)) {
         /* Register the bounce event */
         $params = array_merge($params, CRM_Mailing_BAO_BouncePattern::match($result->getMessage()));
         CRM_Mailing_Event_BAO_Bounce::create($params);
     } else {
         /* Register the delivery event */
         CRM_Mailing_Event_BAO_Delivered::create($params);
     }
     CRM_Core_DAO::transaction('COMMIT');
     return true;
 }