EmailComponentを使ってメール送信
cake1.1の頃にあったEmailComponentはまともに使えなかったけれど、1.2からはマシになったと話を聞いていたので、1.3の環境で試してみた。
参考サイトはこちら
http://koshikawa.net/tp/promenade.cgi?id=24
EmailComponentの使い方。htmlメール&テンプレート。CakePHPメール - CPA-LABテクニカル
まずはメール送信を行いたいcontrollerにEmailComponentを追加
class HogeController extends AppController { var $components = array('Email');
action側でメール送信に必要なプロパティをセット
$this->Email->reset(); $this->Email->to = 'example@example.com'; $this->Email->from = 'example@example.com'; $this->Email->return = 'example@example.com'; $this->Email->replyTo = 'example@example.com'; $this->Email->xMailer = 'exampleMailer'; $this->Email->language = 'Japanese'; $this->Email->charset = 'ISO-2022-JP'; $this->Email->subject = 'メール件名'; $this->Email->send('メール本文');
このままでもメール送信そのものはできますが、文字化けするので自前でエンコード。
$this->Email->subject = mb_convert_encoding('メール件名', $this->Email->charset, Configure::read('App.encoding')); $body = 'メールの本文'; $this->Email->send(mb_convert_encoding($body,$this->Email->charset, Configure::read('App.encoding')));
また、本文が70文字を超えると自動改行されて、そこで文字化けが発生してしまうので、一行の文字数を大きくしておく。(長いURLを送りたいときの改行も防げる)
$this->Email->lineLength = 1024;
というわけで、結局はこんな感じで書いてます。
function send($to, $subject, $body){ $this->Email->to = $to; $this->Email->from = 'example@example.com'; $this->Email->return = 'example@example.com';; $this->Email->replyTo = 'example@example.com';; $this->Email->xMailer = 'exampleMailer'; $this->Email->language = 'Japanese'; $this->Email->charset = 'ISO-2022-JP'; $this->Email->lineLength = 1024; $this->Email->subject = $this->_convEnc($subject); $this->Email->send($this->_convEnc($body)); } function _convEnc($value){ return mb_convert_encoding($value, $this->Email->charset, Configure::read('App.encoding')); }
今回↓もかなり参考になりました。
- 作者: 滝下真玄
- 出版社/メーカー: 秀和システム
- 発売日: 2010/09/27
- メディア: 単行本
- 購入: 3人 クリック: 112回
- この商品を含むブログ (11件) を見る