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', []); } 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.', [1 => CRM_Utils_System::url('civicrm/admin/setting/smtp', 'reset=1')])); throw new CRM_Core_Exception(ts('There is no valid smtp server setting. Click Administer >> System Setting >> Outbound Email to set the SMTP Server.', [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.', [1 => CRM_Utils_System::url('civicrm/admin/setting/smtp', 'reset=1')])); throw new CRM_Core_Exception(ts('There is no valid sendmail path setting. Click Administer >> System Setting >> Outbound Email to set the sendmail server.', [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', []); } elseif ($mailingInfo['outBound_option'] == CRM_Mailing_Config::OUTBOUND_OPTION_MOCK) { $mailer = self::_createMailer('mock', []); } 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.', [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.', [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.', [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.', [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); } // Previously, CiviCRM bundled patches to change the behavior of 3 specific drivers. Use wrapper/filters to avoid patching. $mailer = new CRM_Utils_Mail_FilteredPearMailer($driver, $params, $mailer); if (in_array($driver, ['smtp', 'mail', 'sendmail'])) { $mailer->addFilter('2000_log', ['CRM_Utils_Mail_Logger', 'filter']); $mailer->addFilter('2100_validate', function ($mailer, &$recipients, &$headers, &$body) { if (!is_array($headers)) { return PEAR::raiseError('$headers must be an array'); } }); } 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 * returnpath : email address for bounces to be sent to * messageId : Message ID for this email mesage * 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 = $params['from'] ?? NULL; 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 = $params['text'] ?? NULL; $htmlMessage = $params['html'] ?? NULL; $attachments = $params['attachments'] ?? NULL; // CRM-6224 if (trim(CRM_Utils_String::htmlToText($htmlMessage)) == '') { $htmlMessage = FALSE; } $headers = []; // 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 ); // On some servers mail() fails when 'Cc' or 'Bcc' headers are defined but empty. foreach (['Cc', 'Bcc'] as $optionalHeader) { $headers[$optionalHeader] = $params[strtolower($optionalHeader)] ?? NULL; if (empty($headers[$optionalHeader])) { unset($headers[$optionalHeader]); } } $headers['Subject'] = $params['subject'] ?? NULL; $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'] = $params['messageId'] ?? '<' . uniqid('civicrm_', TRUE) . "@$emailDomain>"; } if (!empty($params['autoSubmitted'])) { $headers['Auto-Submitted'] = "Auto-Generated"; } // make sure we has to have space, CRM-6977 foreach (['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'], TRUE, 'base64', 'attachment', (isset($attach['charset']) ? $attach['charset'] : '') ); } } $message = self::setMimeParams($msg); $headers = $msg->headers($headers); $to = [$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] // TODO: Refactor this quirk-handler as another filter in FilteredPearMailer. But that would merit review of impact on universe. $driver = ($mailer instanceof CRM_Utils_Mail_FilteredPearMailer) ? $mailer->getDriver() : NULL; $isPhpMail = (get_class($mailer) === "Mail_mail" || $driver === 'mail'); if (!$isPhpMail) { // get emails from headers, since these are // combination of name and email addresses. if (!empty($headers['Cc'])) { $to[] = $headers['Cc'] ?? NULL; } if (!empty($headers['Bcc'])) { $to[] = $headers['Bcc'] ?? NULL; 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.', [ 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 .= '