LoginSignup
15
13

More than 5 years have passed since last update.

Phalconでvoltを使ってメールを送る

Last updated at Posted at 2014-05-06

PHPフレームワークのPhalconにおいて、標準テンプレートエンジンのvoltを使ってメールを送る方法

準備

メールのテンプレートファイルは app/views/emails/example.volt のように格納する。

Controllerの拡張

app/controllers/ControllerBase.php を次の通り編集する。

<?php
use Phalcon\Mvc\Controller;

class ControllerBase extends Controller
{
    /**
     * テンプレートを使ってメールを送る
     * 
     * @param string $to 送信先メールアドレス
     * @param string $subject メールのタイトル
     * @param string $template テンプレートの名称
     * @param string $headers 追加ヘッダ(mb_send_mail互換)
     * @param string $params 追加パラメータ(mb_send_mail互換)
     * @return boolean 成否
     */
    protected function mail($to, $subject, $template, $headers = null, $params = null)
    {
        if (empty($template)) {
            return false;
        }

        $view = clone $this->view;
        $view->start();
        $view->setRenderLevel(View::LEVEL_ACTION_VIEW);
        $view->render('emails', $template);
        $view->finish();
        $body = $view->getContent();

        return mb_send_mail($to, $subject, $body, $headers, $params);
    }
}

利用方法

各コントローラからは基底クラスに ControllerBase を指定した上で $this->mail() を呼べばOK。

15
13
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
15
13