Fixing issues where mb_internal_encoding() might not be correctly set, causing email subjects to be incorrectly encoded. Tests added. Fixes #904

This commit is contained in:
mark_story 2010-07-18 20:48:15 -04:00
parent 4c27c24a72
commit 41997b06e1
2 changed files with 42 additions and 1 deletions

View file

@ -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;
}
/**

View file

@ -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 = 'هذه رسالة بعنوان طويل مرسل للمستلم';