/** * Register an open event. * * @param int $queue_id * The Queue Event ID of the recipient. */ public static function open($queue_id) { // First make sure there's a matching queue event. $success = FALSE; $q = new CRM_Mailing_Event_BAO_Queue(); $q->id = $queue_id; if ($q->find(TRUE)) { $oe = new CRM_Mailing_Event_BAO_Opened(); $oe->event_queue_id = $queue_id; $oe->time_stamp = date('YmdHis'); $oe->save(); $success = TRUE; } return $success; }
/** * Register an open event * * @param int $queue_id The Queue Event ID of the recipient * @return void * @access public * @static */ public static function open($queue_id) { /* First make sure there's a matching queue event */ require_once 'CRM/Mailing/Event/BAO/Queue.php'; $success = false; $q = new CRM_Mailing_Event_BAO_Queue(); $q->id = $queue_id; if ($q->find(true)) { $oe = new CRM_Mailing_Event_BAO_Opened(); $oe->event_queue_id = $queue_id; $oe->time_stamp = date('YmdHis'); $oe->save(); $success = true; } return $success; }
public static function processMandrillCalls($reponse) { $events = array('open', 'click', 'hard_bounce', 'soft_bounce', 'spam', 'reject'); $bounceType = array(); //MTE-17 $config = CRM_Core_Config::singleton(); if (property_exists($config, 'civiVersion')) { $civiVersion = $config->civiVersion; } else { $civiVersion = CRM_Core_BAO_Domain::version(); } if (version_compare('4.4alpha1', $civiVersion) > 0) { $jobCLassName = 'CRM_Mailing_DAO_Job'; } else { $jobCLassName = 'CRM_Mailing_DAO_MailingJob'; } foreach ($reponse as $value) { //changes done to check if email exists in response array if (in_array($value['event'], $events) && CRM_Utils_Array::value('email', $value['msg'])) { $metaData = CRM_Utils_Array::value('metadata', $value['msg']) ? CRM_Utils_Array::value('CiviCRM_Mandrill_id', $value['msg']['metadata']) : NULL; $header = self::extractHeader($metaData); $mail = self::getMailing($header, $jobCLassName); $contacts = array(); if ($mail->find(TRUE)) { if ($value['event'] == 'click' && $mail->url_tracking === FALSE || $value['event'] == 'open' && $mail->open_tracking === FALSE) { continue; } $emails = self::retrieveEmailContactId($value['msg']['email']); if (!CRM_Utils_Array::value('contact_id', $emails['email'])) { continue; } $value['mailing_id'] = $mail->id; // IF no activity id in header then create new activity if (empty($header[0])) { self::createActivity($value, NULL, $header); } if (empty($header[2])) { $params = array('job_id' => CRM_Core_DAO::getFieldValue($jobCLassName, $mail->id, 'id', 'mailing_id'), 'contact_id' => $emails['email']['contact_id'], 'email_id' => $emails['email']['id']); $eventQueue = CRM_Mailing_Event_BAO_Queue::create($params); $eventQueueID = $eventQueue->id; $hash = $eventQueue->hash; $jobId = $params['job_id']; } else { $eventQueueID = $header[3]; $hash = explode('@', $header[4]); $hash = $hash[0]; $jobId = $header[2]; } if ($eventQueueID) { $mandrillActivtyParams = array('mailing_queue_id' => $eventQueueID, 'activity_id' => $header[0]); CRM_Mte_BAO_MandrillActivity::create($mandrillActivtyParams); } $msgBody = ''; if (!empty($header[0])) { $msgBody = CRM_Core_DAO::getFieldValue('CRM_Activity_DAO_Activity', $header[0], 'details'); } $value['mail_body'] = $msgBody; $bType = ucfirst(preg_replace('/_\\w+/', '', $value['event'])); $assignedContacts = array(); switch ($value['event']) { case 'open': $oe = new CRM_Mailing_Event_BAO_Opened(); $oe->event_queue_id = $eventQueueID; $oe->time_stamp = date('YmdHis', $value['ts']); $oe->save(); break; case 'click': if (CRM_Utils_Array::value(1, $header) == 'b') { break; } $tracker = new CRM_Mailing_BAO_TrackableURL(); $tracker->url = $value['url']; $tracker->mailing_id = $mail->id; if (!$tracker->find(TRUE)) { $tracker->save(); } $open = new CRM_Mailing_Event_BAO_TrackableURLOpen(); $open->event_queue_id = $eventQueueID; $open->trackable_url_id = $tracker->id; $open->time_stamp = date('YmdHis', $value['ts']); $open->save(); break; case 'hard_bounce': case 'soft_bounce': case 'spam': case 'reject': if (empty($bounceType)) { CRM_Core_PseudoConstant::populate($bounceType, 'CRM_Mailing_DAO_BounceType', TRUE, 'id', NULL, NULL, NULL, 'name'); } //Delete queue in delivered since this email is not successfull $delivered = new CRM_Mailing_Event_BAO_Delivered(); $delivered->event_queue_id = $eventQueueID; if ($delivered->find(TRUE)) { $delivered->delete(); } $bounceParams = array('time_stamp' => date('YmdHis', $value['ts']), 'event_queue_id' => $eventQueueID, 'bounce_type_id' => $bounceType["Mandrill {$bType}"], 'job_id' => $jobId, 'hash' => $hash); $bounceParams['bounce_reason'] = CRM_Utils_Array::value('bounce_description', $value['msg']); if (empty($bounceParams['bounce_reason'])) { $bounceParams['bounce_reason'] = CRM_Core_DAO::getFieldValue('CRM_Mailing_DAO_BounceType', $bounceType["Mandrill {$bType}"], 'description'); } CRM_Mailing_Event_BAO_Bounce::create($bounceParams); if (substr($value['event'], -7) == '_bounce') { $mailingBackend = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::MAILING_PREFERENCES_NAME, 'mandrill_smtp_settings'); if (CRM_Utils_Array::value('group_id', $mailingBackend)) { list($domainEmailName, $domainEmailAddress) = CRM_Core_BAO_Domain::getNameAndEmail(); $mailBody = ts('The following email failed to be delivered due to a') . " {$bType} Bounce :</br>\nTo: {$value['msg']['email']} </br>\nFrom: {$value['msg']['sender']} </br>\nSubject: {$value['msg']['subject']}</br>\nMessage Body: {$msgBody}"; $mailParams = array('groupName' => 'Mandrill bounce notification', 'from' => '"' . $domainEmailName . '" <' . $domainEmailAddress . '>', 'subject' => ts('Mandrill Bounce Notification'), 'text' => $mailBody, 'html' => $mailBody); $query = "SELECT ce.email, cc.sort_name, cgc.contact_id FROM civicrm_contact cc\nINNER JOIN civicrm_group_contact cgc ON cgc.contact_id = cc.id\nINNER JOIN civicrm_email ce ON ce.contact_id = cc.id\nWHERE cc.is_deleted = 0 AND cc.is_deceased = 0 AND cgc.group_id = {$mailingBackend['group_id']} AND ce.is_primary = 1 AND ce.email <> %1"; $queryParam = array(1 => array($value['msg']['email'], 'String')); $dao = CRM_Core_DAO::executeQuery($query, $queryParam); while ($dao->fetch()) { $mailParams['toName'] = $dao->sort_name; $mailParams['toEmail'] = $dao->email; CRM_Utils_Mail::send($mailParams); $value['assignee_contact_id'][] = $dao->contact_id; } } } $bType = 'Bounce'; break; } // create activity for click and open event if ($value['event'] == 'open' || $value['event'] == 'click' || $bType == 'Bounce') { self::createActivity($value, $bType, $header); } } } } }