views:

676

answers:

3

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.

A: 

I can't give you a super-detailed answer, but if you want the full output (including the layout), I think you want to write your email function as an Action helper, and insert it at the PostDispatch hook of the Zend_Controller_Action->dispatch() loop.

See http://nethands.de/download/zenddispatch%5Fen.pdf for the full Zend Framework Dispatch Process Overview.

If you don't need the layout included in your email content, then you could do this at many points, including by the use of a context switch, as you mentioned.

gravelpot
Thanks, you've given me something to think about. I probably don't need the layout actually, just the rendered view. So the context switch may not be a bad idea then. What are some of the other points you mentioned that I could intercept this content at?
asgeo1
I think that the most proper way to do it would still be to write it as an action helper (or at least as it's own non-action method in your controller class), and execute it from within the controller action.In that case, I don't think the context switch would even be necessary.
gravelpot
A: 

when you dispatch the action, you can catch the event in postDispatch() method of plugin, that you can dynamically add to the stack from desired action. In that you recieve the contents of response by

//in action
//...some php code
Zend_Controller_Front::getInstance()->registerPlugin(new My_Plugin());
//in plugin
$htmlCode = $this->_response->getBody();
Tomáš Fejfar
+3  A: 

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');
Mark Basmayor
This is how I did it when trying to send an email. You call a view get the rendering result and then email it. Beware, do you know that most email agents display results differently than browsers...
dimitris mistriotis
I do the same and it works well. You can use the current view if it's useful to share data, and if not, create a new one as per the second example. Generally you can also use strip_tags to generate a decent text based version too.
David Caunt