Keep the charset label for 7-bit ISO-2022 header values

encodeHeader() fell back to us-ascii for any header value without 8-bit
chars, which mislabelled ISO-2022-JP text. Values containing ESC escape
sequences now keep the message charset.

Fixes #2185
This commit is contained in:
dualfroz committed 2026-10-14 18:51:49 +02:00
1 parent 4ce8260f80
commit 5f8d078d57
2 files changed
+74 -1

No files matched your search

+2 -1
View File
@@ -3768,7 +3768,8 @@ class PHPMailer
break;
}
if ($this->has8bitChars($str)) {
//ISO-2022 charsets such as ISO-2022-JP are 7-bit, but text using their escape sequences isn't us-ascii
if ($this->has8bitChars($str) || strpos($str, "\x1B") !== false) {
$charset = $this->CharSet;
} else {
$charset = static::CHARSET_ASCII;
+72
View File
@@ -0,0 +1,72 @@
<?php
/**
* PHPMailer - PHP email transport unit tests.
* PHP version 5.5.
*
* @author Marcus Bointon <phpmailer@synchromedia.co.uk>
* @author Andy Prevost
* @copyright 2012 - 2020 Marcus Bointon
* @copyright 2004 - 2009 Andy Prevost
* @license https://www.gnu.org/licenses/old-licenses/lgpl-2.1.html GNU Lesser General Public License
*/
namespace PHPMailer\Test\PHPMailer;
use PHPMailer\Test\TestCase;
/**
* Test the charset label chosen when encoding header values.
*
* @covers \PHPMailer\PHPMailer\PHPMailer::encodeHeader
*/
final class EncodeHeaderTest extends TestCase
{
/**
* Test the charset label used for 7-bit header values.
*
* @dataProvider dataEncodeHeaderCharset
*
* @param string $input The header value to encode.
* @param string $expected The expected function return value.
* @param string $charset The charset to use.
*/
public function testEncodeHeaderCharset($input, $expected, $charset)
{
$this->Mail->CharSet = $charset;
self::assertSame($expected, $this->Mail->encodeHeader($input));
}
/**
* Data provider.
*
* @return array
*/
public static function dataEncodeHeaderCharset()
{
return [
'ISO-2022-JP text keeps its charset label even though it is 7-bit' => [
//"Test" in Japanese katakana, converted to ISO-2022-JP
'input' => "\x1B\$B%F%9%H\x1B(B",
'expected' => '=?ISO-2022-JP?Q?=1B$B%F%9%H=1B(B?=',
'charset' => 'ISO-2022-JP',
],
'ISO-2022-JP text in the middle of ASCII text keeps its charset label' => [
'input' => "Re: \x1B\$B%F%9%H\x1B(B",
'expected' => '=?ISO-2022-JP?Q?Re:_=1B$B%F%9%H=1B(B?=',
'charset' => 'ISO-2022-JP',
],
'Plain ASCII text with an ISO-2022-JP charset is not encoded' => [
'input' => 'Test',
'expected' => 'Test',
'charset' => 'ISO-2022-JP',
],
'ASCII text with a control character still falls back to us-ascii' => [
'input' => "Joe\x7FBloggs",
'expected' => '=?us-ascii?Q?Joe=7FBloggs?=',
'charset' => 'utf-8',
],
];
}
}