Skip to content

Commit

Permalink
Add support for returning decoded subjects
Browse files Browse the repository at this point in the history
  • Loading branch information
Marlinc committed Mar 19, 2016
1 parent f41fd23 commit c694b47
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 3 deletions.
20 changes: 18 additions & 2 deletions src/Mailer/Email.php
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
34 changes: 33 additions & 1 deletion tests/TestCase/Mailer/EmailTest.php
Expand Up @@ -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
*
Expand Down Expand Up @@ -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));
}

/**
Expand Down Expand Up @@ -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
*
Expand Down

0 comments on commit c694b47

Please sign in to comment.