function MailFormatHelper::wrapMailLine

Same name in other branches
  1. 8.9.x core/lib/Drupal/Core/Mail/MailFormatHelper.php \Drupal\Core\Mail\MailFormatHelper::wrapMailLine()
  2. 10 core/lib/Drupal/Core/Mail/MailFormatHelper.php \Drupal\Core\Mail\MailFormatHelper::wrapMailLine()
  3. 11.x core/lib/Drupal/Core/Mail/MailFormatHelper.php \Drupal\Core\Mail\MailFormatHelper::wrapMailLine()

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:


Content-Type:
application/vnd.openxmlformats-officedocument.wordprocessingml.document;
name="hello_drupal.docx"
1 call to MailFormatHelper::wrapMailLine()
MailFormatHelper::wrapMail in core/lib/Drupal/Core/Mail/MailFormatHelper.php
Performs format=flowed soft wrapping for mail (RFC 3676).

File

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

Class

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

Namespace

Drupal\Core\Mail

Code

protected static function wrapMailLine(&$line, $key, $values) {
    $line_is_mime_header = FALSE;
    $mime_headers = [
        'Content-Type',
        'Content-Transfer-Encoding',
        'Content-Disposition',
        'Content-Description',
    ];
    // Do not break MIME headers which could be longer than 77 characters.
    foreach ($mime_headers as $header) {
        if (strpos($line, $header . ': ') === 0) {
            $line_is_mime_header = TRUE;
            break;
        }
    }
    if (!$line_is_mime_header) {
        // Use soft-breaks only for purely quoted or unindented text.
        $line = wordwrap($line, 77 - $values['length'], $values['soft'] ? " \n" : "\n");
    }
    // Break really long words at the maximum width allowed.
    $line = wordwrap($line, 996 - $values['length'], $values['soft'] ? " \n" : "\n", TRUE);
}

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