/** * 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; }
/** * Synchronize the object with the UF Match entry. Can be called stand-alone from * the drupalUsers script * * @param Object $user the drupal user object * @param string $userKey the id of the user from the uf object * @param string $mail the email address of the user * @param string $uf the name of the user framework * @param integer $status returns the status if user created or already exits (used for drupal sync) * * @return the ufmatch object that was found or created * @access public * @static */ function &synchronizeUFMatch(&$user, $userKey, $mail, $uf, $status = null) { // validate that mail is a valid email address. Drupal does not check for this stuff require_once 'CRM/Utils/Rule.php'; if (!CRM_Utils_Rule::email($mail)) { return $status ? null : false; } $newContact = false; // make sure that a contact id exists for this user id $ufmatch =& new CRM_Core_DAO_UFMatch(); $ufmatch->uf_id = $userKey; $ufmatch->domain_id = CRM_Core_Config::domainID(); if (!$ufmatch->find(true)) { $query = "\nSELECT civicrm_contact.id as contact_id, civicrm_contact.domain_id as domain_id\nFROM civicrm_contact\nLEFT JOIN civicrm_location ON ( civicrm_location.entity_table = 'civicrm_contact' AND\n civicrm_contact.id = civicrm_location.entity_id AND \n civicrm_location.is_primary = 1 )\nLEFT JOIN civicrm_email ON ( civicrm_location.id = civicrm_email.location_id AND civicrm_email.is_primary = 1 )\nWHERE civicrm_email.email = '" . CRM_Utils_Type::escape($mail, 'String') . "' AND civicrm_contact.domain_id = " . CRM_Core_Config::domainID(); $dao =& new CRM_Core_DAO(); $dao->query($query); if ($dao->fetch()) { $ufmatch->contact_id = $dao->contact_id; $ufmatch->domain_id = $dao->domain_id; $ufmatch->email = $mail; } else { if ($uf == 'Mambo') { CRM_Utils_System_Mambo::setEmail($user); } require_once 'CRM/Core/BAO/LocationType.php'; $locationType =& CRM_Core_BAO_LocationType::getDefault(); //CRM_Core_Error::debug('M', $mail); $params = array('email' => $mail, 'location_type' => $locationType->name); $contact =& crm_create_contact($params, 'Individual'); if (is_a($contact, 'CRM_Core_Error')) { //CRM_Core_Error::debug( 'error', $contact ); exit(1); } $ufmatch->contact_id = $contact->id; $ufmatch->domain_id = $contact->domain_id; $ufmatch->email = $mail; } $ufmatch->save(); $newContact = true; } return $status ? $newContact : $ufmatch; }