From 3ee2a5e5c1a5fb2d7e5a29e544189a2846fc313f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elan=20Ruusam=C3=A4e?= Date: Sat, 6 Jul 2019 05:18:08 +0300 Subject: [PATCH] fix AddressList toString method to quote semicolon --- CHANGELOG.md | 2 +- src/Header/AbstractAddressList.php | 6 ++++-- test/Storage/MessageTest.php | 27 +++++++++++++++++++++++++++ 3 files changed, 32 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e7ac968e..d516f8c1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,7 +22,7 @@ All notable changes to this project will be documented in this file, in reverse ### Fixed -- Nothing. +- [#230](https://github.com/zendframework/zend-mail/pull/230) Fix AddressList toString method to quote semicolon ## 2.10.0 - 2018-06-07 diff --git a/src/Header/AbstractAddressList.php b/src/Header/AbstractAddressList.php index ab48f671..a058a988 100644 --- a/src/Header/AbstractAddressList.php +++ b/src/Header/AbstractAddressList.php @@ -134,7 +134,9 @@ public function getFieldValue($format = HeaderInterface::FORMAT_RAW) $email = $address->getEmail(); $name = $address->getName(); - if (! empty($name) && false !== strstr($name, ',')) { + // quote $name if value requires so + if (! empty($name) && (false !== strpos($name, ',') || false !== strpos($name, ';'))) { + // FIXME: what if name contains double quote? $name = sprintf('"%s"', $name); } @@ -240,7 +242,7 @@ protected static function getComments($value) * Supposed to be private, protected as a workaround for PHP bug 68194 * * @param string $value - * @return void + * @return string */ protected static function stripComments($value) { diff --git a/test/Storage/MessageTest.php b/test/Storage/MessageTest.php index fcc9c5d2..2cacea05 100644 --- a/test/Storage/MessageTest.php +++ b/test/Storage/MessageTest.php @@ -10,6 +10,7 @@ use Exception as GeneralException; use PHPUnit\Framework\TestCase; use Zend\Mail\Exception as MailException; +use Zend\Mail\Headers; use Zend\Mail\Storage; use Zend\Mail\Storage\Exception; use Zend\Mail\Storage\Message; @@ -24,6 +25,7 @@ class MessageTest extends TestCase { protected $file; + protected $file2; public function setUp() @@ -432,6 +434,31 @@ public function testSpaceInFieldName() $this->assertEquals(Mime\Decode::splitHeaderField($header, 'baz'), 42); } + /** + * splitMessage with Headers as input fails to process AddressList with semicolons + * + * @see https://github.com/zendframework/zend-mail/pull/230 + */ + public function testHeadersLosesNameQuoting() + { + $headerList = [ + 'From: "Famous bearings |;" ', + 'Reply-To: "Famous bearings |:" ', + ]; + + // create Headers object from array + Mime\Decode::splitMessage(implode("\r\n", $headerList), $headers1, $body); + $this->assertInstanceOf(Headers::class, $headers1); + // create Headers object from Headers object + Mime\Decode::splitMessage($headers1, $headers2, $body); + $this->assertInstanceOf(Headers::class, $headers2); + + // test that same problem does not happen with Storage\Message internally + $message = new Message(['headers' => $headers2, 'content' => (string)$body]); + $this->assertEquals('"Famous bearings |;" ', $message->from); + $this->assertEquals('Famous bearings |: ', $message->replyTo); + } + /** * @group ZF2-372 */