From 8c1b9b3e5b52981551fc6044539af1d974e39064 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=A1lint=20Szekeres?= Date: Thu, 17 Oct 2024 13:18:53 +0200 Subject: [PATCH] [Mime] fix encoding issue with UTF-8 addresses containing doubles spaces --- Header/AbstractHeader.php | 14 ++++++++++++++ Tests/Header/MailboxHeaderTest.php | 8 ++++++++ 2 files changed, 22 insertions(+) diff --git a/Header/AbstractHeader.php b/Header/AbstractHeader.php index d61df57..2670367 100644 --- a/Header/AbstractHeader.php +++ b/Header/AbstractHeader.php @@ -180,6 +180,20 @@ protected function getEncodableWordTokens(string $string): array $tokens[] = $encodedToken; } + foreach ($tokens as $i => $token) { + // whitespace(s) between 2 encoded tokens + if ( + 0 < $i + && isset($tokens[$i + 1]) + && preg_match('~^[\t ]+$~', $token) + && $this->tokenNeedsEncoding($tokens[$i - 1]) + && $this->tokenNeedsEncoding($tokens[$i + 1]) + ) { + $tokens[$i - 1] .= $token.$tokens[$i + 1]; + array_splice($tokens, $i, 2); + } + } + return $tokens; } diff --git a/Tests/Header/MailboxHeaderTest.php b/Tests/Header/MailboxHeaderTest.php index 2fc8e1e..bddadee 100644 --- a/Tests/Header/MailboxHeaderTest.php +++ b/Tests/Header/MailboxHeaderTest.php @@ -62,6 +62,14 @@ public function testUtf8CharsInLocalPart() { $header = new MailboxHeader('Sender', new Address('fabïen@symfony.com')); $this->assertSame('fabïen@symfony.com', $header->getBodyAsString()); + + // name with single space + $header = new MailboxHeader('Sender', new Address('fabïen@symfony.com', 'Fabïen Pötencier')); + $this->assertSame('=?utf-8?Q?Fab=C3=AFen_P=C3=B6tencier?= ', $header->getBodyAsString()); + + // name with double spaces + $header = new MailboxHeader('Sender', new Address('fabïen@symfony.com', 'Fabïen Pötencier')); + $this->assertSame('=?utf-8?Q?Fab=C3=AFen__P=C3=B6tencier?= ', $header->getBodyAsString()); } public function testToString()