Skip to content

Commit

Permalink
Fixing issues where mb_internal_encoding() might not be correctly set…
Browse files Browse the repository at this point in the history
…, causing email subjects to be incorrectly encoded. Tests added. Fixes #904
  • Loading branch information
markstory committed Jul 19, 2010
1 parent 4c27c24 commit 41997b0
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
11 changes: 10 additions & 1 deletion cake/libs/controller/components/email.php
Expand Up @@ -723,7 +723,16 @@ function _encode($subject) {
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;
}

/**
Expand Down
32 changes: 32 additions & 0 deletions cake/tests/cases/libs/controller/components/email.test.php
Expand Up @@ -857,7 +857,38 @@ function testContentStripping() {
$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');
}

/**
Expand All @@ -867,6 +898,7 @@ function testContentStripping() {
* @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 = 'هذه رسالة بعنوان طويل مرسل للمستلم';
Expand Down

0 comments on commit 41997b0

Please sign in to comment.