/** * Compose a message. * * @param int $job_id * ID of the Job associated with this message. * @param int $event_queue_id * ID of the EventQueue. * @param string $hash * Hash of the EventQueue. * @param string $contactId * ID of the Contact. * @param string $email * Destination address. * @param string $recipient * To: of the recipient. * @param bool $test * Is this mailing a test?. * @param $contactDetails * @param $attachments * @param bool $isForward * Is this mailing compose for forward?. * @param string $fromEmail * Email address of who is forwardinf it. * * @param null $replyToEmail * * @return Mail_mime The mail object */ public function compose($job_id, $event_queue_id, $hash, $contactId, $email, &$recipient, $test, $contactDetails, &$attachments, $isForward = FALSE, $fromEmail = NULL, $replyToEmail = NULL) { $config = CRM_Core_Config::singleton(); $this->getTokens(); if ($this->_domain == NULL) { $this->_domain = CRM_Core_BAO_Domain::getDomain(); } list($verp, $urls, $headers) = $this->getVerpAndUrlsAndHeaders($job_id, $event_queue_id, $hash, $email, $isForward); //set from email who is forwarding it and not original one. if ($fromEmail) { unset($headers['From']); $headers['From'] = "<{$fromEmail}>"; } if ($replyToEmail && $fromEmail != $replyToEmail) { $headers['Reply-To'] = "{$replyToEmail}"; } if ($contactDetails) { $contact = $contactDetails; } elseif ($contactId === 0) { //anonymous user $contact = array(); CRM_Utils_Hook::tokenValues($contact, $contactId, $job_id); } else { $params = array(array('contact_id', '=', $contactId, 0, 0)); list($contact) = CRM_Contact_BAO_Query::apiQuery($params); //CRM-4524 $contact = reset($contact); if (!$contact || is_a($contact, 'CRM_Core_Error')) { CRM_Core_Error::debug_log_message(ts('CiviMail will not send email to a non-existent contact: %1', array(1 => $contactId))); // setting this because function is called by reference //@todo test not calling function by reference $res = NULL; return $res; } // also call the hook to get contact details CRM_Utils_Hook::tokenValues($contact, $contactId, $job_id); } $pTemplates = $this->getPreparedTemplates(); $pEmails = array(); foreach ($pTemplates as $type => $pTemplate) { $html = $type == 'html' ? TRUE : FALSE; $pEmails[$type] = array(); $pEmail =& $pEmails[$type]; $template =& $pTemplates[$type]['template']; $tokens =& $pTemplates[$type]['tokens']; $idx = 0; if (!empty($tokens)) { foreach ($tokens as $idx => $token) { $token_data = $this->getTokenData($token, $html, $contact, $verp, $urls, $event_queue_id); array_push($pEmail, $template[$idx]); array_push($pEmail, $token_data); } } else { array_push($pEmail, $template[$idx]); } if (isset($template[$idx + 1])) { array_push($pEmail, $template[$idx + 1]); } } $html = NULL; if (isset($pEmails['html']) && is_array($pEmails['html']) && count($pEmails['html'])) { $html =& $pEmails['html']; } $text = NULL; if (isset($pEmails['text']) && is_array($pEmails['text']) && count($pEmails['text'])) { $text =& $pEmails['text']; } // push the tracking url on to the html email if necessary if ($this->open_tracking && $html) { array_push($html, "\n" . '<img src="' . $config->userFrameworkResourceURL . "extern/open.php?q={$event_queue_id}\" width='1' height='1' alt='' border='0'>"); } $message = new Mail_mime("\n"); $useSmarty = defined('CIVICRM_MAIL_SMARTY') && CIVICRM_MAIL_SMARTY ? TRUE : FALSE; if ($useSmarty) { $smarty = CRM_Core_Smarty::singleton(); // also add the contact tokens to the template $smarty->assign_by_ref('contact', $contact); } $mailParams = $headers; if ($text && ($test || $contact['preferred_mail_format'] == 'Text' || $contact['preferred_mail_format'] == 'Both' || $contact['preferred_mail_format'] == 'HTML' && !array_key_exists('html', $pEmails))) { $textBody = implode('', $text); if ($useSmarty) { $textBody = $smarty->fetch("string:{$textBody}"); } $mailParams['text'] = $textBody; } if ($html && ($test || ($contact['preferred_mail_format'] == 'HTML' || $contact['preferred_mail_format'] == 'Both'))) { $htmlBody = implode('', $html); if ($useSmarty) { $htmlBody = $smarty->fetch("string:{$htmlBody}"); } $mailParams['html'] = $htmlBody; } if (empty($mailParams['text']) && empty($mailParams['html'])) { // CRM-9833 // something went wrong, lets log it and return null (by reference) CRM_Core_Error::debug_log_message(ts('CiviMail will not send an empty mail body, Skipping: %1', array(1 => $email))); $res = NULL; return $res; } $mailParams['attachments'] = $attachments; $mailingSubject = CRM_Utils_Array::value('subject', $pEmails); if (is_array($mailingSubject)) { $mailingSubject = implode('', $mailingSubject); } $mailParams['Subject'] = $mailingSubject; $mailParams['toName'] = CRM_Utils_Array::value('display_name', $contact); $mailParams['toEmail'] = $email; // Add job ID to mailParams for external email delivery service to utilise $mailParams['job_id'] = $job_id; CRM_Utils_Hook::alterMailParams($mailParams, 'civimail'); // CRM-10699 support custom email headers if (!empty($mailParams['headers'])) { $headers = array_merge($headers, $mailParams['headers']); } //cycle through mailParams and set headers array foreach ($mailParams as $paramKey => $paramValue) { //exclude values not intended for the header if (!in_array($paramKey, array('text', 'html', 'attachments', 'toName', 'toEmail'))) { $headers[$paramKey] = $paramValue; } } if (!empty($mailParams['text'])) { $message->setTxtBody($mailParams['text']); } if (!empty($mailParams['html'])) { $message->setHTMLBody($mailParams['html']); } if (!empty($mailParams['attachments'])) { foreach ($mailParams['attachments'] as $fileID => $attach) { $message->addAttachment($attach['fullPath'], $attach['mime_type'], $attach['cleanName']); } } //pickup both params from mail params. $toName = trim($mailParams['toName']); $toEmail = trim($mailParams['toEmail']); if ($toName == $toEmail || strpos($toName, '@') !== FALSE) { $toName = NULL; } else { $toName = CRM_Utils_Mail::formatRFC2822Name($toName); } $headers['To'] = "{$toName} <{$toEmail}>"; $headers['Precedence'] = 'bulk'; // Will test in the mail processor if the X-VERP is set in the bounced email. // (As an option to replace real VERP for those that can't set it up) $headers['X-CiviMail-Bounce'] = $verp['bounce']; //CRM-5058 //token replacement of subject $headers['Subject'] = $mailingSubject; CRM_Utils_Mail::setMimeParams($message); $headers = $message->headers($headers); //get formatted recipient $recipient = $headers['To']; // make sure we unset a lot of stuff unset($verp); unset($urls); unset($params); unset($contact); unset($ids); return $message; }
/** * 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; }
/** * Send an email from the specified template based on an array of params. * * @param array $params * A string-keyed array of function params, see function body for details. * * @return array * Array of four parameters: a boolean whether the email was sent, and the subject, text and HTML templates */ public static function sendTemplate($params) { $defaults = array('groupName' => NULL, 'valueName' => NULL, 'messageTemplateID' => NULL, 'contactId' => NULL, 'tplParams' => array(), 'from' => NULL, 'toName' => NULL, 'toEmail' => NULL, 'cc' => NULL, 'bcc' => NULL, 'replyTo' => NULL, 'attachments' => NULL, 'isTest' => FALSE, 'PDFFilename' => NULL); $params = array_merge($defaults, $params); CRM_Utils_Hook::alterMailParams($params, 'messageTemplate'); if ((!$params['groupName'] || !$params['valueName']) && !$params['messageTemplateID']) { CRM_Core_Error::fatal(ts("Message template's option group and/or option value or ID missing.")); } if ($params['messageTemplateID']) { // fetch the three elements from the db based on id $query = 'SELECT msg_subject subject, msg_text text, msg_html html, pdf_format_id format FROM civicrm_msg_template mt WHERE mt.id = %1 AND mt.is_default = 1'; $sqlParams = array(1 => array($params['messageTemplateID'], 'String')); } else { // fetch the three elements from the db based on option_group and option_value names $query = 'SELECT msg_subject subject, msg_text text, msg_html html, pdf_format_id format FROM civicrm_msg_template mt JOIN civicrm_option_value ov ON workflow_id = ov.id JOIN civicrm_option_group og ON ov.option_group_id = og.id WHERE og.name = %1 AND ov.name = %2 AND mt.is_default = 1'; $sqlParams = array(1 => array($params['groupName'], 'String'), 2 => array($params['valueName'], 'String')); } $dao = CRM_Core_DAO::executeQuery($query, $sqlParams); $dao->fetch(); if (!$dao->N) { if ($params['messageTemplateID']) { CRM_Core_Error::fatal(ts('No such message template: id=%1.', array(1 => $params['messageTemplateID']))); } else { CRM_Core_Error::fatal(ts('No such message template: option group %1, option value %2.', array(1 => $params['groupName'], 2 => $params['valueName']))); } } $mailContent = array('subject' => $dao->subject, 'text' => $dao->text, 'html' => $dao->html, 'format' => $dao->format); $dao->free(); CRM_Utils_Hook::alterMailContent($mailContent); // add the test banner (if requested) if ($params['isTest']) { $query = "SELECT msg_subject subject, msg_text text, msg_html html\n FROM civicrm_msg_template mt\n JOIN civicrm_option_value ov ON workflow_id = ov.id\n JOIN civicrm_option_group og ON ov.option_group_id = og.id\n WHERE og.name = 'msg_tpl_workflow_meta' AND ov.name = 'test_preview' AND mt.is_default = 1"; $testDao = CRM_Core_DAO::executeQuery($query); $testDao->fetch(); $mailContent['subject'] = $testDao->subject . $mailContent['subject']; $mailContent['text'] = $testDao->text . $mailContent['text']; $mailContent['html'] = preg_replace('/<body(.*)$/im', "<body\\1\n{$testDao->html}", $mailContent['html']); $testDao->free(); } // replace tokens in the three elements (in subject as if it was the text body) $domain = CRM_Core_BAO_Domain::getDomain(); $hookTokens = array(); $mailing = new CRM_Mailing_BAO_Mailing(); $mailing->subject = $mailContent['subject']; $mailing->body_text = $mailContent['text']; $mailing->body_html = $mailContent['html']; $tokens = $mailing->getTokens(); CRM_Utils_Hook::tokens($hookTokens); $categories = array_keys($hookTokens); $contactID = CRM_Utils_Array::value('contactId', $params); if ($contactID) { $contactParams = array('contact_id' => $contactID); $returnProperties = array(); if (isset($tokens['subject']['contact'])) { foreach ($tokens['subject']['contact'] as $name) { $returnProperties[$name] = 1; } } if (isset($tokens['text']['contact'])) { foreach ($tokens['text']['contact'] as $name) { $returnProperties[$name] = 1; } } if (isset($tokens['html']['contact'])) { foreach ($tokens['html']['contact'] as $name) { $returnProperties[$name] = 1; } } // @todo CRM-17253 don't resolve contact details if there are no tokens // effectively comment out this next (performance-expensive) line // but unfortunately testing is a bit think on the ground to that needs to // be added. list($contact) = CRM_Utils_Token::getTokenDetails($contactParams, $returnProperties, FALSE, FALSE, NULL, CRM_Utils_Token::flattenTokens($tokens), 'CRM_Core_BAO_MessageTemplate'); $contact = $contact[$contactID]; } $mailContent['subject'] = CRM_Utils_Token::replaceDomainTokens($mailContent['subject'], $domain, FALSE, $tokens['text'], TRUE); $mailContent['text'] = CRM_Utils_Token::replaceDomainTokens($mailContent['text'], $domain, FALSE, $tokens['text'], TRUE); $mailContent['html'] = CRM_Utils_Token::replaceDomainTokens($mailContent['html'], $domain, TRUE, $tokens['html'], TRUE); if ($contactID) { $mailContent['subject'] = CRM_Utils_Token::replaceContactTokens($mailContent['subject'], $contact, FALSE, $tokens['text'], FALSE, TRUE); $mailContent['text'] = CRM_Utils_Token::replaceContactTokens($mailContent['text'], $contact, FALSE, $tokens['text'], FALSE, TRUE); $mailContent['html'] = CRM_Utils_Token::replaceContactTokens($mailContent['html'], $contact, FALSE, $tokens['html'], FALSE, TRUE); $contactArray = array($contactID => $contact); CRM_Utils_Hook::tokenValues($contactArray, array($contactID), NULL, CRM_Utils_Token::flattenTokens($tokens), 'CRM_Core_BAO_MessageTemplate'); $contact = $contactArray[$contactID]; $mailContent['subject'] = CRM_Utils_Token::replaceHookTokens($mailContent['subject'], $contact, $categories, TRUE); $mailContent['text'] = CRM_Utils_Token::replaceHookTokens($mailContent['text'], $contact, $categories, TRUE); $mailContent['html'] = CRM_Utils_Token::replaceHookTokens($mailContent['html'], $contact, $categories, TRUE); } // strip whitespace from ends and turn into a single line $mailContent['subject'] = "{strip}{$mailContent['subject']}{/strip}"; // parse the three elements with Smarty $smarty = CRM_Core_Smarty::singleton(); foreach ($params['tplParams'] as $name => $value) { $smarty->assign($name, $value); } foreach (array('subject', 'text', 'html') as $elem) { $mailContent[$elem] = $smarty->fetch("string:{$mailContent[$elem]}"); } // send the template, honouring the target user’s preferences (if any) $sent = FALSE; // create the params array $params['subject'] = $mailContent['subject']; $params['text'] = $mailContent['text']; $params['html'] = $mailContent['html']; if ($params['toEmail']) { $contactParams = array(array('email', 'LIKE', $params['toEmail'], 0, 1)); list($contact, $_) = CRM_Contact_BAO_Query::apiQuery($contactParams); $prefs = array_pop($contact); if (isset($prefs['preferred_mail_format']) and $prefs['preferred_mail_format'] == 'HTML') { $params['text'] = NULL; } if (isset($prefs['preferred_mail_format']) and $prefs['preferred_mail_format'] == 'Text') { $params['html'] = NULL; } $config = CRM_Core_Config::singleton(); if (isset($params['isEmailPdf']) && $params['isEmailPdf'] == 1) { $pdfHtml = CRM_Contribute_BAO_ContributionPage::addInvoicePdfToEmail($params['contributionId'], $params['contactId']); if (empty($params['attachments'])) { $params['attachments'] = array(); } $params['attachments'][] = CRM_Utils_Mail::appendPDF('Invoice.pdf', $pdfHtml, $mailContent['format']); } $pdf_filename = ''; if ($config->doNotAttachPDFReceipt && $params['PDFFilename'] && $params['html']) { if (empty($params['attachments'])) { $params['attachments'] = array(); } $params['attachments'][] = CRM_Utils_Mail::appendPDF($params['PDFFilename'], $params['html'], $mailContent['format']); if (isset($params['tplParams']['email_comment'])) { $params['html'] = $params['tplParams']['email_comment']; $params['text'] = strip_tags($params['tplParams']['email_comment']); } } $sent = CRM_Utils_Mail::send($params); if ($pdf_filename) { unlink($pdf_filename); } } return array($sent, $mailContent['subject'], $mailContent['text'], $mailContent['html']); }
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; }
/** * Compose a message * * @param int $job_id ID of the Job associated with this message * @param int $event_queue_id ID of the EventQueue * @param string $hash Hash of the EventQueue * @param string $contactId ID of the Contact * @param string $email Destination address * @param string $recipient To: of the recipient * @param boolean $test Is this mailing a test? * @param boolean $isForward Is this mailing compose for forward? * @param string $fromEmail email address of who is forwardinf it. * @return object The mail object * @access public */ public function &compose($job_id, $event_queue_id, $hash, $contactId, $email, &$recipient, $test, $contactDetails, &$attachments, $isForward = false, $fromEmail = null) { require_once 'api/v2/Contact.php'; require_once 'CRM/Utils/Token.php'; require_once 'CRM/Activity/BAO/Activity.php'; $config = CRM_Core_Config::singleton(); $knownTokens = $this->getTokens(); if ($this->_domain == null) { require_once 'CRM/Core/BAO/Domain.php'; $this->_domain =& CRM_Core_BAO_Domain::getDomain(); } list($verp, $urls, $headers) = $this->getVerpAndUrlsAndHeaders($job_id, $event_queue_id, $hash, $email, $isForward); //set from email who is forwarding it and not original one. if ($fromEmail) { unset($headers['From']); $headers['From'] = "<{$fromEmail}>"; } if (defined('CIVICRM_MAIL_SMARTY')) { require_once 'CRM/Core/Smarty/resources/String.php'; civicrm_smarty_register_string_resource(); } if ($contactDetails) { $contact = $contactDetails; } else { $params = array('contact_id' => $contactId); $contact =& civicrm_contact_get($params); //CRM-4524 $contact = reset($contact); if (!$contact || is_a($contact, 'CRM_Core_Error')) { return null; } // also call the hook to get contact details require_once 'CRM/Utils/Hook.php'; CRM_Utils_Hook::tokenValues($contact, $contactId, $job_id); } $pTemplates = $this->getPreparedTemplates(); $pEmails = array(); foreach ($pTemplates as $type => $pTemplate) { $html = $type == 'html' ? true : false; $pEmails[$type] = array(); $pEmail =& $pEmails[$type]; $template =& $pTemplates[$type]['template']; $tokens =& $pTemplates[$type]['tokens']; $idx = 0; if (!empty($tokens)) { foreach ($tokens as $idx => $token) { $token_data = $this->getTokenData($token, $html, $contact, $verp, $urls, $event_queue_id); array_push($pEmail, $template[$idx]); array_push($pEmail, $token_data); } } else { array_push($pEmail, $template[$idx]); } if (isset($template[$idx + 1])) { array_push($pEmail, $template[$idx + 1]); } } $html = null; if (isset($pEmails['html']) && is_array($pEmails['html']) && count($pEmails['html'])) { $html =& $pEmails['html']; } $text = null; if (isset($pEmails['text']) && is_array($pEmails['text']) && count($pEmails['text'])) { $text =& $pEmails['text']; } // push the tracking url on to the html email if necessary if ($this->open_tracking && $html) { array_push($html, "\n" . '<img src="' . $config->userFrameworkResourceURL . "extern/open.php?q={$event_queue_id}\" width='1' height='1' alt='' border='0'>"); } $message = new Mail_mime("\n"); if (defined('CIVICRM_MAIL_SMARTY')) { $smarty = CRM_Core_Smarty::singleton(); // also add the contact tokens to the template $smarty->assign_by_ref('contact', $contact); } $mailParams = $headers; if ($text && ($test || $contact['preferred_mail_format'] == 'Text' || $contact['preferred_mail_format'] == 'Both' || $contact['preferred_mail_format'] == 'HTML' && !array_key_exists('html', $pEmails))) { $textBody = join('', $text); if (defined('CIVICRM_MAIL_SMARTY')) { $smarty->security = true; $textBody = $smarty->fetch("string:{$textBody}"); $smarty->security = false; } $mailParams['text'] = $textBody; } if ($html && ($test || ($contact['preferred_mail_format'] == 'HTML' || $contact['preferred_mail_format'] == 'Both'))) { $htmlBody = join('', $html); if (defined('CIVICRM_MAIL_SMARTY')) { $smarty->security = true; $htmlBody = $smarty->fetch("string:{$htmlBody}"); $smarty->security = false; } $mailParams['html'] = $htmlBody; } $mailParams['attachments'] = $attachments; $mailingSubject = CRM_Utils_Array::value('subject', $pEmails); if (is_array($mailingSubject)) { $mailingSubject = join('', $mailingSubject); } $mailParams['Subject'] = $mailingSubject; $mailParams['toName'] = $contact['display_name']; $mailParams['toEmail'] = $email; require_once 'CRM/Utils/Hook.php'; CRM_Utils_Hook::alterMailParams($mailParams); if (!empty($mailParams['text'])) { $message->setTxtBody($mailParams['text']); } if (!empty($mailParams['html'])) { $message->setHTMLBody($mailParams['html']); } if (!empty($mailParams['attachments'])) { foreach ($mailParams['attachments'] as $fileID => $attach) { $message->addAttachment($attach['fullPath'], $attach['mime_type'], $attach['cleanName']); } } $headers['To'] = "{$mailParams['toName']} <{$mailParams['toEmail']}>"; $headers['Precedence'] = 'bulk'; // Will test in the mail processor if the X-VERP is set in the bounced email. // (As an option to replace real VERP for those that can't set it up) $headers['X-CiviMail-Bounce'] = $verp['bounce']; //CRM-5058 //token replacement of subject $headers['Subject'] = $mailingSubject; CRM_Utils_Mail::setMimeParams($message); $headers = $message->headers($headers); //get formatted recipient $recipient = $headers['To']; // make sure we unset a lot of stuff unset($verp); unset($urls); unset($params); unset($contact); unset($ids); return $message; }
/** * 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; }