get('mailing_backend'); if ($mailingInfo['outBound_option'] == CRM_Mailing_Config::OUTBOUND_OPTION_REDIRECT_TO_DB || (defined('CIVICRM_MAILER_SPOOL') && CIVICRM_MAILER_SPOOL) ) { $mailer = self::_createMailer('CRM_Mailing_BAO_Spool', array()); } elseif ($mailingInfo['outBound_option'] == CRM_Mailing_Config::OUTBOUND_OPTION_SMTP) { if ($mailingInfo['smtpServer'] == '' || !$mailingInfo['smtpServer']) { CRM_Core_Error::debug_log_message(ts('There is no valid smtp server setting. Click Administer >> System Setting >> Outbound Email to set the SMTP Server.', array(1 => CRM_Utils_System::url('civicrm/admin/setting/smtp', 'reset=1')))); CRM_Core_Error::fatal(ts('There is no valid smtp server setting. Click Administer >> System Setting >> Outbound Email to set the SMTP Server.', array(1 => CRM_Utils_System::url('civicrm/admin/setting/smtp', 'reset=1')))); } $params['host'] = $mailingInfo['smtpServer'] ? $mailingInfo['smtpServer'] : 'localhost'; $params['port'] = $mailingInfo['smtpPort'] ? $mailingInfo['smtpPort'] : 25; if ($mailingInfo['smtpAuth']) { $params['username'] = $mailingInfo['smtpUsername']; $params['password'] = CRM_Utils_Crypt::decrypt($mailingInfo['smtpPassword']); $params['auth'] = TRUE; } else { $params['auth'] = FALSE; } /* * Set the localhost value, CRM-3153 * Use the host name of the web server, falling back to the base URL * (eg when using the PHP CLI), and then falling back to localhost. */ $params['localhost'] = CRM_Utils_Array::value( 'SERVER_NAME', $_SERVER, CRM_Utils_Array::value( 'host', parse_url(CIVICRM_UF_BASEURL), 'localhost' ) ); // also set the timeout value, lets set it to 30 seconds // CRM-7510 $params['timeout'] = 30; // CRM-9349 $params['persist'] = TRUE; $mailer = self::_createMailer('smtp', $params); } elseif ($mailingInfo['outBound_option'] == CRM_Mailing_Config::OUTBOUND_OPTION_SENDMAIL) { if ($mailingInfo['sendmail_path'] == '' || !$mailingInfo['sendmail_path'] ) { CRM_Core_Error::debug_log_message(ts('There is no valid sendmail path setting. Click Administer >> System Setting >> Outbound Email to set the sendmail server.', array(1 => CRM_Utils_System::url('civicrm/admin/setting/smtp', 'reset=1')))); CRM_Core_Error::fatal(ts('There is no valid sendmail path setting. Click Administer >> System Setting >> Outbound Email to set the sendmail server.', array(1 => CRM_Utils_System::url('civicrm/admin/setting/smtp', 'reset=1')))); } $params['sendmail_path'] = $mailingInfo['sendmail_path']; $params['sendmail_args'] = $mailingInfo['sendmail_args']; $mailer = self::_createMailer('sendmail', $params); } elseif ($mailingInfo['outBound_option'] == CRM_Mailing_Config::OUTBOUND_OPTION_MAIL) { $mailer = self::_createMailer('mail', array()); } elseif ($mailingInfo['outBound_option'] == CRM_Mailing_Config::OUTBOUND_OPTION_MOCK) { $mailer = self::_createMailer('mock', array()); } elseif ($mailingInfo['outBound_option'] == CRM_Mailing_Config::OUTBOUND_OPTION_DISABLED) { CRM_Core_Error::debug_log_message(ts('Outbound mail has been disabled. Click Administer >> System Setting >> Outbound Email to set the OutBound Email.', array(1 => CRM_Utils_System::url('civicrm/admin/setting/smtp', 'reset=1')))); CRM_Core_Error::statusBounce(ts('Outbound mail has been disabled. Click Administer >> System Setting >> Outbound Email to set the OutBound Email.', array(1 => CRM_Utils_System::url('civicrm/admin/setting/smtp', 'reset=1')))); } else { CRM_Core_Error::debug_log_message(ts('There is no valid SMTP server Setting Or SendMail path setting. Click Administer >> System Setting >> Outbound Email to set the OutBound Email.', array(1 => CRM_Utils_System::url('civicrm/admin/setting/smtp', 'reset=1')))); CRM_Core_Error::debug_var('mailing_info', $mailingInfo); CRM_Core_Error::statusBounce(ts('There is no valid SMTP server Setting Or sendMail path setting. Click Administer >> System Setting >> Outbound Email to set the OutBound Email.', array(1 => CRM_Utils_System::url('civicrm/admin/setting/smtp', 'reset=1')))); } return $mailer; } /** * Create a new instance of a PEAR Mail driver. * * @param string $driver * 'CRM_Mailing_BAO_Spool' or a name suitable for Mail::factory(). * @param array $params * @return object * More specifically, a class which implements the "send()" function */ public static function _createMailer($driver, $params) { if ($driver == 'CRM_Mailing_BAO_Spool') { $mailer = new CRM_Mailing_BAO_Spool($params); } else { $mailer = Mail::factory($driver, $params); } CRM_Utils_Hook::alterMailer($mailer, $driver, $params); return $mailer; } /** * 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). * * @return bool * TRUE if a mail was sent, else FALSE. */ public static function send(&$params) { $defaultReturnPath = 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 (!$defaultReturnPath) { $defaultReturnPath = self::pluckEmailFromHeader($from); } // first call the mail alter hook CRM_Utils_Hook::alterMailParams($params, 'singleEmail'); // check if any module has aborted mail sending if (!empty($params['abortMailSend']) || empty($params['toEmail'])) { 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 (!empty($params['headers'])) { $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, $defaultReturnPath); // CRM-11295: Omit reply-to headers if empty; this avoids issues with overzealous mailservers $replyTo = CRM_Utils_Array::value('replyTo', $params, CRM_Utils_Array::value('from', $params)); if (!empty($replyTo)) { $headers['Reply-To'] = $replyTo; } $headers['Date'] = date('r'); if ($includeMessageId) { $headers['Message-ID'] = '<' . uniqid('civicrm_', TRUE) . "@$emailDomain>"; } 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 = \Civi::service('pear_mail'); // CRM-3795, CRM-7355, CRM-7557, CRM-9058, CRM-9887, CRM-12883, CRM-19173 and others ... // The PEAR library requires different parameters based on the mailer used: // * Mail_mail requires the Cc/Bcc recipients listed ONLY in the $headers variable // * All other mailers require that all be recipients be listed in the $to array AND that // the Bcc must not be present in $header as otherwise it will be shown to all recipients // ref: https://pear.php.net/bugs/bug.php?id=8047, full thread and answer [2011-04-19 20:48 UTC] 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); unset($headers['Bcc']); } } 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 */ public 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 .= '