Skip to content
Permalink
Browse files

Add support for returning decoded subjects

  • Loading branch information...
Marlinc committed Mar 18, 2016
1 parent f41fd23 commit c694b47503f883ef46f9e0310290addec4d069ab
Showing with 51 additions and 3 deletions.
  1. +18 −2 src/Mailer/Email.php
  2. +33 −1 tests/TestCase/Mailer/EmailTest.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
@@ -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
*

0 comments on commit c694b47

Please sign in to comment.
You can’t perform that action at this time.