Give HTML Email Action in Zend Framework

I have an action that passes some content through the layout.

I really want to send this output in an email. What is the best way to achieve this in the Zend Framework?

I know that I need to use the Zend_Mail component to send email, but I don’t understand how to attach the output of my action to Zend_Mail .

I read something in the ContextSwitch action helper, and I think this might be appropriate, but I'm still not sure.

I'm still new to the Zend Framework. I'm used to using methods such as output buffering to capture output, which I don't think is the right way to do this in Zend.

+4
source share
4 answers

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 are doing this from a class that is not an instance of Zend_Controller_Action, you may need to create an instance of 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'); 
+8
source

when you submit an action, you can catch the event in the postDispatch () method of the plugin, which you can dynamically add to the stack from the desired action. In doing so, you get the contents of the response

 //in action //...some php code Zend_Controller_Front::getInstance()->registerPlugin(new My_Plugin()); //in plugin $htmlCode = $this->_response->getBody(); 
+1
source
 public static function sendMail($data = array(), $template = ''){ $html = new Zend_View(); $html->setScriptPath(APPLICATION_PATH . '/modules/default/views'); // assign valeues if(count($data['Assigni'])){ foreach($data['Assigni'] as $assign){ $html->assign($assign['key'], $assign['value']); } } // create mail object $mail = new Zend_Mail('utf-8'); // render view //'scripts/newsletter/emailtemplate.phtml' $bodyText = $html->render($template); $mail->addTo($data['To']); $mail->setSubject($data['Subject']); $mail->setFrom($data['From'], $data['FromName']); $mail->setBodyHtml($bodyText); $mail->send(); } 
+1
source

I cannot give you a detailed answer, but if you want to get the full output (including the layout), I think you want to write your email function as an action helper and insert it into the PostDispatch hook of the Zend_Controller_Action-> dispatch () loop.

See http://nethands.de/download/zenddispatch_en.pdf for a complete overview of the Zend Framework submission process.

If you don’t need the layout included in your email content, you can do this in many cases, including using the context switch, as you mentioned.

0
source

All Articles