diff --git a/src/Mailer/Email.php b/src/Mailer/Email.php index f1ff17765d9..fc1973714d4 100644 --- a/src/Mailer/Email.php +++ b/src/Mailer/Email.php @@ -695,12 +695,13 @@ protected function _addEmail($varName, $email, $name) * Get/Set Subject. * * @param string|null $subject Subject string. + * @param bool $decode Whether to decode the subject. * @return string|$this */ - public function subject($subject = null) + public function subject($subject = null, $decode = false) { if ($subject === null) { - return $this->_subject; + return ($decode) ? $this->_decode($this->_subject) : $this->_subject; } $this->_subject = $this->_encode((string)$subject); return $this; @@ -1483,6 +1484,21 @@ protected function _encode($text) return $return; } + /** + * Decode the specified string + * + * @param string $text String to decode + * @return string Decoded string + */ + protected function _decode($text) + { + $restore = mb_internal_encoding(); + mb_internal_encoding($this->_appCharset); + $return = mb_decode_mimeheader($text); + mb_internal_encoding($restore); + return $return; + } + /** * Translates a string for one charset to another if the App.encoding value * differs and the mb_convert_encoding function exists diff --git a/tests/TestCase/Mailer/EmailTest.php b/tests/TestCase/Mailer/EmailTest.php index ff20f3f719a..b7ae2ed7418 100644 --- a/tests/TestCase/Mailer/EmailTest.php +++ b/tests/TestCase/Mailer/EmailTest.php @@ -71,6 +71,16 @@ public function encode($text) return $this->_encode($text); } + /** + * Decode to protected method + * + * @return string + */ + public function decode($text) + { + return $this->_decode($text); + } + /** * Render to protected method * @@ -563,9 +573,11 @@ public function testSubject() $this->CakeEmail->subject(1); $this->assertSame('1', $this->CakeEmail->subject()); - $this->CakeEmail->subject('هذه رسالة بعنوان طويل مرسل للمستلم'); + $input = 'هذه رسالة بعنوان طويل مرسل للمستلم'; + $this->CakeEmail->subject($input); $expected = '=?UTF-8?B?2YfYsNmHINix2LPYp9mE2Kkg2KjYudmG2YjYp9mGINi32YjZitmEINmF2LE=?=' . "\r\n" . ' =?UTF-8?B?2LPZhCDZhNmE2YXYs9iq2YTZhQ==?='; $this->assertSame($expected, $this->CakeEmail->subject()); + $this->assertSame($input, $this->CakeEmail->subject(null, true)); } /** @@ -2343,6 +2355,26 @@ public function testEncode() $this->assertSame($expected, $result); } + /** + * Test CakeEmail::_decode function + * + * @return void + */ + public function testDecode() + { + $this->CakeEmail->headerCharset = 'ISO-2022-JP'; + $result = $this->CakeEmail->decode('=?ISO-2022-JP?B?GyRCRnxLXDhsGyhC?='); + $expected = '日本語'; + $this->assertSame($expected, $result); + + $this->CakeEmail->headerCharset = 'ISO-2022-JP'; + $result = $this->CakeEmail->decode("=?ISO-2022-JP?B?GyRCRDkkJEQ5JCREOSQkGyhCU3ViamVjdBskQiROPmw5ZyRPGyhCZm9s?=\r\n" . + " =?ISO-2022-JP?B?ZGluZxskQiQ5JGskTiQsQDUkNyQkJHMkQCQxJEkkJCRDJD8kJCRJGyhC?=\r\n" . + " =?ISO-2022-JP?B?GyRCJCYkSiRrJHMkQCRtJCYhKRsoQg==?="); + $expected = '長い長い長いSubjectの場合はfoldingするのが正しいんだけどいったいどうなるんだろう?'; + $this->assertSame($expected, $result); + } + /** * Tests charset setter/getter *