Skip to content
This repository was archived by the owner on Jan 30, 2020. It is now read-only.

Commit 8feb312

Browse files
committed
fix AddressList toString method to quote semicolon
1 parent ece418b commit 8feb312

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

src/Header/AbstractAddressList.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,9 @@ public function getFieldValue($format = HeaderInterface::FORMAT_RAW)
134134
$email = $address->getEmail();
135135
$name = $address->getName();
136136

137-
if (! empty($name) && false !== strstr($name, ',')) {
137+
// quote $name if value requires so
138+
if (! empty($name) && (false !== strpos($name, ',') || false !== strpos($name, ';'))) {
139+
// FIXME: what if name contains double quote?
138140
$name = sprintf('"%s"', $name);
139141
}
140142

@@ -240,7 +242,7 @@ protected static function getComments($value)
240242
* Supposed to be private, protected as a workaround for PHP bug 68194
241243
*
242244
* @param string $value
243-
* @return void
245+
* @return string
244246
*/
245247
protected static function stripComments($value)
246248
{

test/Storage/MessageTest.php

+25
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Exception as GeneralException;
1111
use PHPUnit\Framework\TestCase;
1212
use Zend\Mail\Exception as MailException;
13+
use Zend\Mail\Headers;
1314
use Zend\Mail\Storage;
1415
use Zend\Mail\Storage\Exception;
1516
use Zend\Mail\Storage\Message;
@@ -24,6 +25,7 @@
2425
class MessageTest extends TestCase
2526
{
2627
protected $file;
28+
2729
protected $file2;
2830

2931
public function setUp()
@@ -432,6 +434,29 @@ public function testSpaceInFieldName()
432434
$this->assertEquals(Mime\Decode::splitHeaderField($header, 'baz'), 42);
433435
}
434436

437+
/**
438+
* splitMessage with Headers as input fails to process AddressList with semicolons
439+
*/
440+
public function testHeadersLosesNameQuoting()
441+
{
442+
$headerList = [
443+
'From: "Famous bearings |;" <[email protected]>',
444+
'Reply-To: "Famous bearings |:" <[email protected]>',
445+
];
446+
447+
// create Headers object from array
448+
Mime\Decode::splitMessage(implode("\r\n", $headerList), $headers1, $body);
449+
$this->assertInstanceOf(Headers::class, $headers1);
450+
// create Headers object from Headers object
451+
Mime\Decode::splitMessage($headers1, $headers2, $body);
452+
$this->assertInstanceOf(Headers::class, $headers2);
453+
454+
// test that same problem does not happen with Storage\Message internally
455+
$message = new Message(['headers' => $headers2, 'content' => (string)$body]);
456+
$this->assertEquals('"Famous bearings |;" <[email protected]>', $message->from);
457+
$this->assertEquals('Famous bearings |: <[email protected]>', $message->replyTo);
458+
}
459+
435460
/**
436461
* @group ZF2-372
437462
*/

0 commit comments

Comments
 (0)