Skip to content

Commit 0f3a919

Browse files
committed
MimePart: fixed not escaped header before encoded into utf-8
1 parent d998397 commit 0f3a919

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

src/Mail/MimePart.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -279,12 +279,21 @@ public function getEncodedMessage(): string
279279
*/
280280
private static function encodeSequence(string $s, int &$offset = 0, ?int $type = null): string
281281
{
282+
$escape = static function (string $s): string {
283+
// RFC 2822 atext except =
284+
if (preg_match('#[^ a-zA-Z0-9!\#$%&\'*+/?^_`{|}~-]#', $s) === 1) {
285+
return sprintf('"%s"', addcslashes($s, '"\\'));
286+
}
287+
288+
return $s;
289+
};
290+
282291
if (
283292
(strlen($s) < self::LineLength - 3) && // 3 is tab + quotes
284293
strspn($s, "!\"#$%&\\'()*+,-./0123456789:;<>@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^`abcdefghijklmnopqrstuvwxyz{|}~=? _\r\n\t") === strlen($s)
285294
) {
286-
if ($type && preg_match('#[^ a-zA-Z0-9!\#$%&\'*+/?^_`{|}~-]#', $s)) { // RFC 2822 atext except =
287-
return self::append('"' . addcslashes($s, '"\\') . '"', $offset);
295+
if ($type !== null) {
296+
$s = $escape($s);
288297
}
289298

290299
return self::append($s, $offset);
@@ -296,6 +305,10 @@ private static function encodeSequence(string $s, int &$offset = 0, ?int $type =
296305
$offset = 1;
297306
}
298307

308+
if ($type !== null) {
309+
$s = $escape($s);
310+
}
311+
299312
$s = iconv_mime_encode(str_repeat(' ', $old = $offset), $s, [
300313
'scheme' => 'B', // Q is broken
301314
'input-charset' => 'UTF-8',

tests/Mail/Mail.headers.002.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ require __DIR__ . '/Mail.php';
1717

1818
$mail = new Message;
1919

20-
$mail->setFrom('John Doe <[email protected]>');
20+
$mail->setFrom('Kdo uteče, obědvá <[email protected]>');
2121

2222
$mail->addTo('Lady Jane <[email protected]>');
2323
$mail->addCc('[email protected]');
@@ -37,7 +37,7 @@ Assert::match(<<<'EOD'
3737
MIME-Version: 1.0
3838
X-Mailer: Nette Framework
3939
Date: %a%
40-
From: John Doe <[email protected]>
40+
From: =?UTF-8?B?IktkbyB1dGXEjWUsIG9ixJtkdsOhIg==?= <[email protected]>
4141
To: Lady Jane <[email protected]>
4242
4343

0 commit comments

Comments
 (0)