Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Add charset() / headerCharset() methods to CakeEmail class #567

Merged
merged 1 commit into from

2 participants

Norio Suzuki José Lorenzo Rodríguez
Norio Suzuki

original pull request was #564. change branch to 2.2.


CakeEmail have many setter / getter methods for mail headers.
But, charset and headerChaset have a only property.

This is a strange. see below :

  • now
$email = new CakeEmail;

$email->charset = 'ISO-2022-JP';       // strange here
$email->headerCharset = 'ISO-2022-JP'; // strange here

$email->from('suzuki@example.com');
$email->to('suzuki@example.jp);
$emaul->subject('TEST MAIL');
$email->send('TEST MAIL BODY');
  • my patch
$email = new CakeEmail;

$email->charset('ISO-2022-JP');       // this is nice accessor.
$email->headerCharset('ISO-2022-JP'); // this is nice accessor.

$email->from('suzuki@example.com');
$email->to('suzuki@example.jp);
$emaul->subject('TEST MAIL');
$email->send('TEST MAIL BODY');
José Lorenzo Rodríguez
Owner

Thanks!

José Lorenzo Rodríguez lorenzo merged commit 47a2c22 into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
34 lib/Cake/Network/Email/CakeEmail.php
View
@@ -460,6 +460,37 @@ public function addBcc($email, $name = null) {
}
/**
+ * Charset setter/getter
+ *
+ * @param string $charset
+ * @return string $this->charset
+ */
+ public function charset($charset = null) {
+ if ($charset === null) {
+ return $this->charset;
+ }
+ $this->charset = $charset;
+ if (empty($this->headerCharset)) {
+ $this->headerCharset = $charset;
+ }
+ return $this->charset;
+ }
+
+/**
+ * HeaderCharset setter/getter
+ *
+ * @param string $charset
+ * @return string $this->charset
+ */
+ public function headerCharset($charset = null) {
+ if ($charset === null) {
+ return $this->headerCharset;
+ }
+ return $this->headerCharset = $charset;
+ }
+
+
+/**
* Set email
*
* @param string $varName
@@ -1116,6 +1147,9 @@ protected function _encode($text) {
$restore = mb_internal_encoding();
mb_internal_encoding($this->_appCharset);
}
+ if (empty($this->headerCharset)) {
+ $this->headerCharset = $this->charset;
+ }
$return = mb_encode_mimeheader($text, $this->headerCharset, 'B');
if ($internalEncoding) {
mb_internal_encoding($restore);
131 lib/Cake/Test/Case/Network/Email/CakeEmailTest.php
View
@@ -1498,4 +1498,135 @@ public function testEncode() {
. " =?ISO-2022-JP?B?GyRCJCYkSiRrJHMkQCRtJCYhKRsoQg==?=";
$this->assertSame($expected, $result);
}
+
+
+/**
+ * Tests charset setter/getter
+ *
+ * @return void
+ */
+ public function testCharset() {
+ $this->CakeEmail->charset('UTF-8');
+ $this->assertSame($this->CakeEmail->charset(), 'UTF-8');
+
+ $this->CakeEmail->charset('ISO-2022-JP');
+ $this->assertSame($this->CakeEmail->charset(), 'ISO-2022-JP');
+
+ $charset = $this->CakeEmail->charset('Shift_JIS');
+ $this->assertSame($charset, 'Shift_JIS');
+
+ }
+
+/**
+ * Tests headerCharset setter/getter
+ *
+ * @return void
+ */
+ public function testHeaderCharset() {
+ $this->CakeEmail->headerCharset('UTF-8');
+ $this->assertSame($this->CakeEmail->headerCharset(), 'UTF-8');
+
+ $this->CakeEmail->headerCharset('ISO-2022-JP');
+ $this->assertSame($this->CakeEmail->headerCharset(), 'ISO-2022-JP');
+
+ $charset = $this->CakeEmail->headerCharset('Shift_JIS');
+ $this->assertSame($charset, 'Shift_JIS');
+ }
+
+
+/**
+ * Tests for compatible check.
+ * charset property and charset() method.
+ * headerCharset property and headerCharset() method.
+ */
+ public function testCharsetsCompatible() {
+ $this->skipIf(!function_exists('mb_convert_encoding'));
+
+ $checkHeaders = array(
+ 'from' => true,
+ 'to' => true,
+ 'cc' => true,
+ 'subject' => true,
+ );
+
+ // Header Charset : null (used by default UTF-8)
+ // Body Charset : ISO-2022-JP
+ $oldStyleEmail = $this->_getEmailByOldStyleCharset('iso-2022-jp', null);
+ $oldStyleHeaders = $oldStyleEmail->getHeaders($checkHeaders);
+
+ $newStyleEmail = $this->_getEmailByNewStyleCharset('iso-2022-jp', null);
+ $newStyleHeaders = $newStyleEmail->getHeaders($checkHeaders);
+
+ $this->assertSame($oldStyleHeaders['From'], $newStyleHeaders['From']);
+ $this->assertSame($oldStyleHeaders['To'], $newStyleHeaders['To']);
+ $this->assertSame($oldStyleHeaders['Cc'], $newStyleHeaders['Cc']);
+ $this->assertSame($oldStyleHeaders['Subject'], $newStyleHeaders['Subject']);
+
+ // Header Charset : UTF-8
+ // Boby Charset : ISO-2022-JP
+ $oldStyleEmail = $this->_getEmailByOldStyleCharset('iso-2022-jp', 'utf-8');
+ $oldStyleHeaders = $oldStyleEmail->getHeaders($checkHeaders);
+
+ $newStyleEmail = $this->_getEmailByNewStyleCharset('iso-2022-jp', 'utf-8');
+ $newStyleHeaders = $newStyleEmail->getHeaders($checkHeaders);
+
+ $this->assertSame($oldStyleHeaders['From'], $newStyleHeaders['From']);
+ $this->assertSame($oldStyleHeaders['To'], $newStyleHeaders['To']);
+ $this->assertSame($oldStyleHeaders['Cc'], $newStyleHeaders['Cc']);
+ $this->assertSame($oldStyleHeaders['Subject'], $newStyleHeaders['Subject']);
+
+ // Header Charset : ISO-2022-JP
+ // Boby Charset : UTF-8
+ $oldStyleEmail = $this->_getEmailByOldStyleCharset('utf-8', 'iso-2022-jp');
+ $oldStyleHeaders = $oldStyleEmail->getHeaders($checkHeaders);
+
+ $newStyleEmail = $this->_getEmailByNewStyleCharset('utf-8', 'iso-2022-jp');
+ $newStyleHeaders = $newStyleEmail->getHeaders($checkHeaders);
+
+ $this->assertSame($oldStyleHeaders['From'], $newStyleHeaders['From']);
+ $this->assertSame($oldStyleHeaders['To'], $newStyleHeaders['To']);
+ $this->assertSame($oldStyleHeaders['Cc'], $newStyleHeaders['Cc']);
+ $this->assertSame($oldStyleHeaders['Subject'], $newStyleHeaders['Subject']);
+
+ }
+
+ private function _getEmailByOldStyleCharset($charset, $headerCharset) {
+ $email = new CakeEmail(array('transport' => 'Debug'));
+
+ if (! empty($charset)) {
+ $email->charset = $charset;
+ }
+ if (! empty($headerCharset)) {
+ $email->headerCharset = $headerCharset;
+ }
+
+ $email->from('someone@example.com', 'どこかの誰か');
+ $email->to('someperson@example.jp', 'どこかのどなたか');
+ $email->cc('miku@example.net', 'ミク');
+ $email->subject('テストメール');
+ $email->send('テストメールの本文');
+
+ return $email;
+ }
+
+ private function _getEmailByNewStyleCharset($charset, $headerCharset) {
+ $email = new CakeEmail(array('transport' => 'Debug'));
+
+ if (! empty($charset)) {
+ $email->charset($charset);
+ }
+ if (! empty($headerCharset)) {
+ $email->headerCharset($headerCharset);
+ }
+
+ $email->from('someone@example.com', 'どこかの誰か');
+ $email->to('someperson@example.jp', 'どこかのどなたか');
+ $email->cc('miku@example.net', 'ミク');
+ $email->subject('テストメール');
+ $email->send('テストメールの本文');
+
+ return $email;
+ }
+
+
}
Something went wrong with that request. Please try again.