views:

1345

answers:

4

I want to make email templates in Zend Framework.

For example,

<html>
<body>
Dear {$username$}, <br>
This is a invitation email sent by your {$friend$}.<br>
Regards,<br>
Admin
</body>
</html>

I want to make this file, get it in Zend framework, set those parameters (username, friend) and then send the email.

How can I do that? Does Zend support this?

+17  A: 

Hi this is realy common.

Create an view script like : /views/emails/template.phtml

<body>
<?php echo $this->name; ?>
<h1>Welcome</h1>
<?php echo $this->mysite; ?>
</body>

and when creating the email :

// create view object
$html = new Zend_View();
$html->setScriptPath(APPLICATION_PATH . '/modules/default/views/emails/');

// assign valeues
$html->assign('name', 'John Doe');
$html->assign('site', 'limespace.de');

// create mail object
$mail = new Zend_Mail('utf-8');

// render view
$bodyText = $html->render('template.phtml');

// configure base stuff
$mail->addTo('[email protected]');
$mail->setSubject('Welcome to Limespace.de');
$mail->setFrom('[email protected]','Limespace');
$mail->setBodyHtml($bodyText);
$mail->send();
ArneRie
It is worth noting that if you're in a controller action, and you haven't deviated too far from the default MVC architechture, you can simply utilize the existing view instance, rather than creating a new one (if you're not worried about variable scoping). `$bodyText = $this->view->render('template.phtml')` will suffice in most situations.
jason
A: 

Very good your post ! helped me a lot...

Adriano
A: 

Thanks! Very helpful.

Chris
A: 

Thanks, this post is so helpful.

Niko