Permalink
Browse files

Supported japanese legacy charset and changed to use upper case chars…

…et in Content-Type line
  • Loading branch information...
1 parent b004992 commit f816f6ae1b13e6336548ea6ee8a5c4477d022005 @suzuki suzuki committed with markstory May 21, 2012
Showing with 78 additions and 8 deletions.
  1. +26 −5 lib/Cake/Network/Email/CakeEmail.php
  2. +52 −3 lib/Cake/Test/Case/Network/Email/CakeEmailTest.php
@@ -307,6 +307,14 @@ class CakeEmail {
protected $_charset8bit = array('UTF-8', 'SHIFT_JIS');
/**
+ * define Content-Type charset name
+ *
+ * @var array
+ */
+ protected $_contentTypeCharset = array('ISO-2022-JP-MS' => 'ISO-2022-JP');
+
+
+/**
* Constructor
* @param mixed $config Array of configs, or string to load configs from email.php
*
@@ -716,9 +724,9 @@ public function getHeaders($include = array()) {
if (!empty($this->_attachments) || $this->_emailFormat === 'both') {
$headers['Content-Type'] = 'multipart/mixed; boundary="' . $this->_boundary . '"';
} elseif ($this->_emailFormat === 'text') {
- $headers['Content-Type'] = 'text/plain; charset=' . $this->charset;
+ $headers['Content-Type'] = 'text/plain; charset=' . $this->_getContentTypeCharset();
} elseif ($this->_emailFormat === 'html') {
- $headers['Content-Type'] = 'text/html; charset=' . $this->charset;
+ $headers['Content-Type'] = 'text/html; charset=' . $this->_getContentTypeCharset();
}
$headers['Content-Transfer-Encoding'] = $this->_getContentTransferEncoding();
@@ -1434,7 +1442,7 @@ protected function _render($content) {
if (isset($rendered['text'])) {
if ($textBoundary !== $boundary || $hasAttachments) {
$msg[] = '--' . $textBoundary;
- $msg[] = 'Content-Type: text/plain; charset=' . $this->charset;
+ $msg[] = 'Content-Type: text/plain; charset=' . $this->_getContentTypeCharset();
$msg[] = 'Content-Transfer-Encoding: ' . $this->_getContentTransferEncoding();
$msg[] = '';
}
@@ -1447,7 +1455,7 @@ protected function _render($content) {
if (isset($rendered['html'])) {
if ($textBoundary !== $boundary || $hasAttachments) {
$msg[] = '--' . $textBoundary;
- $msg[] = 'Content-Type: text/html; charset=' . $this->charset;
+ $msg[] = 'Content-Type: text/html; charset=' . $this->_getContentTypeCharset();
$msg[] = 'Content-Transfer-Encoding: ' . $this->_getContentTransferEncoding();
$msg[] = '';
}
@@ -1559,4 +1567,17 @@ protected function _getContentTransferEncoding() {
return '7bit';
}
-}
+/**
+ * Return charset value for Content-Type
+ *
+ * @return string
+ */
+ protected function _getContentTypeCharset() {
+ $charset = strtoupper($this->charset);
+ if (array_key_exists($charset, $this->_contentTypeCharset)) {
+ return strtoupper($this->_contentTypeCharset[$charset]);
+ } else {
+ return strtoupper($this->charset);
+ }
+ }
+}
@@ -1269,8 +1269,8 @@ public function testMessage() {
$this->assertContains($expected, $this->CakeEmail->message(CakeEmail::MESSAGE_TEXT));
$message = $this->CakeEmail->message();
- $this->assertContains('Content-Type: text/plain; charset=utf-8', $message);
- $this->assertContains('Content-Type: text/html; charset=utf-8', $message);
+ $this->assertContains('Content-Type: text/plain; charset=UTF-8', $message);
+ $this->assertContains('Content-Type: text/html; charset=UTF-8', $message);
// UTF-8 is 8bit
$this->assertTrue($this->_checkContentTransferEncoding($message, '8bit'));
@@ -1539,10 +1539,59 @@ public function testBodyEncoding() {
$email->to('someone@example.com')->from('someone@example.com');
$result = $email->send('ってテーブルを作ってやってたらう');
- $this->assertContains('Content-Type: text/plain; charset=iso-2022-jp', $result['headers']);
+ $this->assertContains('Content-Type: text/plain; charset=ISO-2022-JP', $result['headers']);
$this->assertContains(mb_convert_encoding('ってテーブルを作ってやってたらう','ISO-2022-JP'), $result['message']);
}
+/**
+ * Tests that the body is encoded using the configured charset (Japanese standard encoding)
+ *
+ * @return void
+ */
+ public function testBodyEncodingIso2022Jp() {
+ $this->skipIf(!function_exists('mb_convert_encoding'));
+ $email = new CakeEmail(array(
+ 'charset' => 'iso-2022-jp',
+ 'headerCharset' => 'iso-2022-jp',
+ 'transport' => 'Debug'
+ ));
+ $email->subject('あれ?もしかしての前と');
+ $headers = $email->getHeaders(array('subject'));
+ $expected = "?ISO-2022-JP?B?GyRCJCIkbCEpJGIkNyQrJDckRiROQTAkSBsoQg==?=";
+ $this->assertContains($expected, $headers['Subject']);
+
+ $email->to('someone@example.com')->from('someone@example.com');
+ $result = $email->send('①㈱');
+ $this->assertTextContains("Content-Type: text/plain; charset=ISO-2022-JP", $result['headers']);
+ $this->assertTextNotContains("Content-Type: text/plain; charset=ISO-2022-JP-MS", $result['headers']); // not charset=iso-2022-jp-ms
+ $this->assertTextNotContains(mb_convert_encoding('①㈱','ISO-2022-JP-MS'), $result['message']);
+ }
+
+/**
+ * Tests that the body is encoded using the configured charset (Japanese irregular encoding, but sometime use this)
+ *
+ * @return void
+ */
+ public function testBodyEncodingIso2022JpMs() {
+ $this->skipIf(!function_exists('mb_convert_encoding'));
+ $email = new CakeEmail(array(
+ 'charset' => 'iso-2022-jp-ms',
+ 'headerCharset' => 'iso-2022-jp-ms',
+ 'transport' => 'Debug'
+ ));
+ $email->subject('あれ?もしかしての前と');
+ $headers = $email->getHeaders(array('subject'));
+ $expected = "?ISO-2022-JP?B?GyRCJCIkbCEpJGIkNyQrJDckRiROQTAkSBsoQg==?=";
+ $this->assertContains($expected, $headers['Subject']);
+
+ $email->to('someone@example.com')->from('someone@example.com');
+ $result = $email->send('①㈱');
+ $this->assertTextContains("Content-Type: text/plain; charset=ISO-2022-JP", $result['headers']);
+ $this->assertTextNotContains("Content-Type: text/plain; charset=iso-2022-jp-ms", $result['headers']); // not charset=iso-2022-jp-ms
+ $this->assertContains(mb_convert_encoding('①㈱','ISO-2022-JP-MS'), $result['message']);
+ }
+
+
protected function _checkContentTransferEncoding($message, $charset) {
$boundary = '--alt-' . $this->CakeEmail->getBoundary();
$result['text'] = false;

0 comments on commit f816f6a

Please sign in to comment.