From 41997b06e19eb7c7572d0b9d926bac4037af3849 Mon Sep 17 00:00:00 2001 From: mark_story Date: Sun, 18 Jul 2010 20:48:15 -0400 Subject: [PATCH] Fixing issues where mb_internal_encoding() might not be correctly set, causing email subjects to be incorrectly encoded. Tests added. Fixes #904 --- cake/libs/controller/components/email.php | 11 ++++++- .../libs/controller/components/email.test.php | 32 +++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/cake/libs/controller/components/email.php b/cake/libs/controller/components/email.php index b341f3965..aae446593 100755 --- a/cake/libs/controller/components/email.php +++ b/cake/libs/controller/components/email.php @@ -723,7 +723,16 @@ class EmailComponent extends Object{ if ($this->delivery == 'mail') { $nl = ''; } - return mb_encode_mimeheader($subject, $this->charset, 'B', $nl); + $internalEncoding = function_exists('mb_internal_encoding'); + if ($internalEncoding) { + $restore = mb_internal_encoding(); + mb_internal_encoding($this->charset); + } + $return = mb_encode_mimeheader($subject, $this->charset, 'B', $nl); + if ($internalEncoding) { + mb_internal_encoding($restore); + } + return $return; } /** diff --git a/cake/tests/cases/libs/controller/components/email.test.php b/cake/tests/cases/libs/controller/components/email.test.php index 521a3fbb9..6d19c77f0 100755 --- a/cake/tests/cases/libs/controller/components/email.test.php +++ b/cake/tests/cases/libs/controller/components/email.test.php @@ -857,7 +857,38 @@ HTMLBLOC; $result = $this->Controller->EmailTest->strip($content, true); $expected = $content; $this->assertEqual($result, $expected); + } +/** + * test that the _encode() will set mb_internal_encoding. + * + * @return void + */ + function test_encodeSettingInternalCharset() { + $skip = !function_exists('mb_internal_encoding'); + if ($this->skipIf($skip, 'Missing mb_* functions, cannot run test.')) { + return; + } + mb_internal_encoding('ISO-8859-1'); + + $this->Controller->charset = 'UTF-8'; + $this->Controller->EmailTest->to = 'postmaster@localhost'; + $this->Controller->EmailTest->from = 'noreply@example.com'; + $this->Controller->EmailTest->subject = 'هذه رسالة بعنوان طويل مرسل للمستلم'; + $this->Controller->EmailTest->replyTo = 'noreply@example.com'; + $this->Controller->EmailTest->template = null; + $this->Controller->EmailTest->delivery = 'debug'; + + $this->Controller->EmailTest->sendAs = 'text'; + $this->assertTrue($this->Controller->EmailTest->send('This is the body of the message')); + + $subject = '=?UTF-8?B?2YfYsNmHINix2LPYp9mE2Kkg2KjYudmG2YjYp9mGINi32YjZitmEINmF2LE=?=' . "\r\n" . ' =?UTF-8?B?2LPZhCDZhNmE2YXYs9iq2YTZhQ==?='; + + preg_match('/Subject: (.*)Header:/s', $this->Controller->Session->read('Message.email.message'), $matches); + $this->assertEqual(trim($matches[1]), $subject); + + $result = mb_internal_encoding(); + $this->assertEqual($result, 'ISO-8859-1'); } /** @@ -867,6 +898,7 @@ HTMLBLOC; * @return void */ function testMultibyte() { + $this->Controller->charset = 'UTF-8'; $this->Controller->EmailTest->to = 'postmaster@localhost'; $this->Controller->EmailTest->from = 'noreply@example.com'; $this->Controller->EmailTest->subject = 'هذه رسالة بعنوان طويل مرسل للمستلم';