You are here

public function HTMLMailSystem::formatMailMIME in HTML Mail 7.2

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

Use the MailMime class to format the message body.

See also

https://www.drupal.org/project/mailmime

1 call to HTMLMailSystem::formatMailMIME()
HTMLMailSystem::format in ./htmlmail.mail.inc
Format emails according to module settings.

File

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

Class

HTMLMailSystem
Implements MailSystemInterface.

Code

public function formatMailMIME(array &$message) {
  $eol = variable_get('mail_line_endings', MAIL_LINE_ENDINGS);
  $message['body'] = MailMIME::concat($message['body']);

  // Build a full email message string.
  $email = MailMIME::encodeEmail($message['headers'], $message['body']);

  // Parse it into MIME parts.
  if (!($mime = MailMIME::parse($email))) {
    watchdog('HTMLMailSystem', 'Could not parse email message.', array(), WATCHDOG_ERROR);
    return $message;
  }

  // Work on a copy so that the original $message['body'] remains unchanged.
  $email = $message;
  if (!($email['body'] = $mime
    ->getHTMLBody()) && !($email['body'] = $mime
    ->getTXTBody())) {
    $email['body'] = '';
  }
  else {

    // Wrap formatted plaintext in <pre> tags.
    if ($email['body'] === strip_tags($email['body']) && preg_match('/.' . $eol . './', $email['body'])) {

      // In condition:
      // No html tags.
      // At least one embedded newline.
      $email['body'] = '<pre>' . $email['body'] . '</pre>';
    }
  }

  // Theme with htmlmail.tpl.php.
  $body = theme('htmlmail', $email);
  $mime
    ->setHTMLBody($body);

  // @todo Change to drupal_html_to_text when issue #299138 gets resolved.
  module_load_include('inc', 'mailsystem', 'html_to_text');
  $mime
    ->setTXTBody(mailsystem_html_to_text($body));
  $message['MailMIME'] =& $mime;
  return $body;
}