-
-
Save nojimage/805732 to your computer and use it in GitHub Desktop.
CakePHP version 1.3.x EmailComponent for Japanese
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* | |
* AppEmailComponent (for Japanese) | |
* | |
* Copyright 2010, nojimage (http://php-tips.com/) | |
* | |
* Licensed under The MIT License | |
* Redistributions of files must retain the above copyright notice. | |
* | |
* @filesource | |
* @package app | |
* @subpackage app.controller.components | |
* @author nojimage <nojimage at gmail.com> | |
* @copyright 2010 nojimage | |
* @license http://www.opensource.org/licenses/mit-license.php The MIT License | |
* @link http://php-tips.com/ | |
* | |
*/ | |
App::import('Core', 'Multibyte'); | |
App::import('Component', 'Email'); | |
class AppEmailComponent extends EmailComponent { | |
var $params; | |
/** | |
* Startup component | |
* | |
* @param object $controller Instantiating controller | |
* @access public | |
*/ | |
function startup(&$controller) { | |
$this->Controller =& $controller; | |
mb_language('Japanese'); | |
// 設定読み出し | |
Configure::load('email'); | |
$this->params = Configure::read('Email'); | |
$this->load($this->Controller->name); | |
} | |
/** | |
* 設定ファイルから値読み込み | |
* | |
* @param $config | |
* @return unknown_type | |
*/ | |
function load($config = 'default') { | |
$keys = array('to', 'cc', 'bcc', 'from', 'replayTo', 'subject', 'lineLength', 'xMailer', 'delivery', 'sendAs', 'smtpOptions', 'layout', 'template', '_debug'); | |
foreach ($keys as $key) { | |
if (!empty($this->params[$config][$key])) { | |
$this->{$key} = $this->params[$config][$key]; | |
} else if (!empty($this->params['default'][$key])) { | |
$this->{$key} = $this->params['default'][$key]; | |
} | |
} | |
} | |
/** | |
* (non-PHPdoc) | |
* @see cake/libs/controller/components/EmailComponent#reset() | |
* @param $config | |
* @return unknown_type | |
*/ | |
function reset($config = '') { | |
parent::reset(); | |
if (empty($config)) { | |
$config = $this->Controller->name; | |
} | |
$this->load($config); | |
} | |
/** | |
* Wrap the message using EmailComponent::$lineLength | |
* 日本語対応 | |
* | |
* @param string $message Message to wrap | |
* @return array Wrapped message | |
* @access private | |
*/ | |
function _wrap($message) { | |
$encoding = strtolower(Configure::read('App.encoding')); | |
$message = $this->_strip($message, true); | |
$message = str_replace(array("\r\n","\r"), "\n", $message); | |
$lines = explode("\n", $message); | |
$formatted = array(); | |
// 行頭禁則文字 | |
$wordwrap_not_head = '、。.)«\]}}〕〉》」』】〙〗〟»ヽヾ'; | |
// 行末禁則文字 | |
$wordwrap_not_foot = '(\[{{〔〈《「『【〘〖‘“«'; | |
if ($this->_lineLength !== null) { | |
trigger_error(__('_lineLength cannot be accessed please use lineLength', true), E_USER_WARNING); | |
$this->lineLength = $this->_lineLength; | |
} | |
foreach ($lines as $line) { | |
if(substr($line, 0, 1) == '.') { | |
$line = '.' . $line; | |
} | |
// 正規表現を使用するためutf-8エンコード | |
if ($encoding != 'UTF-8') { | |
$line = mb_convert_encoding($line, 'UTF-8', $encoding); | |
} | |
if (preg_match('!https?://|ftp://!', $line)) { | |
// httpを含む行は折り返しを行わない | |
$formatted[] = $line; | |
continue; | |
} | |
// $this->lineLengthで折り返す | |
$tmp_line = ''; | |
$parts = array(); | |
while (mb_strwidth($tmp_line . $line, 'UTF-8') > $this->lineLength ) { | |
// 1字切り出し | |
$chr = mb_substr($line, 0, 1, 'UTF-8'); | |
$line = mb_substr($line, 1, mb_strlen($line), 'UTF-8'); | |
$tmp_line .= $chr; | |
if (mb_strwidth($tmp_line, 'UTF-8') >= $this->lineLength) { | |
$next_chr = mb_substr($line, 0, 1, 'UTF-8'); | |
if (mb_strpos($wordwrap_not_foot, $chr, 0, 'UTF-8') !== false | |
|| (strlen($next_chr) > 0 && mb_strpos($wordwrap_not_head, $next_chr, 0, 'UTF-8') !== false)) { | |
// 行末禁則文字に切り出し文字が該当する場合 or 行頭禁則文字に次の文字が該当する場合 | |
continue; | |
} | |
$parts[] = $tmp_line; | |
$tmp_line = ''; | |
} | |
} | |
$parts[] = $tmp_line . $line; | |
$formatted = array_merge($formatted, $parts); | |
} | |
$formatted[] = ''; | |
// charset指定エンコードへ変換 | |
for ($i = 0; $i < count($formatted); $i++) { | |
$formatted[$i] = mb_convert_encoding($formatted[$i], $this->charset, 'UTF-8'); | |
} | |
return $formatted; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* | |
* Email Config Example | |
* | |
* Copyright 2010, nojimage (http://php-tips.com/) | |
* | |
* Licensed under The MIT License | |
* Redistributions of files must retain the above copyright notice. | |
* | |
* @filesource | |
* @package app | |
* @subpackage app.config | |
* @author nojimage <nojimage at gmail.com> | |
* @copyright 2010 nojimage | |
* @license http://www.opensource.org/licenses/mit-license.php The MIT License | |
* @link http://php-tips.com/ | |
* | |
*/ | |
$config['Email'] = array( | |
'default' => array( | |
'delivery' => 'mail', // 送信方法 (mail or smtp) | |
/* // smtpを利用する場合 | |
'smtpOptions' => array( | |
'host' => '', | |
'port' => 587, | |
'username' => '', | |
'password' => ''), | |
*/ | |
'sendAs' => 'text', // メールタイプ (text or html or both) | |
'from' => '[email protected]', // メールの送信元(From:) | |
'subject' => 'メール送信', // メールの表題(Subject:) | |
'lineLength' => 72, // 本文の折り返し文字数(全角は2文字としてカウント) | |
'xMailer' => 'CakePHP AppEmail' /* X-Mailerヘッダ */), | |
/* コントローラ別の設定 */ | |
'Users' => array( | |
'subject' => 'ユーザ登録完了のお知らせ', | |
'template' => 'user_registed') | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment