mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2024-11-15 03:18:26 +00:00
Merge pull request #567 from suzuki/2.2-cakeemail
Add charset() / headerCharset() methods to CakeEmail class
This commit is contained in:
commit
47a2c22af4
2 changed files with 165 additions and 0 deletions
|
@ -459,6 +459,37 @@ class CakeEmail {
|
|||
return $this->_addEmail('_bcc', $email, $name);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
*
|
||||
|
@ -1116,6 +1147,9 @@ class CakeEmail {
|
|||
$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);
|
||||
|
|
|
@ -1498,4 +1498,135 @@ class CakeEmailTest extends CakeTestCase {
|
|||
. " =?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;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue