You are here

public function SwiftMailer::massageMessageBody in Swift Mailer 8

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

Massages the message body into the format expected for rendering.

Parameters

array $message: The message.

Return value

array

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 538

Class

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

Namespace

Drupal\swiftmailer\Plugin\Mail

Code

public function massageMessageBody(array $message) {

  // Get default mail line endings and merge all lines in the e-mail body
  // separated by the mail line endings. Keep Markup objects and escape others
  // and then treat the result as safe markup.
  $line_endings = Settings::get('mail_line_endings', PHP_EOL);
  $applicable_format = $this
    ->getApplicableFormat($message);
  $filter_format = $this->config['message']['filter_format'];
  $message['body'] = Markup::create(implode($line_endings, array_map(function ($body) use ($applicable_format, $filter_format) {

    // If the body contains no html tags but the applicable format is HTML,
    // we can assume newlines will need be converted to <br>.
    if ($applicable_format == SWIFTMAILER_FORMAT_HTML && mb_strlen(strip_tags($body)) === mb_strlen($body)) {

      // The default fallback format is 'plain_text', which escapes markup,
      // converts new lines to <br> and converts URLs to links.
      $build = [
        '#type' => 'processed_text',
        '#text' => $body,
        '#format' => $filter_format,
      ];
      $body = $this->renderer
        ->renderPlain($build);
    }

    // If $item is not marked safe then it will be escaped.
    return $body instanceof MarkupInterface ? $body : MailFormatHelper::htmlToText($body);
  }, $message['body'])));
  return $message;
}