[CodeIgniter3]メール

parserライブラリを使うとコントローラーで生成された値をテンプレートに埋め込んでくれる。

コントローラー

application/controllers/Mail.php

	/**
	 * 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

件名
--------------------------------------------------
メールヘッダ
--------------------------------------------------

{name} 様

{order}

ありがとうございました。

==================================================
メールフッタ
==================================================