You are here

public function HTMLMailSystem::formatMailMime in HTML Mail 8

Same name and namespace in other branches
  1. 8.3 src/Plugin/Mail/HtmlMailSystem.php \Drupal\htmlmail\Plugin\Mail\HtmlMailSystem::formatMailMime()

Use the MailMime class to format the message body.

See also

http://drupal.org/project/mailmime

1 call to HTMLMailSystem::formatMailMime()
HTMLMailSystem::format in src/Plugin/Mail/HTMLMailSystem.php
Format emails according to module settings.

File

src/Plugin/Mail/HTMLMailSystem.php, line 422

Class

HTMLMailSystem
Modify the Drupal mail system to use HTMLMail when sending emails.

Namespace

Drupal\htmlmail\Plugin\Mail

Code

public function formatMailMime(array &$message) {
  $eol = $this->siteSettings
    ->get('mail_line_endings', PHP_EOL);
  $message['body'] = HTMLMailMime::concat($message['body'], $eol);

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

  // Parse it into MIME parts.
  if (!($mime = HTMLMailMime::parse($email, $this->logger, $this->mimeType, $this->fileSystem))) {
    $this
      ->getLogger()
      ->error('Could not parse email message.');
    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.html.twig.
  $theme = [
    '#theme' => HtmlMailHelper::getThemeNames($message),
    '#message' => $email,
  ];
  $body = $this->renderer
    ->render($theme);
  $mime
    ->setHtmlBody($body);
  $mime
    ->setTxtBody(MailFormatHelper::htmlToText($body));
  $message['MailMIME'] =& $mime;
  return $body;
}