PHPMailer\PHPMailer

PHPMailer::encodeHeader()publicWP 1.0

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.

Method of the class: PHPMailer{}

No Hooks.

Return

String.

Usage

$PHPMailer = new PHPMailer();
$PHPMailer->encodeHeader( $str, $position );
$str(string) (required)
The header value to encode
$position(string)
What context the string will be used in
Default: 'text'

PHPMailer::encodeHeader() code WP 6.4.3

public function encodeHeader($str, $position = 'text')
{
    $matchcount = 0;
    switch (strtolower($position)) {
        case 'phrase':
            if (!preg_match('/[\200-\377]/', $str)) {
                //Can't use addslashes as we don't know the value of magic_quotes_sybase
                $encoded = addcslashes($str, "\0..\37\177\\\"");
                if (($str === $encoded) && !preg_match('/[^A-Za-z0-9!#$%&\'*+\/=?^_`{|}~ -]/', $str)) {
                    return $encoded;
                }

                return "\"$encoded\"";
            }
            $matchcount = preg_match_all('/[^\040\041\043-\133\135-\176]/', $str, $matches);
            break;
        /* @noinspection PhpMissingBreakStatementInspection */
        case 'comment':
            $matchcount = preg_match_all('/[()"]/', $str, $matches);
        //fallthrough
        case 'text':
        default:
            $matchcount += preg_match_all('/[\000-\010\013\014\016-\037\177-\377]/', $str, $matches);
            break;
    }

    if ($this->has8bitChars($str)) {
        $charset = $this->CharSet;
    } else {
        $charset = static::CHARSET_ASCII;
    }

    //Q/B encoding adds 8 chars and the charset ("` =?<charset>?[QB]?<content>?=`").
    $overhead = 8 + strlen($charset);

    if ('mail' === $this->Mailer) {
        $maxlen = static::MAIL_MAX_LINE_LENGTH - $overhead;
    } else {
        $maxlen = static::MAX_LINE_LENGTH - $overhead;
    }

    //Select the encoding that produces the shortest output and/or prevents corruption.
    if ($matchcount > strlen($str) / 3) {
        //More than 1/3 of the content needs encoding, use B-encode.
        $encoding = 'B';
    } elseif ($matchcount > 0) {
        //Less than 1/3 of the content needs encoding, use Q-encode.
        $encoding = 'Q';
    } elseif (strlen($str) > $maxlen) {
        //No encoding needed, but value exceeds max line length, use Q-encode to prevent corruption.
        $encoding = 'Q';
    } else {
        //No reformatting needed
        $encoding = false;
    }

    switch ($encoding) {
        case 'B':
            if ($this->hasMultiBytes($str)) {
                //Use a custom function which correctly encodes and wraps long
                //multibyte strings without breaking lines within a character
                $encoded = $this->base64EncodeWrapMB($str, "\n");
            } else {
                $encoded = base64_encode($str);
                $maxlen -= $maxlen % 4;
                $encoded = trim(chunk_split($encoded, $maxlen, "\n"));
            }
            $encoded = preg_replace('/^(.*)$/m', ' =?' . $charset . "?$encoding?\\1?=", $encoded);
            break;
        case 'Q':
            $encoded = $this->encodeQ($str, $position);
            $encoded = $this->wrapText($encoded, $maxlen, true);
            $encoded = str_replace('=' . static::$LE, "\n", trim($encoded));
            $encoded = preg_replace('/^(.*)$/m', ' =?' . $charset . "?$encoding?\\1?=", $encoded);
            break;
        default:
            return $str;
    }

    return trim(static::normalizeBreaks($encoded));
}