You are here

public function HTMLMailSystem::mail in HTML Mail 6.2

Same name and namespace in other branches
  1. 8.2 htmlmail.mail.inc \HTMLMailSystem::mail()
  2. 7.2 htmlmail.mail.inc \HTMLMailSystem::mail()

Send an email message.

Parameters

$message: An associative array containing at least:

  • headers: An associative array of (name => value) email headers.
  • body: The text/plain or text/html message body.
  • MailMIME: The message, parsed into a MailMIME object.

File

./htmlmail.mail.inc, line 202
Formats and sends mail using the MailMIME class.

Class

HTMLMailSystem
Implements MailSystemInterface.

Code

public function mail(array $message) {
  $eol = variable_get('mail_line_endings', MAIL_LINE_ENDINGS);

  // Ensure that subject is non-null.
  $message += array(
    'subject' => t('(No subject)'),
  );

  // Check for empty recipient.
  if (empty($message['to'])) {
    if (empty($message['headers']['To'])) {
      watchdog('HTMLMailSystem', 'Cannot send email about %subject without a recipient.', array(
        '%subject' => $message['subject'],
      ), WATCHDOG_ERROR);
      return FALSE;
    }
    $message['to'] = $message['headers']['To'];
  }
  if (class_exists('MailMIME')) {
    $mime = new MailMIME();
    $to = $mime
      ->encodeHeader('to', $message['to']);
    $subject = $mime
      ->encodeHeader('subject', $message['subject']);
    $txt_headers = $mime
      ->txtHeaders($message['headers']);
  }
  else {
    $to = mime_header_encode($message['to']);
    $subject = mime_header_encode($message['subject']);
    $txt_headers = $this
      ->txtHeaders($message['headers']);
  }
  $body = preg_replace('#(\\r\\n|\\r|\\n)#s', $eol, $message['body']);

  // Check for empty body.
  if (empty($body)) {
    watchdog('HTMLMailSystem', 'Refusing to send a blank email to %recipient about %subject.', array(
      '%recipient' => $message['to'],
      '%subject' => $message['subject'],
    ), WATCHDOG_WARNING);
    return FALSE;
  }
  if (variable_get('htmlmail_debug', 0)) {
    $params = array(
      $to,
      $subject,
      drupal_substr($body, 0, min(80, strpos("\n", $body))) . '...',
      $txt_headers,
    );
  }
  if (isset($message['headers']['Return-Path'])) {

    // A return-path was set.
    if (isset($_SERVER['WINDIR']) || strpos($_SERVER['SERVER_SOFTWARE'], 'Win32') !== FALSE) {

      // On Windows, PHP will use the value of sendmail_from for the
      // Return-Path header.
      $old_from = ini_get('sendmail_from');
      ini_set('sendmail_from', $message['headers']['Return-Path']);
      $result = @mail($to, $subject, $body, $txt_headers);
      ini_set('sendmail_from', $old_from);
    }
    elseif (ini_get('safe_mode')) {

      // If safe mode is in effect, passing the fifth parameter to @mail
      // will cause it to return FALSE and generate a PHP warning, even
      // if the parameter is NULL.
      $result = @mail($to, $subject, $body, $txt_headers);
    }
    else {

      // On most non-Windows systems, the "-f" option to the sendmail command
      // is used to set the Return-Path.
      $extra = '-f' . $message['headers']['Return-Path'];
      $result = @mail($to, $subject, $body, $txt_headers, $extra);
      if (variable_get('htmlmail_debug', 0)) {
        $params[] = $extra;
      }
    }
  }
  else {

    // No return-path was set.
    $result = @mail($to, $subject, $body, $txt_headers);
  }
  if (!$result && variable_get('htmlmail_debug', 0)) {
    $call = '@mail(' . implode(', ', $params) . ')';
    foreach ($params as $i => $value) {
      $params[$i] = var_export($value, 1);
    }
    if (defined('DEBUG_BACKTRACE_IGNORE_ARGS')) {
      $trace = print_r(debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS), 1);
    }
    else {
      $trace = debug_backtrace(0);
      for ($i = count($trace) - 1; $i >= 0; $i--) {
        unset($trace[$i]['args']);
      }
      $trace = print_r($trace);
    }
    watchdog('htmlmail', 'Mail sending failed because:<br /><pre>@call</pre><br />returned FALSE.<br /><pre>@trace</pre>', array(
      '@call' => $call,
      '@trace' => $trace,
    ));
  }
  return $result;
}