You are here

public function HTMLMailSystem::format in HTML Mail 7.2

Same name and namespace in other branches
  1. 8.2 htmlmail.mail.inc \HTMLMailSystem::format()
  2. 6.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

array $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

array The formatted $message, ready for sending.

Overrides MailSystemInterface::format

File

./htmlmail.mail.inc, line 32
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.
    module_load_include('inc', 'mailsystem', 'html_to_text');
    $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, 'https://www.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')) {
      $filtered_body = check_markup($body, $filter);
      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);
      if (isset($message['params']['attachments'])) {
        foreach ($message['params']['attachments'] as $attachment) {
          $mime
            ->addAttachment(drupal_realpath($attachment['uri']), $attachment['filemime'], $attachment['filename'], TRUE, 'base64', 'attachment', '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 error to the %mailmime issue queue.', array(
          '%toemail' => 'MailMIME::toEmail()',
          '%mailmime' => 'Mail MIME',
        ), WATCHDOG_WARNING, 'https://www.drupal.org/node/add/project-issue/mailmime');
      }
    }
    else {
      $message['headers']['Content-Type'] = 'text/html; charset=utf-8';
      $message['body'] = $body;
      if (variable_get('htmlmail_html_with_plain', FALSE)) {
        $boundary = uniqid('np');
        $message['headers']['Content-Type'] = 'multipart/alternative;boundary="' . $boundary . '"';
        $html = $message['body'];
        $raw_message = 'This is a MIME encoded message.';
        $raw_message .= $eol . $eol . "--" . $boundary . $eol;
        $raw_message .= "Content-Type: text/plain;charset=utf-8" . $eol . $eol;
        $raw_message .= mailsystem_html_to_text($html);
        $raw_message .= $eol . $eol . "--" . $boundary . $eol;
        $raw_message .= "Content-Type: text/html;charset=utf-8" . $eol . $eol;
        $raw_message .= $html;
        $raw_message .= $eol . $eol . "--" . $boundary . "--";
        $message['body'] = $raw_message;
      }
    }
  }
  else {
    if (isset($message['MailMIME'])) {
      $mime =& $message['MailMIME'];
      $mime
        ->setHTMLBody('');
      $mime
        ->setContentType('text/plain', array(
        'charset' => 'utf-8',
      ));
      if (isset($message['params']['attachments'])) {
        foreach ($message['params']['attachments'] as $attachment) {
          $mime
            ->addAttachment(drupal_realpath($attachment['uri']), $attachment['filemime'], $attachment['filename'], TRUE, 'base64', 'attachment', '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 error to the %mailmime issue queue.', array(
          '%toemail' => 'MailMIME::toEmail()',
          '%mailmime' => 'Mail MIME',
        ), WATCHDOG_WARNING, 'https://www.drupal.org/node/add/project-issue/mailmime');
      }
    }
    else {
      $message['body'] = $plain;
      $message['headers']['Content-Type'] = 'text/plain; charset=utf-8';
    }
  }
  return $message;
}