diff --git a/Header/AbstractHeader.php b/Header/AbstractHeader.php index 9994ec6..3dc7faf 100644 --- a/Header/AbstractHeader.php +++ b/Header/AbstractHeader.php @@ -188,6 +188,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()