class MailFormatHelper (View source)

Defines a class containing utility methods for formatting mail messages.

Properties

static protected array $urls

Internal array of urls replaced with tokens.

static protected string $regexp

Quoted regex expression based on base path.

static protected array $supportedTags

Array of tags supported.

Methods

static string
wrapMail(string $text, string $indent = '')

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

static string
htmlToText(string $string, array $allowed_tags = NULL)

Transforms an HTML string into plain text, preserving its structure.

static 
wrapMailLine($line, $key, $values)

Wraps words on a single line.

static 
htmlToMailUrls($match = NULL, $reset = FALSE)

Keeps track of URLs and replaces them with placeholder tokens.

static 
htmlToTextClean($indent)

Replaces non-quotation markers from a piece of indentation with spaces.

static string
htmlToTextPad(string $text, string $pad, string $prefix = '')

Pads the last line with the given character.

Details

static string wrapMail(string $text, string $indent = '')

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.

static string htmlToText(string $string, array $allowed_tags = NULL)

Transforms an HTML string into plain text, preserving its structure.

The output will be suitable for use as 'format=flowed; delsp=yes' text (RFC 3676) and can be passed directly to MailManagerInterface::mail() for sending.

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

This function provides suitable alternatives for the following tags:


Parameters

string $string

The string to be transformed.

array $allowed_tags

(optional) If supplied, a list of tags that will be transformed. If omitted, all supported tags are transformed.

Return Value

string

The transformed string.

static protected wrapMailLine($line, $key, $values)

Wraps words on a single line.

Callback for array_walk() within \Drupal\Core\Mail\MailFormatHelper::wrapMail().

Note that we are skipping MIME content header lines, because attached files, especially applications, could have long MIME types or long filenames which result in line length longer than the 77 characters limit and wrapping that line will break the email format. For instance, the attached file hello_drupal.docx will produce the following Content-Type:

Parameters

$line
$key
$values

static protected htmlToMailUrls($match = NULL, $reset = FALSE)

Keeps track of URLs and replaces them with placeholder tokens.

Callback for preg_replace_callback() within \Drupal\Core\Mail\MailFormatHelper::htmlToText().

Parameters

$match
$reset

static protected htmlToTextClean($indent)

Replaces non-quotation markers from a piece of indentation with spaces.

Callback for array_map() within \Drupal\Core\Mail\MailFormatHelper::htmlToText().

Parameters

$indent

static protected string htmlToTextPad(string $text, string $pad, string $prefix = '')

Pads the last line with the given character.

Parameters

string $text

The text to pad.

string $pad

The character to pad the end of the string with.

string $prefix

(optional) Prefix to add to the string.

Return Value

string

The padded string.

See also

MailFormatHelper::htmlToText