diff --git a/src/PHPMailer.php b/src/PHPMailer.php index 5ccf8ff..7672b84 100644 --- a/src/PHPMailer.php +++ b/src/PHPMailer.php @@ -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; diff --git a/test/PHPMailer/EncodeHeaderTest.php b/test/PHPMailer/EncodeHeaderTest.php new file mode 100644 index 0000000..dc8bcee --- /dev/null +++ b/test/PHPMailer/EncodeHeaderTest.php @@ -0,0 +1,72 @@ + + * @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', + ], + ]; + } +}