LoginSignup
6
5

More than 5 years have passed since last update.

軽量テンプレートメールエンジン「Ezmail」

Last updated at Posted at 2013-06-25

説明

使い回しの出来るメールテンプレートエンジンが見つからなかったので自作してみました。
Smartyライクにテンプレートに変数を挿入出来ます。
githubでも公開しています。

準備

1.class.ezmail.phpを用意
2.同じフォルダにtemplatesフォルダを作成
3.txtやtplでメールテンプレートをtemplatesフォルダ内に作成
4.下記「mail.php」のように呼び出し・実行

注意

一応文字化けには配慮していますが、自己責任でお願いします。
ifやforeach等の条件分岐には未対応。
assign位しか使えないトホホ仕様です。

class.ezmail.php
<?php
class EzMail {

    //初期設定
    private $templates_dir;
    private $char;
    private $from;  
    private $cc;
    private $bcc;
    public function __construct() {
        $this->templates_dir = './templates/';
        $this->char = 'utf-8';
        $this->from = 'anomymous@anomymous.com'.PHP_EOL;
        $this->cc = null;
        $this->bcc = null;
    }

    //テンプレートセット
    public function setTempdir($templates_dir = NULL) {
        if(!is_null($templates_dir)) {
            $this->templates_dir = $templates_dir;
        } else {
            $this->templates_dir = './templates/';
        }
        return $this->templates_dir;
    }

    private $templateName;
    public function setTempfile($filename) {
        $this->templateName = $filename;
        return $this->templateName;
    }

    private $templatebody;
    private function __getTemplate() {
        if(file_exists($this->templates_dir.$this->templateName)) {
            $this->templatebody = file_get_contents($this->templates_dir.$this->templateName);
            $this->templatebody = mb_convert_encoding($this->templatebody, $this->char,"auto");
            return $this->templatebody;
        } else {
            return null;
        }
    }

    //From to cc bccセット
    public function setFrom($from = NULL) {
        if(!is_null($from)) {
            $this->from = "From: ".$from.PHP_EOL;
        } else {
            $this->from = NULL;
        }
        return $this->from;
    }
    private $to;
    public function setTo($to) {
        $this->to = $to;
        return $this->to;
    }

    public function setCc($cc = array()) {
        if(!empty($cc)) {
        if(is_array($cc)) {
            $this->cc = "Cc: ".rtrim(implode(',',$cc),',').PHP_EOL;
        } else {
            $this->cc = "Cc: ".$cc.PHP_EOL;
        }
        } else {
        return null;
        }   
    }

    public function setBcc($bcc = array()) {
        if(!empty($bcc)) {
        if(is_array($bcc)) {
            $this->bcc = "Bcc: ".rtrim(implode(',',$bcc),',').PHP_EOL;
        } else {
            $this->bcc = "Bcc: ".$bcc.PHP_EOL;
        }
        } else {
        return null;
        }
    }

    //文字コードセット
    public function setChar($char = null) {
        if(!is_null($char)) {
            $this->char = $char;
        } else {
            $this->char = "utf-8";
        }
        return $this->char;
    }

    //件名セット
    private $subject;
    public function setSubject($subject = null) {
        if(!is_null($subject)) {
            $this->subject = $subject;
        } else {
            $this->subject = "無題";
        }
    }

    //変数取得
    private $data;
    public function assign($key,$value) {
        if(!is_array($value)) {
            $this->data[$key] = $value;
        } else {
            foreach($value as $k=>$v) {
                $this->data[$key][$k] = $v;
            }
        }
        return $this->data;
    }

    private $body;
    private function __setassign($body) {
        if(isset($this->data)) {
            foreach($this->data as $key=>$value) {
                if(is_array($value)) {
                    foreach($value as $k=>$v) {
                        $body = str_replace("{\$".$key.".".$k."}",$v,$body);
                    }
                } else {
                    $body = str_replace("{\$".$key."}",$value,$body);
                }
            }
        }
        return $body;
    }

    public function execute() {
        $body = $this->__getTemplate();
        $body = $this->__setassign($body);
        $subject = $this->subject;
        $header = $this->from;
        $header .= $this->cc;
        $header .= $this->bcc;
        $to = $this->to;
        if(mb_send_mail($to,$subject,$body,$header)) {
            return true;
        } else {
            return false;
        }
    }

}
?>
mail.php
<?php
    //call class file
    //クラスファイル呼び出し
    require_once('class.ezmail.php');

    //make instance
    //インスタンス作成
    $mail = new Ezmail();

    //template directory(default: "./templates/" )
    //remember make directory!!!
    //テンプレートディレクトリ(デフォルト: "./templates/")
    //ディレクトリ作成を忘れないでね!
    $mail->setTempdir();

    //set template file
    //テンプレートファイル呼び出し
    $mail->setTempfile("mail_tmp.txt");

    //set subject
    //件名設定
    $mail->setSubject("無題");

    //set from adrress
    //送信元設定
    $mail->setFrom("from@hoge.com");

    //$cc $bcc is array or string
    //文字列でも配列でも送信出来ます
    $cc = array(
        "cc1@hoge.com",
        "cc2@hoge.com"
    );

    $bcc = array(
        "bcc1@hoge.com",
        "bcc2@hoge.com"
    );

    //set cc and bcc(default:null)
    //cc bcc設定(デフォルト:null)
    $mail->setCc($cc);
    $mail->setBcc($bcc);

    //set to adrress
    //送信先設定
    $mail->setTo("to@hoge.com");

    //set variable to template like smarty
    //available array
    //変数設定(スマーティー風)
    //連想配列も使えます
    $mail->assign("body","test");
    $mail->assign("array",array("array"=>"testarray"));


    //templatefile charcode(default:"utf-8")
    //テンプレートファイルの文字コード設定(デフォルト:"utf-8")
    $mail->setChar("utf-8");

    //send mail!!!
    //送信!
    $mail->execute();
?>
mail_tmp.txt
これはテストです。
{$body}
{$array.array}
6
5
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
6
5