/** * Register a reply event. * * @param int $job_id * The job ID of the reply. * @param int $queue_id * The queue event id. * @param string $hash * The hash. * * @param null $replyto * * @return object|null * The mailing object, or null on failure */ public static function &reply($job_id, $queue_id, $hash, $replyto = NULL) { /* First make sure there's a matching queue event */ $q = CRM_Mailing_Event_BAO_Queue::verify($job_id, $queue_id, $hash); $success = NULL; if (!$q) { return $success; } $mailing = new CRM_Mailing_BAO_Mailing(); $mailings = CRM_Mailing_BAO_Mailing::getTableName(); $jobs = CRM_Mailing_BAO_MailingJob::getTableName(); $mailing->query("SELECT * FROM {$mailings}\n INNER JOIN {$jobs}\n ON {$jobs}.mailing_id = {$mailings}.id\n WHERE {$jobs}.id = {$q->job_id}"); $mailing->fetch(); if ($mailing->auto_responder) { self::autoRespond($mailing, $queue_id, $replyto); } $re = new CRM_Mailing_Event_BAO_Reply(); $re->event_queue_id = $queue_id; $re->time_stamp = date('YmdHis'); $re->save(); if (!$mailing->forward_replies || empty($mailing->replyto_email)) { return $success; } return $mailing; }
/** * Create a new bounce event, update the email address if necessary */ function &create(&$params) { $q =& CRM_Mailing_Event_BAO_Queue::verify($params['job_id'], $params['event_queue_id'], $params['hash']); if (!$q) { return null; } CRM_Core_DAO::transaction('BEGIN'); $bounce =& new CRM_Mailing_Event_BAO_Bounce(); $bounce->time_stamp = date('YmdHis'); $bounce->copyValues($params); $bounce->save(); $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; } } CRM_Core_DAO::transaction('COMMIT'); }
public function preProcess() { $this->_type = 'unsubscribe'; $this->_job_id = $job_id = CRM_Utils_Request::retrieve('jid', 'Integer', $this); $this->_queue_id = $queue_id = CRM_Utils_Request::retrieve('qid', 'Integer', $this); $this->_hash = $hash = CRM_Utils_Request::retrieve('h', 'String', $this); if (!$job_id || !$queue_id || !$hash) { CRM_Core_Error::fatal(ts("Missing Parameters")); } // verify that the three numbers above match $q = CRM_Mailing_Event_BAO_Queue::verify($job_id, $queue_id, $hash); if (!$q) { CRM_Core_Error::fatal(ts("There was an error in your request")); } list($displayName, $email) = CRM_Mailing_Event_BAO_Queue::getContactInfo($queue_id); $this->assign('display_name', $displayName); $emailMasked = CRM_Utils_String::maskEmail($email); $this->assign('email_masked', $emailMasked); $this->assign('email', $email); $this->_email = $email; $groups = CRM_Mailing_Event_BAO_Unsubscribe::unsub_from_mailing($job_id, $queue_id, $hash, TRUE); $this->assign('groups', $groups); $groupExist = NULL; foreach ($groups as $key => $value) { if ($value) { $groupExist = TRUE; } } if (!$groupExist) { $statusMsg = ts('Email: %1 has been successfully unsubscribed from this Mailing List/Group.', array(1 => $email)); CRM_Core_Session::setStatus($statusMsg, '', 'fail'); } $this->assign('groupExist', $groupExist); }
/** * Create a new delivery event * @param array $params Associative array of delivery event values * @return void * @access public * @static */ function &create(&$params) { $q =& CRM_Mailing_Event_BAO_Queue::verify($params['job_id'], $params['event_queue_id'], $params['hash']); if (!$q) { return null; } $delivered =& new CRM_Mailing_Event_BAO_Delivered(); $delivered->time_stamp = date('YmdHis'); $delivered->copyValues($params); $delivered->save(); return $delivered; }
/** * 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; }
function preProcess() { $job_id = CRM_Utils_Request::retrieve('jid', $this, null); $queue_id = CRM_Utils_Request::retrieve('qid', $this, null); $hash = CRM_Utils_Request::retrieve('h', $this, null); $q =& CRM_Mailing_Event_BAO_Queue::verify($job_id, $queue_id, $hash); if ($q == null) { /** ERROR **/ CRM_Utils_System::statusBounce(ts('Invalid form parameters.')); } $mailing =& $q->getMailing(); /* Show the subject instead of the name here, since it's being * displayed to external contacts/users */ CRM_Utils_System::setTitle(ts('Forward Mailing: %1', array(1 => $mailing->subject))); $this->set('queue_id', $queue_id); $this->set('job_id', $job_id); $this->set('hash', $hash); }
/** * 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; }
public function preProcess() { $this->_type = 'optout'; $this->_job_id = $job_id = CRM_Utils_Request::retrieve('jid', 'Integer', $this); $this->_queue_id = $queue_id = CRM_Utils_Request::retrieve('qid', 'Integer', $this); $this->_hash = $hash = CRM_Utils_Request::retrieve('h', 'String', $this); if (!$job_id || !$queue_id || !$hash) { CRM_Core_Error::fatal(ts("Missing input parameters")); } // verify that the three numbers above match $q = CRM_Mailing_Event_BAO_Queue::verify($job_id, $queue_id, $hash); if (!$q) { CRM_Core_Error::fatal(ts("There was an error in your request")); } list($displayName, $email) = CRM_Mailing_Event_BAO_Queue::getContactInfo($queue_id); $this->assign('display_name', $displayName); $emailMasked = CRM_Utils_String::maskEmail($email); $this->assign('email_masked', $emailMasked); $this->assign('email', $email); $this->_email = $email; }
/** * Create a new delivery event * @param array $params Associative array of delivery event values * @return void * @access public * @static */ public static function &create(&$params) { $q =& CRM_Mailing_Event_BAO_Queue::verify($params['job_id'], $params['event_queue_id'], $params['hash']); if (!$q) { return null; } $q->free(); $delivered =& new CRM_Mailing_Event_BAO_Delivered(); $delivered->time_stamp = date('YmdHis'); $delivered->copyValues($params); $delivered->save(); $queue = new CRM_Mailing_Event_BAO_Queue(); $queue->id = $params['event_queue_id']; $queue->find(true); while ($queue->fetch()) { $email =& new CRM_Core_BAO_Email(); $email->id = $queue->email_id; $email->hold_date = ''; $email->reset_date = date('YmdHis'); $email->save(); } return $delivered; }
public function preProcess() { $job_id = CRM_Utils_Request::retrieve('jid', 'Positive', $this, NULL); $queue_id = CRM_Utils_Request::retrieve('qid', 'Positive', $this, NULL); $hash = CRM_Utils_Request::retrieve('h', 'String', $this, NULL); $q = CRM_Mailing_Event_BAO_Queue::verify($job_id, $queue_id, $hash); if ($q == NULL) { // ERROR. CRM_Core_Error::fatal(ts('Invalid form parameters.')); CRM_Core_Error::statusBounce(ts('Invalid form parameters.')); } $mailing =& $q->getMailing(); if ($hash) { $emailId = CRM_Core_DAO::getfieldValue('CRM_Mailing_Event_DAO_Queue', $hash, 'email_id', 'hash'); $this->_fromEmail = $fromEmail = CRM_Core_DAO::getfieldValue('CRM_Core_DAO_Email', $emailId, 'email'); $this->assign('fromEmail', $fromEmail); } // Show the subject instead of the name here, since it's being // displayed to external contacts/users. CRM_Utils_System::setTitle(ts('Forward Mailing: %1', array(1 => $mailing->subject))); $this->set('queue_id', $queue_id); $this->set('job_id', $job_id); $this->set('hash', $hash); }
/** * 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; }
/** * Unsubscribe a contact from all groups that received this mailing. * * @param int $job_id * The job ID. * @param int $queue_id * The Queue Event ID of the recipient. * @param string $hash * The hash. * @param bool $return * If true return the list of groups. * * @return array|null * $groups Array of all groups from which the contact was removed, or null if the queue event could not be found. */ public static function &unsub_from_mailing($job_id, $queue_id, $hash, $return = FALSE) { // First make sure there's a matching queue event. $q = CRM_Mailing_Event_BAO_Queue::verify($job_id, $queue_id, $hash); $success = NULL; if (!$q) { return $success; } $contact_id = $q->contact_id; $transaction = new CRM_Core_Transaction(); $do = new CRM_Core_DAO(); $mgObject = new CRM_Mailing_DAO_MailingGroup(); $mg = $mgObject->getTableName(); $jobObject = new CRM_Mailing_BAO_MailingJob(); $job = $jobObject->getTableName(); $mailingObject = new CRM_Mailing_BAO_Mailing(); $mailing = $mailingObject->getTableName(); $groupObject = new CRM_Contact_BAO_Group(); $group = $groupObject->getTableName(); $gcObject = new CRM_Contact_BAO_GroupContact(); $gc = $gcObject->getTableName(); //We Need the mailing Id for the hook... $do->query("SELECT {$job}.mailing_id as mailing_id\n FROM {$job}\n WHERE {$job}.id = " . CRM_Utils_Type::escape($job_id, 'Integer')); $do->fetch(); $mailing_id = $do->mailing_id; $do->query("\n SELECT {$mg}.entity_table as entity_table,\n {$mg}.entity_id as entity_id,\n {$mg}.group_type as group_type\n FROM {$mg}\n INNER JOIN {$job}\n ON {$job}.mailing_id = {$mg}.mailing_id\n INNER JOIN {$group}\n ON {$mg}.entity_id = {$group}.id\n WHERE {$job}.id = " . CRM_Utils_Type::escape($job_id, 'Integer') . "\n AND {$mg}.group_type IN ('Include', 'Base')\n AND {$group}.is_hidden = 0"); // Make a list of groups and a list of prior mailings that received // this mailing. $groups = array(); $base_groups = array(); $mailings = array(); while ($do->fetch()) { if ($do->entity_table == $group) { if ($do->group_type == 'Base') { $base_groups[$do->entity_id] = NULL; } else { $groups[$do->entity_id] = NULL; } } elseif ($do->entity_table == $mailing) { $mailings[] = $do->entity_id; } } // As long as we have prior mailings, find their groups and add to the // list. while (!empty($mailings)) { $do->query("\n SELECT {$mg}.entity_table as entity_table,\n {$mg}.entity_id as entity_id\n FROM {$mg}\n WHERE {$mg}.mailing_id IN (" . implode(', ', $mailings) . ")\n AND {$mg}.group_type = 'Include'"); $mailings = array(); while ($do->fetch()) { if ($do->entity_table == $group) { $groups[$do->entity_id] = TRUE; } elseif ($do->entity_table == $mailing) { $mailings[] = $do->entity_id; } } } //Pass the groups to be unsubscribed from through a hook. $group_ids = array_keys($groups); $base_group_ids = array_keys($base_groups); CRM_Utils_Hook::unsubscribeGroups('unsubscribe', $mailing_id, $contact_id, $group_ids, $base_group_ids); // Now we have a complete list of recipient groups. Filter out all // those except smart groups, those that the contact belongs to and // base groups from search based mailings. $baseGroupClause = ''; if (!empty($base_group_ids)) { $baseGroupClause = "OR {$group}.id IN(" . implode(', ', $base_group_ids) . ")"; } $groupIdClause = ''; if ($group_ids || $base_group_ids) { $groupIdClause = "AND {$group}.id IN (" . implode(', ', array_merge($group_ids, $base_group_ids)) . ")"; } $do->query("\n SELECT {$group}.id as group_id,\n {$group}.title as title,\n {$group}.description as description\n FROM {$group}\n LEFT JOIN {$gc}\n ON {$gc}.group_id = {$group}.id\n WHERE {$group}.is_hidden = 0\n {$groupIdClause}\n AND ({$group}.saved_search_id is not null\n OR ({$gc}.contact_id = {$contact_id}\n AND {$gc}.status = 'Added')\n {$baseGroupClause}\n )"); if ($return) { $returnGroups = array(); while ($do->fetch()) { $returnGroups[$do->group_id] = array('title' => $do->title, 'description' => $do->description); } return $returnGroups; } else { while ($do->fetch()) { $groups[$do->group_id] = $do->title; } } $contacts = array($contact_id); foreach ($groups as $group_id => $group_name) { $notremoved = FALSE; if ($group_name) { if (in_array($group_id, $base_group_ids)) { list($total, $removed, $notremoved) = CRM_Contact_BAO_GroupContact::addContactsToGroup($contacts, $group_id, 'Email', 'Removed'); } else { list($total, $removed, $notremoved) = CRM_Contact_BAO_GroupContact::removeContactsFromGroup($contacts, $group_id, 'Email'); } } if ($notremoved) { unset($groups[$group_id]); } } $ue = new CRM_Mailing_Event_BAO_Unsubscribe(); $ue->event_queue_id = $queue_id; $ue->org_unsubscribe = 0; $ue->time_stamp = date('YmdHis'); $ue->save(); $transaction->commit(); return $groups; }
/** * 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; }
/** * Unsubscribe a contact from all groups that received this mailing * * @param int $job_id The job ID * @param int $queue_id The Queue Event ID of the recipient * @param string $hash The hash * @param boolean $return If true return the list of groups. * @return array|null $groups Array of all groups from which the contact was removed, or null if the queue event could not be found. * @access public * @static */ public static function &unsub_from_mailing($job_id, $queue_id, $hash, $return = false) { /* First make sure there's a matching queue event */ $q =& CRM_Mailing_Event_BAO_Queue::verify($job_id, $queue_id, $hash); if (!$q) { return null; } $contact_id = $q->contact_id; require_once 'CRM/Core/Transaction.php'; $transaction = new CRM_Core_Transaction(); $do =& new CRM_Core_DAO(); $mg = CRM_Mailing_DAO_Group::getTableName(); $job = CRM_Mailing_BAO_Job::getTableName(); $mailing = CRM_Mailing_BAO_Mailing::getTableName(); $group = CRM_Contact_BAO_Group::getTableName(); $gc = CRM_Contact_BAO_GroupContact::getTableName(); $do->query("\n SELECT {$mg}.entity_table as entity_table,\n {$mg}.entity_id as entity_id\n FROM {$mg}\n INNER JOIN {$job}\n ON {$job}.mailing_id = {$mg}.mailing_id\n WHERE {$job}.id = " . CRM_Utils_Type::escape($job_id, 'Integer') . "\n AND {$mg}.group_type = 'Include'"); /* Make a list of groups and a list of prior mailings that received * this mailing */ $groups = array(); $mailings = array(); while ($do->fetch()) { if ($do->entity_table == $group) { //$groups[$do->entity_id] = true; $groups[$do->entity_id] = null; } else { if ($do->entity_table == $mailing) { $mailings[] = $do->entity_id; } } } /* As long as we have prior mailings, find their groups and add to the * list */ while (!empty($mailings)) { $do->query("\n SELECT {$mg}.entity_table as entity_table,\n {$mg}.entity_id as entity_id\n FROM {$mg}\n WHERE {$mg}.mailing_id IN (" . implode(', ', $mailings) . ")\n AND {$mg}.group_type = 'Include'"); $mailings = array(); while ($do->fetch()) { if ($do->entity_table == $group) { $groups[$do->entity_id] = true; } else { if ($do->entity_table == $mailing) { $mailings[] = $do->entity_id; } } } } /* Now we have a complete list of recipient groups. Filter out all * those except smart groups and those that the contact belongs to */ $do->query("\n SELECT {$group}.id as group_id,\n {$group}.title as title,\n {$group}.description as description\n FROM {$group}\n LEFT JOIN {$gc}\n ON {$gc}.group_id = {$group}.id\n WHERE {$group}.id IN (" . implode(', ', array_keys($groups)) . ")\n AND ({$group}.saved_search_id is not null\n OR ({$gc}.contact_id = {$contact_id}\n AND {$gc}.status = 'Added')\n )"); if ($return) { while ($do->fetch()) { $groups[$do->group_id] = array('title' => $do->title, 'description' => $do->description); } return $groups; } else { while ($do->fetch()) { $groups[$do->group_id] = $do->title; } } $contacts = array($contact_id); foreach ($groups as $group_id => $group_name) { $notremoved = false; if ($group_name) { list($total, $removed, $notremoved) = CRM_Contact_BAO_GroupContact::removeContactsFromGroup($contacts, $group_id, 'Email'); } if ($notremoved) { unset($groups[$group_id]); } } $ue =& new CRM_Mailing_Event_BAO_Unsubscribe(); $ue->event_queue_id = $queue_id; $ue->org_unsubscribe = 0; $ue->time_stamp = date('YmdHis'); $ue->save(); $transaction->commit(); return $groups; }
/** * Resubscribe a contact to the groups, he/she was unsubscribed from. * * @param int $job_id * The job ID. * @param int $queue_id * The Queue Event ID of the recipient. * @param string $hash * The hash. * * @return array|null * $groups Array of all groups to which the contact was added, or null if the queue event could not be found. */ public static function &resub_to_mailing($job_id, $queue_id, $hash) { // First make sure there's a matching queue event. $q = CRM_Mailing_Event_BAO_Queue::verify($job_id, $queue_id, $hash); $success = NULL; if (!$q) { return $success; } // check if this queue_id was actually unsubscribed $ue = new CRM_Mailing_Event_BAO_Unsubscribe(); $ue->event_queue_id = $queue_id; $ue->org_unsubscribe = 0; if (!$ue->find(TRUE)) { return $success; } $contact_id = $q->contact_id; $transaction = new CRM_Core_Transaction(); $do = new CRM_Core_DAO(); $mg = CRM_Mailing_DAO_MailingGroup::getTableName(); $job = CRM_Mailing_BAO_MailingJob::getTableName(); $mailing = CRM_Mailing_BAO_Mailing::getTableName(); $group = CRM_Contact_BAO_Group::getTableName(); $gc = CRM_Contact_BAO_GroupContact::getTableName(); // We Need the mailing Id for the hook... $do->query("SELECT {$job}.mailing_id as mailing_id\n FROM {$job}\n WHERE {$job}.id = " . CRM_Utils_Type::escape($job_id, 'Integer')); $do->fetch(); $mailing_id = $do->mailing_id; $do->query("\n SELECT {$mg}.entity_table as entity_table,\n {$mg}.entity_id as entity_id\n FROM {$mg}\n INNER JOIN {$job}\n ON {$job}.mailing_id = {$mg}.mailing_id\n INNER JOIN {$group}\n ON {$mg}.entity_id = {$group}.id\n WHERE {$job}.id = " . CRM_Utils_Type::escape($job_id, 'Integer') . "\n AND {$mg}.group_type IN ( 'Include', 'Base' )\n AND {$group}.is_hidden = 0"); // Make a list of groups and a list of prior mailings that received // this mailing. $groups = array(); $mailings = array(); while ($do->fetch()) { if ($do->entity_table == $group) { $groups[$do->entity_id] = NULL; } elseif ($do->entity_table == $mailing) { $mailings[] = $do->entity_id; } } // As long as we have prior mailings, find their groups and add to the // list. while (!empty($mailings)) { $do->query("\n SELECT {$mg}.entity_table as entity_table,\n {$mg}.entity_id as entity_id\n FROM {$mg}\n WHERE {$mg}.mailing_id IN (" . implode(', ', $mailings) . ")\n AND {$mg}.group_type = 'Include'"); $mailings = array(); while ($do->fetch()) { if ($do->entity_table == $group) { $groups[$do->entity_id] = TRUE; } elseif ($do->entity_table == $mailing) { $mailings[] = $do->entity_id; } } } $group_ids = array_keys($groups); $base_groups = NULL; CRM_Utils_Hook::unsubscribeGroups('resubscribe', $mailing_id, $contact_id, $group_ids, $base_groups); // Now we have a complete list of recipient groups. Filter out all // those except smart groups and those that the contact belongs to. $do->query("\n SELECT {$group}.id as group_id,\n {$group}.title as title\n FROM {$group}\n LEFT JOIN {$gc}\n ON {$gc}.group_id = {$group}.id\n WHERE {$group}.id IN (" . implode(', ', $group_ids) . ")\n AND ({$group}.saved_search_id is not null\n OR ({$gc}.contact_id = {$contact_id}\n AND {$gc}.status = 'Removed')\n )"); while ($do->fetch()) { $groups[$do->group_id] = $do->title; } $contacts = array($contact_id); foreach ($groups as $group_id => $group_name) { $notadded = 0; if ($group_name) { list($total, $added, $notadded) = CRM_Contact_BAO_GroupContact::addContactsToGroup($contacts, $group_id, 'Email'); } if ($notadded) { unset($groups[$group_id]); } } // remove entry from Unsubscribe table. $ue = new CRM_Mailing_Event_BAO_Unsubscribe(); $ue->event_queue_id = $queue_id; $ue->org_resubscribe = 0; if ($ue->find(TRUE)) { $ue->delete(); } $transaction->commit(); return $groups; }
/** * 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; }
/** * Run page. * * This includes assigning smarty variables and other page processing. * * @return string * @throws Exception */ public function run() { $job_id = CRM_Utils_Request::retrieve('jid', 'Integer', CRM_Core_DAO::$_nullObject); $queue_id = CRM_Utils_Request::retrieve('qid', 'Integer', CRM_Core_DAO::$_nullObject); $hash = CRM_Utils_Request::retrieve('h', 'String', CRM_Core_DAO::$_nullObject); if (!$job_id || !$queue_id || !$hash) { CRM_Core_Error::fatal(ts("Missing input parameters")); } // verify that the three numbers above match $q = CRM_Mailing_Event_BAO_Queue::verify($job_id, $queue_id, $hash); if (!$q) { CRM_Core_Error::fatal(ts("There was an error in your request")); } $cancel = CRM_Utils_Request::retrieve("_qf_{$this->_type}_cancel", 'String', CRM_Core_DAO::$_nullObject, FALSE, NULL, $_REQUEST); if ($cancel) { $config = CRM_Core_Config::singleton(); CRM_Utils_System::redirect($config->userFrameworkBaseURL); } $confirm = CRM_Utils_Request::retrieve('confirm', 'Boolean', CRM_Core_DAO::$_nullObject, FALSE, NULL, $_REQUEST); list($displayName, $email) = CRM_Mailing_Event_BAO_Queue::getContactInfo($queue_id); $this->assign('display_name', $displayName); $this->assign('email', $email); $this->assign('confirm', $confirm); $groups = CRM_Mailing_Event_BAO_Unsubscribe::unsub_from_mailing($job_id, $queue_id, $hash, TRUE); $this->assign('groups', $groups); $groupExist = NULL; foreach ($groups as $key => $value) { if ($value) { $groupExist = TRUE; } } $this->assign('groupExist', $groupExist); if ($confirm) { if ($this->_type == 'unsubscribe') { $groups = CRM_Mailing_Event_BAO_Unsubscribe::unsub_from_mailing($job_id, $queue_id, $hash); if (count($groups)) { CRM_Mailing_Event_BAO_Unsubscribe::send_unsub_response($queue_id, $groups, FALSE, $job_id); } else { // should we indicate an error, or just ignore? } } elseif ($this->_type == 'resubscribe') { $groups = CRM_Mailing_Event_BAO_Resubscribe::resub_to_mailing($job_id, $queue_id, $hash); if (count($groups)) { CRM_Mailing_Event_BAO_Resubscribe::send_resub_response($queue_id, $groups, FALSE, $job_id); } else { // should we indicate an error, or just ignore? } } else { if (CRM_Mailing_Event_BAO_Unsubscribe::unsub_from_domain($job_id, $queue_id, $hash)) { CRM_Mailing_Event_BAO_Unsubscribe::send_unsub_response($queue_id, NULL, TRUE, $job_id); } else { // should we indicate an error, or just ignore? } } } else { $confirmURL = CRM_Utils_System::url("civicrm/mailing/{$this->_type}", "reset=1&jid={$job_id}&qid={$queue_id}&h={$hash}&confirm=1"); $this->assign('confirmURL', $confirmURL); // push context for further process CRM-4431 $session = CRM_Core_Session::singleton(); $session->pushUserContext($confirmURL); } return parent::run(); }