Render action to HTML email in Zend Framework

html-email, zend-framework

Solution

From your controller:

// do this if you're not using the default layout
$this->_helper->layout()->disableLayout();

$this->view->data = $items;

$htmlString = $this->view->render('foo/bar.phtml');

If you're doing this from a class that's not an instance of Zend_Controller_Action, you may have to create an instance of a Zend_view first, to do this:

$view = new Zend_view();

// you have to explicitly define the path to the template you're using
$view->setScriptPath(array($pathToTemplate)); 

$view->data = $data;

$htmlString = $view->render('foo/bar.phtml');

Problem

I have an action which is rendering some content via a layout. I actually want to send this output in an email. What is the best way to achieve this in the Zend Framework? I know I need to use the `Zend_Mail` component to send the email, but I'm unclear about how to attach the output of my action to `Zend_Mail`. I've done some reading on the ContextSwitch action helper, and I think that might be appropriate, but I'm still not convinced. I'm still new to Zend Framework. I'm used to using techniques like output buffering to capture output, which I don't think is the correct way to do this in Zend.

Original source