電子メールは、配信メカニズムが異なるだけで、他の種類のビューと同じです。Yii が期待するテンプレートの場所は次のとおりです。
yii/
-- protected/
-- views/
-- mail/
-- template.html
メール用に Yii 内からテンプレートを指定できます。のドキュメントを参照してくださいYiiMailMessage->setBody
。
/**
* Set the body of this entity, either as a string, or array of view
* variables if a view is set, or as an instance of
* {@link Swift_OutputByteStream}.
*
* @param mixed the body of the message. If a $this->view is set and this
* is a string, this is passed to the view as $body. If $this->view is set
* and this is an array, the array values are passed to the view like in the
* controller render() method
* @param string content type optional. For html, set to 'html/text'
* @param string charset optional
*/
例:
$message = new YiiMailMessage;
$message->view = 'main_tpl';
$message->setBody(array(
'data' => $data,
'user' => $user,
));
$message->subject = $subject;
$message->addTo($email);
$message->from = $from;
Yii::app()->mail->send($message);
これにより、テンプレートを使用してメッセージが準備され、不足している部分を埋めるためyii/protected/views/mail/main_tpl.php
に一緒に送信され$data
ます。$user