Skip to content

Commit

Permalink
MimePart: fixed not escaped header before encoded into utf-8
Browse files Browse the repository at this point in the history
  • Loading branch information
Daaarkling committed Oct 10, 2024
1 parent d998397 commit 0f3a919
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
17 changes: 15 additions & 2 deletions src/Mail/MimePart.php
Original file line number Diff line number Diff line change
Expand Up @@ -279,12 +279,21 @@ public function getEncodedMessage(): string
*/
private static function encodeSequence(string $s, int &$offset = 0, ?int $type = null): string
{
$escape = static function (string $s): string {
// RFC 2822 atext except =
if (preg_match('#[^ a-zA-Z0-9!\#$%&\'*+/?^_`{|}~-]#', $s) === 1) {
return sprintf('"%s"', addcslashes($s, '"\\'));
}

return $s;
};

if (
(strlen($s) < self::LineLength - 3) && // 3 is tab + quotes
strspn($s, "!\"#$%&\\'()*+,-./0123456789:;<>@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^`abcdefghijklmnopqrstuvwxyz{|}~=? _\r\n\t") === strlen($s)
) {
if ($type && preg_match('#[^ a-zA-Z0-9!\#$%&\'*+/?^_`{|}~-]#', $s)) { // RFC 2822 atext except =
return self::append('"' . addcslashes($s, '"\\') . '"', $offset);
if ($type !== null) {
$s = $escape($s);
}

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

if ($type !== null) {
$s = $escape($s);
}

$s = iconv_mime_encode(str_repeat(' ', $old = $offset), $s, [
'scheme' => 'B', // Q is broken
'input-charset' => 'UTF-8',
Expand Down
4 changes: 2 additions & 2 deletions tests/Mail/Mail.headers.002.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ require __DIR__ . '/Mail.php';

$mail = new Message;

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

$mail->addTo('Lady Jane <[email protected]>');
$mail->addCc('[email protected]');
Expand All @@ -37,7 +37,7 @@ Assert::match(<<<'EOD'
MIME-Version: 1.0
X-Mailer: Nette Framework
Date: %a%
From: John Doe <[email protected]>
From: =?UTF-8?B?IktkbyB1dGXEjWUsIG9ixJtkdsOhIg==?= <[email protected]>
To: Lady Jane <[email protected]>
Cc: [email protected]
Bcc: [email protected]
Expand Down

0 comments on commit 0f3a919

Please sign in to comment.