Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MimePart: fixed not escaped header before encoded into utf-8 #102

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
6 changes: 4 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 All @@ -50,3 +50,5 @@ Assert::match(<<<'EOD'
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 7bit
EOD, TestMailer::$output);

Assert::match('"Kdo uteče, obědvá"', iconv_mime_decode('=?UTF-8?B?IktkbyB1dGXEjWUsIG9ixJtkdsOhIg==?='));