Skip to content
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 <doe@example.com>');
$mail->setFrom('Kdo uteče, obědvá <doe@example.com>');

$mail->addTo('Lady Jane <jane@example.com>');
$mail->addCc('jane@example.info');
Expand All @@ -37,7 +37,7 @@ Assert::match(<<<'EOD'
MIME-Version: 1.0
X-Mailer: Nette Framework
Date: %a%
From: John Doe <doe@example.com>
From: =?UTF-8?B?IktkbyB1dGXEjWUsIG9ixJtkdsOhIg==?= <doe@example.com>
To: Lady Jane <jane@example.com>
Cc: jane@example.info
Bcc: bcc@example.com
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==?='));