You are here

protected function SwiftMailer::massageMessageBody in Swift Mailer 8.2

Same name and namespace in other branches
  1. 8 src/Plugin/Mail/SwiftMailer.php \Drupal\swiftmailer\Plugin\Mail\SwiftMailer::massageMessageBody()

Massages the message body into the format expected for rendering.

@internal

Parameters

array $message: The message.

boolean $is_html: True if generating HTML output, false for plain text.

1 call to SwiftMailer::massageMessageBody()
SwiftMailer::format in src/Plugin/Mail/SwiftMailer.php
Formats a message composed by drupal_mail().

File

src/Plugin/Mail/SwiftMailer.php, line 531

Class

SwiftMailer
Provides a 'Swift Mailer' plugin to send emails.

Namespace

Drupal\swiftmailer\Plugin\Mail

Code

protected function massageMessageBody(array &$message, $is_html) {
  $text_format = $message['params']['text_format'] ?? $this->config['message']['text_format'] ?: NULL;
  $line_endings = Settings::get('mail_line_endings', PHP_EOL);
  $body = [];
  foreach ($message['body'] as $part) {
    if (!$part instanceof MarkupInterface) {
      if ($is_html) {

        // Convert to HTML. The default 'plain_text' format escapes markup,
        // converts new lines to <br> and converts URLs to links.
        $body[] = check_markup($part, $text_format);
      }
      else {

        // The body will be plain text. However we need to convert to HTML
        // to render the template then convert back again. Use a fixed
        // conversion because we don't want to convert URLs to links.
        $body[] = preg_replace("|\n|", "<br />\n", HTML::escape($part)) . "<br />\n";
      }
    }
    else {
      $body[] = $part . $line_endings;
    }
  }

  // Merge all lines in the e-mail body and treat the result as safe markup.
  $message['body'] = Markup::create(implode('', $body));

  // Attempt to use the mail theme defined in MailSystem.
  if ($this->mailManager instanceof MailsystemManager) {
    $mail_theme = $this->mailManager
      ->getMailTheme();
  }
  else {
    $mail_theme = $this->themeManager
      ->getActiveTheme()
      ->getName();
  }
  $render = [
    '#theme' => $message['params']['theme'] ?? 'swiftmailer',
    '#message' => $message,
    '#is_html' => $is_html,
  ];
  if ($is_html) {
    $render['#attached']['library'] = [
      "{$mail_theme}/swiftmailer",
    ];
  }
  $message['body'] = $this->renderer
    ->renderPlain($render);
  if ($is_html) {

    // Process CSS from libraries.
    $assets = AttachedAssets::createFromRenderArray($render);
    $css = '';

    // Request optimization so that the CssOptimizer performs essential
    // processing such as @include.
    foreach ($this->assetResolver
      ->getCssAssets($assets, TRUE) as $css_asset) {
      $css .= file_get_contents($css_asset['data']);
    }
    if ($css) {
      $message['body'] = (new CssToInlineStyles())
        ->convert($message['body'], $css);
    }
  }
  else {

    // Convert to plain text.
    $message['body'] = (new Html2Text($message['body']))
      ->getText();
  }
}