test

CakePHP Note

EmailComponent

UPDATE:2008.04.06

メール送信用コンポーネント。レイアウトとエレメントによるビューが使え、添付ファイルやHTMLメールを送信する事も出来ます。

*日本語対応は、、、

ビュー

レイアウトとエレメントが使えます。

レイアウト

  • app
    • view
      • layouts
        • email
          • html
            • default.ctp(HTMLメール用)
          • text
            • default.ctp(テキスト・メール用)

エレメント

  • app
    • view
      • elements
        • email
          • html
            • welcome.ctp
          • text
            • welcome.ctp

実装

コントローラーの$components属性に'Email'を設定してEmailComponentを有効にします。

class UsersController extends AppController {
    var $components = array('Email');
    
    function welcome(){
        //$Userにユーザーの情報が入ったとする
        $this->Email->to = $User['User']['email'];
        $this->Email->bcc = array('secret@example.com');
        $this->Email->subject = 'Welcome to our site';
        $this->Email->replyTo = 'support@example.com';
        $this->Email->from = 'Cool Web App ';
        $this->Email->template = 'welcome_message'; //「.ctp」を省いた名前
        //'html' または 'text' あるいは 'both' (デフォルトは 'text')
        $this->Email->sendAs = 'both';
        //データをセット
        $this->set('User', $User);
        //メールを送信
        $this->Email->send();
    }
}

属性

$to(送信先)
$cc(CC送信先)
$bcc(BCC送信先)
$replyTo(返信先)
$from(送信者)
$subject(件名)
$template(テンプレート/app/view/elements/email/以下に配置)
$sendAs(メール形式:texxかhtml または both)
$attachments(添付するファイル)
$delivery(送信方法、mailまたはsmtp)
$smtpOptions(smtpの場合の設定)