parserライブラリを使うとコントローラーで生成された値をテンプレートに埋め込んでくれる。
コントローラー
application/controllers/Mail.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
/** * Mail Send Test * @return [type] [description] */ public function sendMail() { $this->load->library('parser'); $data = []; $data['name'] = 'おなまえ'; // {name}に設定 $data['order'] = ' // {order}に設定 ----- 商品:ほにゃらら 価格:11,111円 数量:1 ----- '; $template = $this->parser->parse('template/mail/order.php', $data, TRUE); $lines = explode("\n", $template); $subject = array_shift($lines); // テンプレートの1行目をSubjectに使用 $message = implode("\n", $lines); $this->load->library('email'); $this->email->from('from@aaa.com', '差出人'); $this->email->to('to@bbb.com'); $this->email->subject($subject); $this->email->message($message); $this->email->send(); } |
メールテンプレート
application/views/template/mail/order.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
件名 -------------------------------------------------- メールヘッダ -------------------------------------------------- {name} 様 {order} ありがとうございました。 ================================================== メールフッタ ================================================== |