public function PHPMailer::encodeHeader
Search API
5.x PHPMailer.php | public PHPMailer::encodeHeader($str, $position = 'text') |
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.
Parameters
string $str The header value to encode:
string $position What context the string will be used in:
Return value
string
6 calls to PHPMailer::encodeHeader()
- PHPMailer::addrFormat in inc/
PHPMailer/ src/ PHPMailer.php - Format an address for use in a message header.
- PHPMailer::attachAll in inc/
PHPMailer/ src/ PHPMailer.php - Attach all file, string, and binary attachments to the message. Returns an empty string on failure.
- PHPMailer::createHeader in inc/
PHPMailer/ src/ PHPMailer.php - Assemble message headers.
- PHPMailer::DKIM_Add in inc/
PHPMailer/ src/ PHPMailer.php - Create the DKIM header and body in a new message header.
- PHPMailer::mailPassthru in inc/
PHPMailer/ src/ PHPMailer.php - Call mail() in a safe_mode-aware fashion. Also, unless sendmail_path points to sendmail (or something that claims to be sendmail), don't pass params (not a perfect fix, but it will do).
File
- inc/
PHPMailer/ src/ PHPMailer.php, line 3105
Class
- PHPMailer
- PHPMailer - PHP email creation and transport class.
Namespace
PHPMailer\PHPMailerCode
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) and !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;
}
//RFCs specify a maximum line length of 78 chars, however mail() will sometimes
//corrupt messages with headers longer than 65 chars. See #818
$lengthsub = 'mail' == $this->Mailer ? 13 : 0;
$maxlen = static::STD_LINE_LENGTH - $lengthsub;
// Try to select the encoding which should produce the shortest output
if ($matchcount > strlen($str) / 3) {
// More than a third of the content will need encoding, so B encoding will be most efficient
$encoding = 'B';
//This calculation is:
// max line length
// - shorten to avoid mail() corruption
// - Q/B encoding char overhead ("` =?<charset>?[QB]?<content>?=`")
// - charset name length
$maxlen = static::STD_LINE_LENGTH - $lengthsub - 8 - strlen($this->CharSet);
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', ' =?' . $this->CharSet . "?$encoding?\\1?=", $encoded);
}
elseif ($matchcount > 0) {
//1 or more chars need encoding, use Q-encode
$encoding = 'Q';
//Recalc max line length for Q encoding - see comments on B encode
$maxlen = static::STD_LINE_LENGTH - $lengthsub - 8 - strlen($this->CharSet);
$encoded = $this->encodeQ($str, $position);
$encoded = $this->wrapText($encoded, $maxlen, true);
$encoded = str_replace('=' . static::$LE, "\n", trim($encoded));
$encoded = preg_replace('/^(.*)$/m', ' =?' . $this->CharSet . "?$encoding?\\1?=", $encoded);
}
elseif (strlen($str) > $maxlen) {
//No chars need encoding, but line is too long, so fold it
$encoded = trim($this->wrapText($str, $maxlen, false));
if ($str == $encoded) {
//Wrapping nicely didn't work, wrap hard instead
$encoded = trim(chunk_split($str, static::STD_LINE_LENGTH, static::$LE));
}
$encoded = str_replace(static::$LE, "\n", trim($encoded));
$encoded = preg_replace('/^(.*)$/m', ' \\1', $encoded);
}
else {
//No reformatting needed
return $str;
}
return trim(static::normalizeBreaks($encoded));
}