You are here

public function HTMLMailSystem::format in HTML Mail 6.2

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

Format emails according to module settings.

Parses the message headers and body into a MailMIME object. If another module subsequently modifies the body, then format() should be called again before sending. This is safe because the $message['body'] is not modified.

Parameters

$message: An associative array with at least the following parts:

  • headers: An array of (name => value) email headers.
  • body: The text/plain or text/html message part.

Return value

The formatted $message, ready for sending.

File

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

Class

HTMLMailSystem
Implements MailSystemInterface.

Code

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

  // @todo Remove this when issue #209672 gets resolved.
  $default_from = variable_get('site_mail', ini_get('sendmail_from'));
  if (!empty($message['headers']['From']) && $message['headers']['From'] == $default_from && valid_email_address($default_from)) {
    $message['headers']['From'] = '"' . str_replace('"', '', variable_get('site_name', 'Drupal')) . '" <' . $default_from . '>';
  }

  // Collapse the message body array.
  if (module_exists('mailmime')) {
    $body = $this
      ->formatMailMIME($message);
    $plain = $message['MailMIME']
      ->getTXTBody();
  }
  else {
    if (is_array($message['body'])) {
      $message['body'] = implode("{$eol}{$eol}", $message['body']);
    }
    $body = theme('htmlmail', $message);
    if ($message['body'] && !$body) {
      watchdog('htmlmail', 'The %theme function did not return any text.  Please check your template file for errors.', array(
        '%theme' => "theme('htmlmail', \$message)",
      ), WATCHDOG_WARNING);
      $body = $message['body'];
    }

    // @todo Change to drupal_html_to_text when issue #299138 gets resolved.
    $plain = mailsystem_html_to_text($body);
    if ($body && !$plain) {
      watchdog('htmlmail', 'The %convert function did not return any text. Please report this error to the %mailsystem issue queue.', array(
        '%convert' => 'mailsystem_html_to_text()',
        '%mailsystem' => 'Mail system',
      ), WATCHDOG_WARNING, 'http://drupal.org/node/add/project-issue/mailsystem');
    }
  }

  // Check to see whether recipient allows non-plaintext.
  if ($body && htmlmail_is_allowed($message['to'])) {

    // Optionally apply the selected web theme.
    if (module_exists('echo') && ($theme = htmlmail_get_selected_theme($message))) {
      $themed_body = echo_themed_page($message['subject'], $body, $theme);
      if ($themed_body) {
        $body = $themed_body;
      }
      else {
        watchdog('htmlmail', 'The %echo function did not return any text. Please check the page template of your %theme theme for errors.', array(
          '%echo' => 'echo_themed_page()',
          '%theme' => $theme,
        ), WATCHDOG_WARNING);
      }
    }

    // Optionally apply the selected output filter.
    if ($filter = variable_get('htmlmail_postfilter', FILTER_FORMAT_DEFAULT)) {
      $filtered_body = check_markup($body, $filter, FALSE);
      if ($filtered_body) {
        $body = $filtered_body;
      }
      else {
        watchdog('htmlmail', 'The %check function did not return any text. Please check your %filter output filter for errors.', array(
          '%check' => 'check_markup()',
          '%filter' => $filter,
        ), WATCHDOG_WARNING);
      }
    }

    // Store the fully-themed HTML body.
    if (isset($message['MailMIME'])) {
      $mime =& $message['MailMIME'];
      $mime
        ->setHTMLBody($body);
      list($message['headers'], $message['body']) = $mime
        ->toEmail($message['headers']);
      if (!$message['body']) {
        watchdog('htmlmail', 'The %toemail function did not return any text. Please report this errot to the %mailmime issue queue.', array(
          '%toemail' => 'MailMIME::toEmail()',
          '%mailmime' => 'Mail MIME',
        ), WATCHDOG_WARNING, 'http://drupal.org/node/add/project-issue/mailmime');
      }
    }
    else {
      $message['headers']['Content-Type'] = 'text/html; charset=utf-8';
      $message['body'] = $body;
    }
  }
  else {
    if (isset($message['MailMIME'])) {
      $mime =& $message['MailMIME'];
      $mime
        ->setHTMLBody('');
      $mime
        ->setContentType('text/plain', array(
        'charset' => 'utf-8',
      ));
      list($message['headers'], $message['body']) = $mime
        ->toEmail($message['headers']);
      if (!$message['body']) {
        watchdog('htmlmail', 'The %toemail function did not return any text. Please report this errot to the %mailmime issue queue.', array(
          '%toemail' => 'MailMIME::toEmail()',
          '%mailmime' => 'Mail MIME',
        ), WATCHDOG_WARNING, 'http://drupal.org/node/add/project-issue/mailmime');
      }
    }
    else {
      $message['body'] = $plain;
      $message['headers']['Content-Type'] = 'text/plain; charset=utf-8';
    }
  }
  return $message;
}