public function PHPMailer::wrapText

5.x PHPMailer.php public PHPMailer::wrapText($message, $length, $qp_mode = false)

Word-wrap message. For use with mailers that do not automatically perform wrapping and for quoted-printable encoded messages. Original written by philippe.

Parameters

string $message The message to wrap:

int $length The line length to wrap to:

bool $qp_mode Whether to run in Quoted-Printable mode:

Return value

string

2 calls to PHPMailer::wrapText()
PHPMailer::encodeHeader in inc/PHPMailer/src/PHPMailer.php
Encode a header value (not including its label) optimally. Picks shortest of Q, B, or none. Result includes folding if needed. See RFC822 definitions for phrase, comment and text positions.
PHPMailer::setWordWrap in inc/PHPMailer/src/PHPMailer.php
Apply word wrapping to the message body. Wraps the message body to the number of chars set in the WordWrap property. You should only do this to plain-text bodies as wrapping HTML tags may break them. This is called automatically by createBody(), so…

File

inc/PHPMailer/src/PHPMailer.php, line 2156

Class

PHPMailer
PHPMailer - PHP email creation and transport class.

Namespace

PHPMailer\PHPMailer

Code

public function wrapText($message, $length, $qp_mode = false) 
 {
  if ($qp_mode) {
    $soft_break = sprintf(' =%s', static::$LE);
  }
  else {
    $soft_break = static::$LE;
  }
  // If utf-8 encoding is used, we will need to make sure we don't
  // split multibyte characters when we wrap
  $is_utf8 = static::CHARSET_UTF8 === strtolower($this->CharSet);
  $lelen = strlen(static::$LE);
  $crlflen = strlen(static::$LE);

  $message = static::normalizeBreaks($message);
  //Remove a trailing line break
  if (substr($message, -$lelen) == static::$LE) {
    $message = substr($message, 0, -$lelen);
  }

  //Split message into lines
  $lines = explode(static::$LE, $message);
  //Message will be rebuilt in here
  $message = '';
  foreach ($lines as $line) {
    $words = explode(' ', $line);
    $buf = '';
    $firstword = true;
    foreach ($words as $word) {
      if ($qp_mode and (strlen($word) > $length)) {
        $space_left = $length - strlen($buf) - $crlflen;
        if (!$firstword) {
          if ($space_left > 20) {
            $len = $space_left;
            if ($is_utf8) {
              $len = $this->utf8CharBoundary($word, $len);
            }
            elseif ('=' == substr($word, $len - 1, 1)) {
              --$len;
            }
            elseif ('=' == substr($word, $len - 2, 1)) {
              $len -= 2;
            }
            $part = substr($word, 0, $len);
            $word = substr($word, $len);
            $buf .= ' ' . $part;
            $message .= $buf . sprintf('=%s', static::$LE);
          }
          else {
            $message .= $buf . $soft_break;
          }
          $buf = '';
        }
        while (strlen($word) > 0) {
          if ($length <= 0) {
            break;
          }
          $len = $length;
          if ($is_utf8) {
            $len = $this->utf8CharBoundary($word, $len);
          }
          elseif ('=' == substr($word, $len - 1, 1)) {
            --$len;
          }
          elseif ('=' == substr($word, $len - 2, 1)) {
            $len -= 2;
          }
          $part = substr($word, 0, $len);
          $word = substr($word, $len);

          if (strlen($word) > 0) {
            $message .= $part . sprintf('=%s', static::$LE);
          }
          else {
            $buf = $part;
          }
        }
      }
      else {
        $buf_o = $buf;
        if (!$firstword) {
          $buf .= ' ';
        }
        $buf .= $word;

        if (strlen($buf) > $length and '' != $buf_o) {
          $message .= $buf_o . $soft_break;
          $buf = $word;
        }
      }
      $firstword = false;
    }
    $message .= $buf . static::$LE;
  }

  return $message;
}