Ejemplo n.º 1
0
/**
 * Implementation of hook_civicrm_alterMailParams( )
 * To send headers in mail and also create activity
 */
function mte_civicrm_alterMailParams(&$params, $context = NULL)
{
    if ($context != 'civimail') {
        CRM_Core_Smarty::singleton()->assign('alterMailer', 'ignore');
    }
    if (!mte_checkSettings($context)) {
        return FALSE;
    }
    $session = CRM_Core_Session::singleton();
    $userID = $session->get('userID');
    $activityTypes = CRM_Core_PseudoConstant::activityType(TRUE, FALSE, FALSE, 'name');
    $params['toEmail'] = trim($params['toEmail']);
    // BRES-103 Prevent silent failure when emails with whitespaces are used.
    if (!$userID) {
        $config = CRM_Core_Config::singleton();
        if (version_compare($config->civiVersion, '4.3.alpha1') < 0) {
            //FIX: source id for version less that 4.3
            $matches = array();
            preg_match('/[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,4}\\b/i', $params['from'], $matches);
            if (!empty($matches)) {
                $userID = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Email', $matches[0], 'contact_id', 'email');
                if (!$userID) {
                    $userID = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Email', $params['toEmail'], 'contact_id', 'email');
                }
            }
        } else {
            $userID = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Domain', CRM_Core_Config::domainID(), 'contact_id');
        }
    }
    if ($context == 'civimail' && CRM_Mte_BAO_Mandrill::$_mailingActivityId) {
        $activityParams = array('id' => CRM_Mte_BAO_Mandrill::$_mailingActivityId, 'target_contact_id' => mte_targetContactIds($params), 'deleteActivityTarget' => FALSE, 'version' => 3);
    } else {
        $activityParams = array('source_contact_id' => $userID, 'activity_type_id' => array_search('Mandrill Email Sent', $activityTypes), 'subject' => CRM_Utils_Array::value('subject', $params) ? $params['subject'] : CRM_Utils_Array::value('Subject', $params), 'activity_date_time' => date('YmdHis'), 'status_id' => 2, 'priority_id' => 1, 'version' => 3, 'details' => CRM_Utils_Array::value('html', $params, $params['text']), 'target_contact_id' => mte_targetContactIds($params));
        if (!empty($params['job_id'])) {
            $jobCLassName = 'CRM_Mailing_DAO_MailingJob';
            if (version_compare('4.4alpha1', CRM_Core_Config::singleton()->civiVersion) > 0) {
                $jobCLassName = 'CRM_Mailing_DAO_Job';
            }
            $activityParams['source_record_id'] = CRM_Core_DAO::getFieldValue($jobCLassName, $params['job_id'], 'mailing_id');
        }
    }
    $result = civicrm_api('activity', 'create', $activityParams);
    if (CRM_Utils_Array::value('id', $result)) {
        $params['activityId'] = $mandrillHeader = $result['id'];
        // include verp in header incase of bulk mailing
        if ($context == 'civimail') {
            $mandrillHeader .= CRM_Core_Config::singleton()->verpSeparator . CRM_Utils_Array::value('Return-Path', $params);
        } else {
            mte_createQueue($mandrillHeader, $params['toEmail']);
            $params['mandrillHeader'] = $mandrillHeader;
        }
        $params['headers']['X-MC-Metadata'] = '{"CiviCRM_Mandrill_id": "' . $mandrillHeader . '" }';
        CRM_Core_Smarty::singleton()->assign('alterMailer', 1);
        if ($context == 'civimail' && !CRM_Mte_BAO_Mandrill::$_mailingActivityId) {
            CRM_Mte_BAO_Mandrill::$_mailingActivityId = $result['id'];
        }
        if (!method_exists(CRM_Utils_Hook::singleton(), 'alterMail')) {
            $mailer =& CRM_Core_Config::getMailer();
            mte_getmailer($mailer);
        }
    }
}