Render view as email

I am relatively new to Yii and I am sure that I don’t understand all concepts good enough, so I am sorry, if the question is a dumb one.

I was wondering, could I use view to render email message, which to send using the PHP mail function? I want to use the power of Yii in the email body, but I don’t want to store the template in the model/controller. So, how can I do this?

That’s exactly how i do it in my mailer component, so: yes, it’s surely possible. You can use Yii::app()->controller to get the current controller. It provides the two useful methods resolveViewFile() and renderFile() which you can use to render your email templates with a set of parameters.

Thanks, Mike! I think that this is exactly what I was looking for.

Use this SMTP mailer

Body = $this->renderPartial(‘email_template’, array(‘model’ => $model), true);

				Yii::app()->mailer->Host = Yii::app()->params['smtpHost'];

				Yii::app()->mailer->Username = Yii::app()->params['smtpUsername'];

				Yii::app()->mailer->Passowrd = Yii::app()->params['smtpPassword'];



				Yii::app()->mailer->CharSet = 'UTF-8';

				Yii::app()->mailer->From = Yii::app()->params['smtpEmail'];

				Yii::app()->mailer->FromName = Yii::app()->name;

				Yii::app()->mailer->Sender = Yii::app()->params['smtpEmail'];



				Yii::app()->mailer->Subject = 'Something has been added.';

				Yii::app()->mailer->Body = $this->renderPartial('email_template', array('model' => $model), true);


Check This Article written by me … :)

please see it this extension