PHPMailer\PHPMailer

PHPMailer::wrapText()publicWP 1.0

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

Method of the class: PHPMailer{}

No Hooks.

Return

String.

Usage

$PHPMailer = new PHPMailer();
$PHPMailer->wrapText( $message, $length, $qp_mode );
$message(string) (required)
The message to wrap
$length(int) (required)
The line length to wrap to
$qp_mode(true|false)
Whether to run in Quoted-Printable mode
Default: false

PHPMailer::wrapText() code WP 6.6.2

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 && (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 ($word !== '') {
                    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 = (string) substr($word, $len);

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

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

    return $message;
}