views:

772

answers:

2

I am trying to send mail using mail templates. To do this I want to load a .tpl into a variable. Instead of loading an HTML file and substituting placeholders, I wonder if it is possible to set values of the view in the controller, and then load this view into a variable. This way I would have a variable containing the HTML mail filled out with the information set in the controller prior to loading the view.

Any alternatives are also welcome, I mean, if there are already ways of doing mail templating in a more standardized way.

A: 

Sorry guys (and girls). My google skills must have noticed it was early in the morning. I found what I was looking for here: http://myzendframeworkexperience.blogspot.com/2008/09/sending-mails-with-templates.html

Erik
A: 

A great idea Erik, and I've done this many times.

Zend_View is really just a templating system, and can be used to generate anything, not just HTML.

Sample code - create a view, assign some data, render the view and send the mail!

$view = $this->getHelper('ViewRenderer')->view;

$view->email = $data['email'];    
$view->password = $data['password'];

$text = $view->render('mail/new-user.php');
$mail = new Zend_Mail();

$mail->addTo($data['email'], $data['forename'] . ' ' . $data['lastname']);
$mail->setSubject('Account Details');
$mail->setBodyText($text, 'utf-8');

$mail->send();

In the first line I retrieve the ViewRenderer's view so I have access to the normal script paths. You can create a new Zend_View object, but you'll need to add the path to your view scripts manually.

In my example text based content is generated, but you could generate HTML all the same.

David Caunt