Handling Email with a Widget

I am just curious about everyone’s thoughts.

I am treating emails as a view inside a widget. To me it made sense to "render" the emails through the widget. then I can create my view files and using the output buffer and str_replace customize the email templates aka views.


do emails belong in a widget class?

I do this:

	public function setHtml($body) {

    	if ($this->view !== null) {

        	if (!is_array($body))

            	$body = array('body' => $body);

        	// create a dummy controller to render the view (needed in the console app)

        	$controller = new CController('YiiMail');

        	// renderPartial won't work with CConsoleApplication, so use 

        	// renderInternal - this requires that we use an actual path to the 

        	// view rather than the usual alias

        	$viewPath = Yii::getPathOfAlias(Yii::app()->mail->viewPath . '.' . $this->view) . '.php';

        	$body = $controller->renderInternal($viewPath, array_merge($body, array('mail' => $this)), true);


    	return $this->message->addPart($body, 'text/html');


Slightly modified version of Yii-mail.

It also works from the console. :)