From 738b20a19b58e675ea7948a4e4ca7564c2dd4b41 Mon Sep 17 00:00:00 2001 From: mark_story Date: Mon, 12 Jun 2017 10:57:35 -0400 Subject: [PATCH] Fix encoding of addreses containin comma & unicode Email addresses that contain both unicode and commas will not be correctly encoded by mime_encode_header if the comma precedes the unicode. In this scenario we have to quote the encoded address. Refs #10763 --- lib/Cake/Network/Email/CakeEmail.php | 5 ++- .../Test/Case/Network/Email/CakeEmailTest.php | 31 ++++++++++++++++--- 2 files changed, 31 insertions(+), 5 deletions(-) diff --git a/lib/Cake/Network/Email/CakeEmail.php b/lib/Cake/Network/Email/CakeEmail.php index 33e37ca68..3f27cd4af 100644 --- a/lib/Cake/Network/Email/CakeEmail.php +++ b/lib/Cake/Network/Email/CakeEmail.php @@ -830,7 +830,10 @@ class CakeEmail { $return[] = $email; } else { $encoded = $this->_encode($alias); - if ($encoded === $alias && preg_match('/[^a-z0-9 ]/i', $encoded)) { + if ( + $encoded === $alias && preg_match('/[^a-z0-9 ]/i', $encoded) || + strpos($encoded, ',') !== false + ) { $encoded = '"' . str_replace('"', '\"', $encoded) . '"'; } $return[] = sprintf('%s <%s>', $encoded, $email); diff --git a/lib/Cake/Test/Case/Network/Email/CakeEmailTest.php b/lib/Cake/Test/Case/Network/Email/CakeEmailTest.php index d27f2f709..0acf88ddf 100644 --- a/lib/Cake/Test/Case/Network/Email/CakeEmailTest.php +++ b/lib/Cake/Test/Case/Network/Email/CakeEmailTest.php @@ -450,10 +450,6 @@ class CakeEmailTest extends CakeTestCase { $expected = array('CakePHP ', 'Cake '); $this->assertSame($expected, $result); - $result = $this->CakeEmail->formatAddress(array('me@example.com' => 'Last, First')); - $expected = array('"Last, First" '); - $this->assertSame($expected, $result); - $result = $this->CakeEmail->formatAddress(array('me@example.com' => '"Last" First')); $expected = array('"\"Last\" First" '); $this->assertSame($expected, $result); @@ -471,6 +467,33 @@ class CakeEmailTest extends CakeTestCase { $this->assertSame($expected, $result); } +/** + * Test that addresses are quoted correctly when they contain unicode and + * commas + * + * @return void + */ + public function testFormatAddressEncodeAndEscape() + { + $result = $this->CakeEmail->formatAddress(array( + 'test@example.com' => 'Website, ascii' + )); + $expected = array('"Website, ascii" '); + $this->assertSame($expected, $result); + + $result = $this->CakeEmail->formatAddress(array( + 'test@example.com' => 'Wébsite, unicode' + )); + $expected = array('=?UTF-8?B?V8OpYnNpdGUsIHVuaWNvZGU=?= '); + $this->assertSame($expected, $result); + + $result = $this->CakeEmail->formatAddress(array( + 'test@example.com' => 'Website, électric' + )); + $expected = array('"Website, =?UTF-8?B?w6lsZWN0cmlj?=" '); + $this->assertSame($expected, $result); + } + /** * testFormatAddressJapanese *