Example #1
0
 /**
  * Create a new bounce event, update the email address if necessary
  */
 static function &create(&$params)
 {
     $q =& CRM_Mailing_Event_BAO_Queue::verify($params['job_id'], $params['event_queue_id'], $params['hash']);
     $success = NULL;
     if (!$q) {
         return $success;
     }
     $transaction = new CRM_Core_Transaction();
     $bounce = new CRM_Mailing_Event_BAO_Bounce();
     $bounce->time_stamp = date('YmdHis');
     // if we dont have a valid bounce type, we should set it
     // to bounce_type_id 11 which is Syntax error. this allows such email
     // addresses to be bounce a few more time before being put on hold
     // CRM-4814
     // we changed this behavior since this bounce type might be due to some issue
     // with the connection or smtp server etc
     if (empty($params['bounce_type_id'])) {
         $params['bounce_type_id'] = 11;
         if (empty($params['bounce_reason'])) {
             $params['bounce_reason'] = ts('Unknown bounce type: Could not parse bounce email');
         }
     }
     // CRM-11989
     $params['bounce_reason'] = substr($params['bounce_reason'], 0, 254);
     $bounce->copyValues($params);
     $bounce->save();
     $success = TRUE;
     $bounceTable = CRM_Mailing_Event_BAO_Bounce::getTableName();
     $bounceType = CRM_Mailing_DAO_BounceType::getTableName();
     $emailTable = CRM_Core_BAO_Email::getTableName();
     $queueTable = CRM_Mailing_Event_BAO_Queue::getTableName();
     $bounce->reset();
     // might want to put distinct inside the count
     $query = "SELECT     count({$bounceTable}.id) as bounces,\n                            {$bounceType}.hold_threshold as threshold\n                FROM        {$bounceTable}\n                INNER JOIN  {$bounceType}\n                        ON  {$bounceTable}.bounce_type_id = {$bounceType}.id\n                INNER JOIN  {$queueTable}\n                        ON  {$bounceTable}.event_queue_id = {$queueTable}.id\n                INNER JOIN  {$emailTable}\n                        ON  {$queueTable}.email_id = {$emailTable}.id\n                WHERE       {$emailTable}.id = {$q->email_id}\n                    AND     ({$emailTable}.reset_date IS NULL\n                        OR  {$bounceTable}.time_stamp >= {$emailTable}.reset_date)\n                GROUP BY    {$bounceTable}.bounce_type_id\n                ORDER BY    threshold, bounces desc";
     $bounce->query($query);
     while ($bounce->fetch()) {
         if ($bounce->bounces >= $bounce->threshold) {
             $email = new CRM_Core_BAO_Email();
             $email->id = $q->email_id;
             $email->on_hold = TRUE;
             $email->hold_date = date('YmdHis');
             $email->save();
             break;
         }
     }
     $transaction->commit();
     return $success;
 }
Example #2
0
 /**
  * Create a new bounce event, update the email address if necessary
  */
 static function &create(&$params)
 {
     $q =& CRM_Mailing_Event_BAO_Queue::verify($params['job_id'], $params['event_queue_id'], $params['hash']);
     $success = null;
     if (!$q) {
         return $success;
     }
     require_once 'CRM/Core/Transaction.php';
     $transaction = new CRM_Core_Transaction();
     $bounce = new CRM_Mailing_Event_BAO_Bounce();
     $bounce->time_stamp = date('YmdHis');
     // if we dont have a valid bounce type, we should set it
     // to bounce_type_id 6 which is Invalid. this allows such email
     // addresses to be put on hold immediately, CRM-4814
     if (empty($params['bounce_type_id'])) {
         $params['bounce_type_id'] = 6;
         $params['bounce_reason'] = ts('Unknown bounce type: Could not parse bounce email');
     }
     $bounce->copyValues($params);
     $bounce->save();
     $success = true;
     $bounceTable = CRM_Mailing_Event_BAO_Bounce::getTableName();
     $bounceType = CRM_Mailing_DAO_BounceType::getTableName();
     $emailTable = CRM_Core_BAO_Email::getTableName();
     $queueTable = CRM_Mailing_Event_BAO_Queue::getTableName();
     $bounce->reset();
     // might want to put distinct inside the count
     $query = "SELECT     count({$bounceTable}.id) as bounces,\n                            {$bounceType}.hold_threshold as threshold\n                FROM        {$bounceTable}\n                INNER JOIN  {$bounceType}\n                        ON  {$bounceTable}.bounce_type_id = {$bounceType}.id\n                INNER JOIN  {$queueTable}\n                        ON  {$bounceTable}.event_queue_id = {$queueTable}.id\n                INNER JOIN  {$emailTable}\n                        ON  {$queueTable}.email_id = {$emailTable}.id\n                WHERE       {$emailTable}.id = {$q->email_id}\n                    AND     ({$emailTable}.reset_date IS NULL\n                        OR  {$bounceTable}.time_stamp >= {$emailTable}.reset_date)\n                GROUP BY    {$bounceTable}.bounce_type_id\n                ORDER BY    threshold, bounces desc";
     $bounce->query($query);
     while ($bounce->fetch()) {
         if ($bounce->bounces >= $bounce->threshold) {
             $email = new CRM_Core_BAO_Email();
             $email->id = $q->email_id;
             $email->on_hold = true;
             $email->hold_date = date('YmdHis');
             $email->save();
             break;
         }
     }
     $transaction->commit();
     return $success;
 }
 static function recordBounce($params)
 {
     $isSpam = CRM_Utils_Array::value('is_spam', $params);
     $mailingId = CRM_Utils_Array::value('mailing_id', $params);
     //CiviCRM mailling ID
     $contactId = CRM_Utils_Array::value('contact_id', $params);
     $emailId = CRM_Utils_Array::value('email_id', $params);
     $email = CRM_Utils_Array::value('email', $params);
     $jobId = CRM_Utils_Array::value('job_id', $params);
     $eqParams = array('job_id' => $jobId, 'contact_id' => $contactId, 'email_id' => $emailId);
     $eventQueue = CRM_Mailing_Event_BAO_Queue::create($eqParams);
     $time = date('YmdHis', CRM_Utils_Array::value('date_ts', $params));
     $bounceType = array();
     CRM_Core_PseudoConstant::populate($bounceType, 'CRM_Mailing_DAO_BounceType', TRUE, 'id', NULL, NULL, NULL, 'name');
     $bounce = new CRM_Mailing_Event_BAO_Bounce();
     $bounce->time_stamp = $time;
     $bounce->event_queue_id = $eventQueue->id;
     if ($isSpam) {
         $bounce->bounce_type_id = $bounceType[CRM_Mailjet_Upgrader::SPAM];
         $bounce->bounce_reason = CRM_Utils_Array::value('source', $params);
         //bounce reason when spam occured
     } else {
         $hardBounce = CRM_Utils_Array::value('hard_bounce', $params);
         $blocked = CRM_Utils_Array::value('blocked', $params);
         //  blocked : true if this bounce leads to recipient being blocked
         if ($hardBounce && $blocked) {
             $bounce->bounce_type_id = $bounceType[CRM_Mailjet_Upgrader::BLOCKED];
         } elseif ($hardBounce && !$blocked) {
             $bounce->bounce_type_id = $bounceType[CRM_Mailjet_Upgrader::HARD_BOUNCE];
         } else {
             $bounce->bounce_type_id = $bounceType[CRM_Mailjet_Upgrader::SOFT_BOUNCE];
         }
         $bounce->bounce_reason = $params['error_related_to'] . " - " . $params['error'];
     }
     $bounce->save();
     if ($bounce->bounce_type_id != $bounceType[CRM_Mailjet_Upgrader::SOFT_BOUNCE]) {
         $params = array('id' => $contactId, 'do_not_email' => 1);
         civicrm_api3('Contact', 'create', $params);
     }
     return TRUE;
 }