You are here

public static function MailFormatHelper::wrapMail in Drupal 8

Same name and namespace in other branches
  1. 9 core/lib/Drupal/Core/Mail/MailFormatHelper.php \Drupal\Core\Mail\MailFormatHelper::wrapMail()

Performs format=flowed soft wrapping for mail (RFC 3676).

We use delsp=yes wrapping, but only break non-spaced languages when absolutely necessary to avoid compatibility issues.

We deliberately use LF rather than CRLF, see MailManagerInterface::mail().

Parameters

string $text: The plain text to process.

string $indent: (optional) A string to indent the text with. Only '>' characters are repeated on subsequent wrapped lines. Others are replaced by spaces.

Return value

string The content of the email as a string with formatting applied.

8 calls to MailFormatHelper::wrapMail()
HtmlToTextTest::testRemoveTrailingWhitespace in core/modules/system/tests/src/Functional/Mail/HtmlToTextTest.php
Tests that trailing whitespace is removed before newlines.
HtmlToTextTest::testUsenetSignature in core/modules/system/tests/src/Functional/Mail/HtmlToTextTest.php
Tests that trailing whitespace from Usenet style signatures is not removed.
MailFormatHelper::htmlToText in core/lib/Drupal/Core/Mail/MailFormatHelper.php
Transforms an HTML string into plain text, preserving its structure.
MailFormatHelperTest::testWrapMail in core/tests/Drupal/Tests/Core/Mail/MailFormatHelperTest.php
@covers ::wrapMail
MailTest::testConvertRelativeUrlsIntoAbsolute in core/modules/system/tests/src/Kernel/Mail/MailTest.php
Checks that relative paths in mails are converted into absolute URLs.

... See full list

File

core/lib/Drupal/Core/Mail/MailFormatHelper.php, line 52

Class

MailFormatHelper
Defines a class containing utility methods for formatting mail messages.

Namespace

Drupal\Core\Mail

Code

public static function wrapMail($text, $indent = '') {

  // Convert CRLF into LF.
  $text = str_replace("\r", '', $text);

  // See if soft-wrapping is allowed.
  $clean_indent = static::htmlToTextClean($indent);
  $soft = strpos($clean_indent, ' ') === FALSE;

  // Check if the string has line breaks.
  if (strpos($text, "\n") !== FALSE) {

    // Remove trailing spaces to make existing breaks hard, but leave
    // signature marker untouched (RFC 3676, Section 4.3).
    $text = preg_replace('/(?(?<!^--) +\\n|  +\\n)/m', "\n", $text);

    // Wrap each line at the needed width.
    $lines = explode("\n", $text);
    array_walk($lines, '\\Drupal\\Core\\Mail\\MailFormatHelper::wrapMailLine', [
      'soft' => $soft,
      'length' => strlen($indent),
    ]);
    $text = implode("\n", $lines);
  }
  else {

    // Wrap this line.
    static::wrapMailLine($text, 0, [
      'soft' => $soft,
      'length' => strlen($indent),
    ]);
  }

  // Empty lines with nothing but spaces.
  $text = preg_replace('/^ +\\n/m', "\n", $text);

  // Space-stuff special lines.
  $text = preg_replace('/^(>| |From)/m', ' $1', $text);

  // Apply indentation. We only include non-'>' indentation on the first line.
  $text = $indent . substr(preg_replace('/^/m', $clean_indent, $text), strlen($indent));
  return $text;
}