示例#1
0
文件: Mail.php 项目: ksecor/civicrm
 static function send($from, $toDisplayName, $toEmail, $subject, $text_message = null, $cc = null, $bcc = null, $replyTo = null, $html_message = null, $attachments = null)
 {
     require_once 'CRM/Core/BAO/MailSettings.php';
     $returnPath = CRM_Core_BAO_MailSettings::defaultReturnPath();
     if (!$returnPath) {
         $returnPath = self::pluckEmailFromHeader($from);
     }
     $headers = array();
     $headers['From'] = $from;
     $headers['To'] = "{$toDisplayName} <{$toEmail}>";
     $headers['Cc'] = $cc;
     $headers['Subject'] = $subject;
     $headers['Content-Type'] = $html_message ? 'multipart/mixed; charset=utf-8' : 'text/plain; charset=utf-8';
     $headers['Content-Disposition'] = 'inline';
     $headers['Content-Transfer-Encoding'] = '8bit';
     $headers['Return-Path'] = $returnPath;
     $headers['Reply-To'] = isset($replyTo) ? $replyTo : $from;
     $headers['Date'] = date('r');
     $to = array($toEmail);
     if ($cc) {
         $to[] = $cc;
     }
     if ($bcc) {
         $to[] = $bcc;
     }
     // we need to wrap Mail_mime because PEAR is apparently unable to fix
     // a six-year-old bug (PEAR bug #30) in Mail_mime::_encodeHeaders()
     // this fixes CRM-4631
     require_once 'CRM/Utils/Mail/FixedMailMIME.php';
     $msg = new CRM_Utils_Mail_FixedMailMIME("\n");
     if ($text_message) {
         $msg->setTxtBody($text_message);
     }
     if ($html_message) {
         $msg->setHTMLBody($html_message);
     }
     if (!empty($attachments)) {
         foreach ($attachments as $fileID => $attach) {
             $msg->addAttachment($attach['fullPath'], $attach['mime_type'], $attach['cleanName']);
         }
     }
     $message = self::setMimeParams($msg);
     $headers =& $msg->headers($headers);
     $result = null;
     $mailer =& CRM_Core_Config::getMailer();
     CRM_Core_Error::ignoreException();
     if (is_object($mailer)) {
         $result = $mailer->send($to, $headers, $message);
         CRM_Core_Error::setCallback();
         if (is_a($result, 'PEAR_Error')) {
             $message = self::errorMessage($mailer, $result);
             CRM_Core_Session::setStatus($message, false);
             return false;
         }
         return true;
     }
     return false;
 }
 /**
  * Wrapper function to send mail in CiviCRM. Hooks are called from this function. The input parameter
  * is an associateive array which holds the values of field needed to send an email. These are:
  *
  * from    : complete from envelope
  * toName  : name of person to send email
  * toEmail : email address to send to
  * cc      : email addresses to cc
  * bcc     : email addresses to bcc
  * subject : subject of the email
  * text    : text of the message
  * html    : html version of the message
  * replyTo : reply-to header in the email
  * attachments: an associative array of
  *   fullPath : complete pathname to the file
  *   mime_type: mime type of the attachment
  *   cleanName: the user friendly name of the attachmment
  *
  * @param array $params (by reference)
  *
  * @access public
  *
  * @return boolean true if a mail was sent, else false
  */
 static function send(&$params)
 {
     $returnPath = CRM_Core_BAO_MailSettings::defaultReturnPath();
     $includeMessageId = CRM_Core_BAO_MailSettings::includeMessageId();
     $emailDomain = CRM_Core_BAO_MailSettings::defaultDomain();
     $from = CRM_Utils_Array::value('from', $params);
     if (!$returnPath) {
         $returnPath = self::pluckEmailFromHeader($from);
     }
     $params['returnPath'] = $returnPath;
     // first call the mail alter hook
     CRM_Utils_Hook::alterMailParams($params);
     // check if any module has aborted mail sending
     if (CRM_Utils_Array::value('abortMailSend', $params) || !CRM_Utils_Array::value('toEmail', $params)) {
         return FALSE;
     }
     $textMessage = CRM_Utils_Array::value('text', $params);
     $htmlMessage = CRM_Utils_Array::value('html', $params);
     $attachments = CRM_Utils_Array::value('attachments', $params);
     // CRM-6224
     if (trim(CRM_Utils_String::htmlToText($htmlMessage)) == '') {
         $htmlMessage = FALSE;
     }
     $headers = array();
     // CRM-10699 support custom email headers
     if (CRM_Utils_Array::value('headers', $params)) {
         $headers = array_merge($headers, $params['headers']);
     }
     $headers['From'] = $params['from'];
     $headers['To'] = self::formatRFC822Email(CRM_Utils_Array::value('toName', $params), CRM_Utils_Array::value('toEmail', $params), FALSE);
     $headers['Cc'] = CRM_Utils_Array::value('cc', $params);
     $headers['Bcc'] = CRM_Utils_Array::value('bcc', $params);
     $headers['Subject'] = CRM_Utils_Array::value('subject', $params);
     $headers['Content-Type'] = $htmlMessage ? 'multipart/mixed; charset=utf-8' : 'text/plain; charset=utf-8';
     $headers['Content-Disposition'] = 'inline';
     $headers['Content-Transfer-Encoding'] = '8bit';
     $headers['Return-Path'] = CRM_Utils_Array::value('returnPath', $params);
     // CRM-11295: Omit reply-to headers if empty; this avoids issues with overzealous mailservers
     $replyTo = CRM_Utils_Array::value('replyTo', $params, $from);
     if (!empty($replyTo)) {
         $headers['Reply-To'] = $replyTo;
     }
     $headers['Date'] = date('r');
     if ($includeMessageId) {
         $headers['Message-ID'] = '<' . uniqid('civicrm_', TRUE) . "@{$emailDomain}>";
     }
     if (CRM_Utils_Array::value('autoSubmitted', $params)) {
         $headers['Auto-Submitted'] = "Auto-Generated";
     }
     //make sure we has to have space, CRM-6977
     foreach (array('From', 'To', 'Cc', 'Bcc', 'Reply-To', 'Return-Path') as $fld) {
         if (isset($headers[$fld])) {
             $headers[$fld] = str_replace('"<', '" <', $headers[$fld]);
         }
     }
     // quote FROM, if comma is detected AND is not already quoted. CRM-7053
     if (strpos($headers['From'], ',') !== FALSE) {
         $from = explode(' <', $headers['From']);
         $headers['From'] = self::formatRFC822Email($from[0], substr(trim($from[1]), 0, -1), TRUE);
     }
     require_once 'Mail/mime.php';
     $msg = new Mail_mime("\n");
     if ($textMessage) {
         $msg->setTxtBody($textMessage);
     }
     if ($htmlMessage) {
         $msg->setHTMLBody($htmlMessage);
     }
     if (!empty($attachments)) {
         foreach ($attachments as $fileID => $attach) {
             $msg->addAttachment($attach['fullPath'], $attach['mime_type'], $attach['cleanName']);
         }
     }
     $message = self::setMimeParams($msg);
     $headers =& $msg->headers($headers);
     $to = array($params['toEmail']);
     //get emails from headers, since these are
     //combination of name and email addresses.
     if (CRM_Utils_Array::value('Cc', $headers)) {
         $to[] = CRM_Utils_Array::value('Cc', $headers);
     }
     if (CRM_Utils_Array::value('Bcc', $headers)) {
         $to[] = CRM_Utils_Array::value('Bcc', $headers);
         unset($headers['Bcc']);
     }
     $result = NULL;
     $mailer = CRM_Core_Config::getMailer();
     CRM_Core_Error::ignoreException();
     if (is_object($mailer)) {
         $result = $mailer->send($to, $headers, $message);
         CRM_Core_Error::setCallback();
         if (is_a($result, 'PEAR_Error')) {
             $message = self::errorMessage($mailer, $result);
             // append error message in case multiple calls are being made to
             // this method in the course of sending a batch of messages.
             CRM_Core_Session::setStatus($message, TRUE);
             return FALSE;
         }
         // CRM-10699
         CRM_Utils_Hook::postEmailSend($params);
         return TRUE;
     }
     return FALSE;
 }
示例#3
0
 function send($from, $toDisplayName, $toEmail, $subject, $message, $cc = null, $bcc = null)
 {
     require_once 'CRM/Core/DAO/Domain.php';
     $dao = new CRM_Core_DAO_Domain();
     $dao->id = 1;
     $dao->find(true);
     $returnPath = $dao->email_return_path;
     if (!$returnPath) {
         $returnPath = CRM_Utils_Mail::_pluckEmailFromHeader($from);
     }
     $headers = array();
     $headers['From'] = $from;
     $headers['To'] = CRM_Utils_Mail::encodeAddressHeader($toDisplayName, $toEmail);
     $headers['Cc'] = $cc;
     $headers['Bcc'] = $bcc;
     $headers['Subject'] = CRM_Utils_Mail::encodeSubjectHeader($subject);
     $headers['Content-Type'] = 'text/plain; charset=utf-8';
     $headers['Content-Disposition'] = 'inline';
     $headers['Content-Transfer-Encoding'] = '8bit';
     $headers['Return-Path'] = $returnPath;
     $headers['Reply-To'] = $from;
     $to = array($toEmail);
     if ($cc) {
         $to[] = $cc;
     }
     if ($bcc) {
         $to[] = $bcc;
     }
     $mailer =& CRM_Core_Config::getMailer();
     if ($mailer->send($to, $headers, $message) !== true) {
         return false;
     }
     return true;
 }
示例#4
0
文件: Mail.php 项目: bhirsch/civicrm
 static function send(&$params)
 {
     require_once 'CRM/Core/BAO/MailSettings.php';
     $returnPath = CRM_Core_BAO_MailSettings::defaultReturnPath();
     $from = CRM_Utils_Array::value('from', $params);
     if (!$returnPath) {
         $returnPath = self::pluckEmailFromHeader($from);
     }
     $params['returnPath'] = $returnPath;
     // first call the mail alter hook
     require_once 'CRM/Utils/Hook.php';
     CRM_Utils_Hook::alterMailParams($params);
     // check if any module has aborted mail sending
     if (CRM_Utils_Array::value('abortMailSend', $params) || !CRM_Utils_Array::value('toEmail', $params)) {
         return false;
     }
     $textMessage = CRM_Utils_Array::value('text', $params);
     $htmlMessage = CRM_Utils_Array::value('html', $params);
     $attachments = CRM_Utils_Array::value('attachments', $params);
     $headers = array();
     $headers['From'] = $params['from'];
     $headers['To'] = "{$params['toName']} <{$params['toEmail']}>";
     $headers['Cc'] = CRM_Utils_Array::value('cc', $params);
     $headers['Subject'] = CRM_Utils_Array::value('subject', $params);
     $headers['Content-Type'] = $htmlMessage ? 'multipart/mixed; charset=utf-8' : 'text/plain; charset=utf-8';
     $headers['Content-Disposition'] = 'inline';
     $headers['Content-Transfer-Encoding'] = '8bit';
     $headers['Return-Path'] = CRM_Utils_Array::value('returnPath', $params);
     $headers['Reply-To'] = CRM_Utils_Array::value('replyTo', $params, $from);
     $headers['Date'] = date('r');
     $to = array($params['toEmail']);
     if (CRM_Utils_Array::value('cc', $params)) {
         $to[] = CRM_Utils_Array::value('cc', $params);
     }
     if (CRM_Utils_Array::value('bcc', $params)) {
         $to[] = CRM_Utils_Array::value('bcc', $params);
     }
     // we need to wrap Mail_mime because PEAR is apparently unable to fix
     // a six-year-old bug (PEAR bug #30) in Mail_mime::_encodeHeaders()
     // this fixes CRM-4631
     require_once 'CRM/Utils/Mail/FixedMailMIME.php';
     $msg = new CRM_Utils_Mail_FixedMailMIME("\n");
     if ($textMessage) {
         $msg->setTxtBody($textMessage);
     }
     if ($htmlMessage) {
         $msg->setHTMLBody($htmlMessage);
     }
     if (!empty($attachments)) {
         foreach ($attachments as $fileID => $attach) {
             $msg->addAttachment($attach['fullPath'], $attach['mime_type'], $attach['cleanName']);
         }
     }
     $message = self::setMimeParams($msg);
     $headers =& $msg->headers($headers);
     $result = null;
     $mailer =& CRM_Core_Config::getMailer();
     CRM_Core_Error::ignoreException();
     if (is_object($mailer)) {
         $result = $mailer->send($to, $headers, $message);
         CRM_Core_Error::setCallback();
         if (is_a($result, 'PEAR_Error')) {
             $message = self::errorMessage($mailer, $result);
             CRM_Core_Session::setStatus($message, false);
             return false;
         }
         return true;
     }
     return false;
 }
示例#5
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);
        }
    }
}
示例#6
0
 /**
  * Wrapper function to send mail in CiviCRM. Hooks are called from this function. The input parameter
  * is an associateive array which holds the values of field needed to send an email. These are:
  *
  * from    : complete from envelope
  * toName  : name of person to send email
  * toEmail : email address to send to
  * cc      : email addresses to cc
  * bcc     : email addresses to bcc
  * subject : subject of the email
  * text    : text of the message
  * html    : html version of the message
  * replyTo : reply-to header in the email
  * attachments: an associative array of
  *   fullPath : complete pathname to the file
  *   mime_type: mime type of the attachment
  *   cleanName: the user friendly name of the attachmment
  *
  * @param array $params (by reference)
  * 
  * @access public
  * @return boolean true if a mail was sent, else false
  */
 static function send(&$params)
 {
     require_once 'CRM/Core/BAO/MailSettings.php';
     $returnPath = CRM_Core_BAO_MailSettings::defaultReturnPath();
     $from = CRM_Utils_Array::value('from', $params);
     if (!$returnPath) {
         $returnPath = self::pluckEmailFromHeader($from);
     }
     $params['returnPath'] = $returnPath;
     // first call the mail alter hook
     require_once 'CRM/Utils/Hook.php';
     CRM_Utils_Hook::alterMailParams($params);
     // check if any module has aborted mail sending
     if (CRM_Utils_Array::value('abortMailSend', $params) || !CRM_Utils_Array::value('toEmail', $params)) {
         return false;
     }
     $textMessage = CRM_Utils_Array::value('text', $params);
     $htmlMessage = CRM_Utils_Array::value('html', $params);
     $attachments = CRM_Utils_Array::value('attachments', $params);
     // CRM-6224
     if (trim(CRM_Utils_String::htmlToText($htmlMessage)) == '') {
         $htmlMessage = false;
     }
     $headers = array();
     $headers['From'] = $params['from'];
     $headers['To'] = "{$params['toName']} <{$params['toEmail']}>";
     $headers['Cc'] = CRM_Utils_Array::value('cc', $params);
     $headers['Bcc'] = CRM_Utils_Array::value('bcc', $params);
     $headers['Subject'] = CRM_Utils_Array::value('subject', $params);
     $headers['Content-Type'] = $htmlMessage ? 'multipart/mixed; charset=utf-8' : 'text/plain; charset=utf-8';
     $headers['Content-Disposition'] = 'inline';
     $headers['Content-Transfer-Encoding'] = '8bit';
     $headers['Return-Path'] = CRM_Utils_Array::value('returnPath', $params);
     $headers['Reply-To'] = CRM_Utils_Array::value('replyTo', $params, $from);
     $headers['Date'] = date('r');
     if (CRM_Utils_Array::value('autoSubmitted', $params)) {
         $headers['Auto-Submitted'] = "Auto-Generated";
     }
     //make sure we has to have space, CRM-6977
     foreach (array('From', 'To', 'Cc', 'Bcc', 'Reply-To', 'Return-Path') as $fld) {
         $headers[$fld] = str_replace('"<', '" <', $headers[$fld]);
     }
     // quote FROM, if comma is detected AND is not already quoted. CRM-7053
     if (strpos($headers['From'], ',') !== false) {
         $from = explode(' <', $headers['From']);
         if (substr($from[0], 0, 1) != '"' || substr($from[0], -1, 1) != '"') {
             $from[0] = str_replace('"', '\\"', $from[0]);
             $headers['From'] = "\"{$from[0]}\" <{$from[1]}";
         }
     }
     require_once 'Mail/mime.php';
     $msg = new Mail_mime("\n");
     if ($textMessage) {
         $msg->setTxtBody($textMessage);
     }
     if ($htmlMessage) {
         $msg->setHTMLBody($htmlMessage);
     }
     if (!empty($attachments)) {
         foreach ($attachments as $fileID => $attach) {
             $msg->addAttachment($attach['fullPath'], $attach['mime_type'], $attach['cleanName']);
         }
     }
     $message =& self::setMimeParams($msg);
     $headers =& $msg->headers($headers);
     $to = array($params['toEmail']);
     //get emails from headers, since these are
     //combination of name and email addresses.
     if (CRM_Utils_Array::value('Cc', $headers)) {
         $to[] = CRM_Utils_Array::value('Cc', $headers);
     }
     if (CRM_Utils_Array::value('Bcc', $headers)) {
         $to[] = CRM_Utils_Array::value('Bcc', $headers);
         unset($headers['Bcc']);
     }
     $result = null;
     $mailer =& CRM_Core_Config::getMailer();
     CRM_Core_Error::ignoreException();
     if (is_object($mailer)) {
         $result = $mailer->send($to, $headers, $message);
         CRM_Core_Error::setCallback();
         if (is_a($result, 'PEAR_Error')) {
             $message = self::errorMessage($mailer, $result);
             CRM_Core_Session::setStatus($message, false);
             return false;
         }
         return true;
     }
     return false;
 }