"; } if (!empty($params['autoSubmitted'])) { $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']); $result = null; $mailer =& CRM_Core_Config::getMailer( ); // Mail_smtp and Mail_sendmail mailers require Bcc anc Cc emails // be included in both $to and $headers['Cc', 'Bcc'] if (get_class($mailer) != "Mail_mail") { //get emails from headers, since these are //combination of name and email addresses. if (!empty($headers['Cc'])) { $to[] = CRM_Utils_Array::value( 'Cc', $headers ); } if (!empty($headers['Bcc'])) { $to[] = CRM_Utils_Array::value( 'Bcc', $headers ); } } if (is_object($mailer)) { $errorScope = CRM_Core_TemporaryErrorScope::ignoreException(); $result = $mailer->send($to, $headers, $message); 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, ts('Mailing Error'), 'error'); return FALSE; } // CRM-10699 CRM_Utils_Hook::postEmailSend($params); return TRUE; } return FALSE; } /** * @param $mailer * @param $result * * @return string */ static function errorMessage($mailer, $result) { $message = '
' . ts('An error occurred when CiviCRM attempted to send an email (via %1). If you received this error after submitting on online contribution or event registration - the transaction was completed, but we were unable to send the email receipt.', array( 1 => 'SMTP')) . '
' . '' . ts('The mail library returned the following error message:') . '
' . $result->getMessage() . '
' . ts('This is probably related to a problem in your Outbound Email Settings (Administer CiviCRM » System Settings » Outbound Email), OR the FROM email address specifically configured for your contribution page or event. Possible causes are:') . '
'; if (is_a($mailer, 'Mail_smtp')) { $message .= '' . ts('Check this page for more information.', array( 1 => CRM_Utils_System::docURL2('user/advanced-configuration/email-system-configuration', TRUE))) . '
'; return $message; } /** * @param $to * @param $headers * @param $message */ static function logger(&$to, &$headers, &$message) { if (is_array($to)) { $toString = implode(', ', $to); $fileName = $to[0]; } else { $toString = $fileName = $to; } $content = "To: " . $toString . "\n"; foreach ($headers as $key => $val) { $content .= "$key: $val\n"; } $content .= "\n" . $message . "\n"; if (is_numeric(CIVICRM_MAIL_LOG)) { $config = CRM_Core_Config::singleton(); // create the directory if not there $dirName = $config->configAndLogDir . 'mail' . DIRECTORY_SEPARATOR; CRM_Utils_File::createDir($dirName); $fileName = md5(uniqid(CRM_Utils_String::munge($fileName))) . '.txt'; file_put_contents($dirName . $fileName, $content ); } else { file_put_contents(CIVICRM_MAIL_LOG, $content, FILE_APPEND); } } /** * Get the email address itself from a formatted full name + address string * * Ugly but working. * * @param string $header the full name + email address string * * @return string the plucked email address * @static */ static function pluckEmailFromHeader($header) { preg_match('/<([^<]*)>$/', $header, $matches); if (isset($matches[1])) { return $matches[1]; } return NULL; } /** * Get the Active outBound email * * @return boolean true if valid outBound email configuration found, false otherwise * @access public * @static */ static function validOutBoundMail() { $mailingInfo = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::MAILING_PREFERENCES_NAME, 'mailing_backend' ); if ($mailingInfo['outBound_option'] == CRM_Mailing_Config::OUTBOUND_OPTION_MAIL) { return TRUE; } elseif ($mailingInfo['outBound_option'] == CRM_Mailing_Config::OUTBOUND_OPTION_SMTP) { if (!isset($mailingInfo['smtpServer']) || $mailingInfo['smtpServer'] == '' || $mailingInfo['smtpServer'] == 'YOUR SMTP SERVER' || ($mailingInfo['smtpAuth'] && ($mailingInfo['smtpUsername'] == '' || $mailingInfo['smtpPassword'] == '')) ) { return FALSE; } return TRUE; } elseif ($mailingInfo['outBound_option'] == CRM_Mailing_Config::OUTBOUND_OPTION_SENDMAIL) { if (!$mailingInfo['sendmail_path'] || !$mailingInfo['sendmail_args']) { return FALSE; } return TRUE; } elseif ($mailingInfo['outBound_option'] == CRM_Mailing_Config::OUTBOUND_OPTION_REDIRECT_TO_DB) { return TRUE; } return FALSE; } /** * @param $message * @param null $params * * @return mixed */ static function &setMimeParams(&$message, $params = NULL) { static $mimeParams = NULL; if (!$params) { if (!$mimeParams) { $mimeParams = array( 'text_encoding' => '8bit', 'html_encoding' => '8bit', 'head_charset' => 'utf-8', 'text_charset' => 'utf-8', 'html_charset' => 'utf-8', ); } $params = $mimeParams; } return $message->get($params); } /** * @param $name * @param $email * @param bool $useQuote * * @return null|string */ static function formatRFC822Email($name, $email, $useQuote = FALSE) { $result = NULL; $name = trim($name); // strip out double quotes if present at the beginning AND end if (substr($name, 0, 1) == '"' && substr($name, -1, 1) == '"' ) { $name = substr($name, 1, -1); } if (!empty($name)) { // escape the special characters $name = str_replace(array('<', '"', '>'), array('\<', '\"', '\>'), $name ); if (strpos($name, ',') !== FALSE || $useQuote ) { // quote the string if it has a comma $name = '"' . $name . '"'; } $result = "$name "; } $result .= "<{$email}>"; return $result; } /** * Takes a string and checks to see if it needs to be escaped / double quoted * and if so does the needful and return the formatted name * * This code has been copied and adapted from ezc/Mail/src/tools.php */ static function formatRFC2822Name($name) { $name = trim($name); if (!empty($name)) { // remove the quotes around the name part if they are already there if (substr($name, 0, 1) == '"' && substr($name, -1) == '"') { $name = substr($name, 1, -1); } // add slashes to " and \ and surround the name part with quotes if (strpbrk($name, ",@<>:;'\"") !== FALSE) { $name = '"' . addcslashes($name, '\\"') . '"'; } } return $name; } /** * @param string $fileName * @param string $html * @param string $format * * @return array $attachments */ static function appendPDF($fileName, $html, $format = NULL) { $pdf_filename = CRM_Core_Config::singleton()->templateCompileDir . CRM_Utils_File::makeFileName($fileName); //FIXME : CRM-7894 //xmlns attribute is required in XHTML but it is invalid in HTML, //Also the namespace "xmlns=http://www.w3.org/1999/xhtml" is default, //and will be added to the tag even if you do not include it. $html = preg_replace('/()/', '\1\3\4', $html); file_put_contents($pdf_filename, CRM_Utils_PDF_Utils::html2pdf($html, $fileName, TRUE, $format) ); return array( 'fullPath' => $pdf_filename, 'mime_type' => 'application/pdf', 'cleanName' => $fileName, ); } }