You are here

public function HtmlMailSystem::formatMailMime in HTML Mail 8.3

Same name and namespace in other branches
  1. 8 src/Plugin/Mail/HTMLMailSystem.php \Drupal\htmlmail\Plugin\Mail\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 src/Plugin/Mail/HtmlMailSystem.php
Format emails according to module settings.

File

src/Plugin/Mail/HtmlMailSystem.php, line 486

Class

HtmlMailSystem
Modify the Drupal mail system to use HTML Mail 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;
}