function SymfonyMailer::format

Same name and namespace in other branches
  1. 11.x core/lib/Drupal/Core/Mail/Plugin/Mail/SymfonyMailer.php \Drupal\Core\Mail\Plugin\Mail\SymfonyMailer::format()

Formats a message prior to sending.

Allows to preprocess, format, and postprocess a mail message before it is passed to the sending system. The message body is received as an array of lines that are either strings or objects implementing \Drupal\Component\Render\MarkupInterface. It must be converted to the format expected by mail() which is a single string that can be either plain text or HTML. In the HTML case an alternate plain-text version can be returned in $message['plain'].

The conversion process consists of the following steps:

Parameters

array $message: A message array, as described in hook_mail_alter().

Return value

array The formatted $message.

Overrides MailInterface::format

File

core/lib/Drupal/Core/Mail/Plugin/Mail/SymfonyMailer.php, line 100

Class

SymfonyMailer
Defines an experimental mail backend, based on the Symfony mailer component.

Namespace

Drupal\Core\Mail\Plugin\Mail

Code

public function format(array $message) {
  foreach ($message['body'] as &$part) {
    // If the message contains HTML, convert it to plain text (which also
    // wraps the mail body).
    if ($part instanceof MarkupInterface) {
      $part = MailFormatHelper::htmlToText($part);
    }
    else {
      $part = MailFormatHelper::wrapMail($part);
    }
  }
  // Join the body array into one string.
  $message['body'] = implode("\n\n", $message['body']);
  return $message;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.