views:

681

answers:

2

Hi,

I'm using the Symfony 1.4 mailer where I build the various bits needed for an email and then send it out using:

$this->getMailer()->composeAndSend($sender, $recipient, $subject, $body);

In the email body, I need to able to take advantage of variables generated in the action, so right now I might have this in my action:

$body = 'Your username is '.$username.' and this is the email body.';

Does anyone know of an elegant way of storing/organising various email bodies, instead of having to code them like this straight into my action? I will have many email templates and will also have them in multiple languages.

I've found an old Askeet tutorial discussing this but it seems somewhat out of date with the new symfony 1.4 integration of SwiftMailer, and SwiftMailer documentation itself isn't very clear on this.

Thank you.

+4  A: 

I store the email bodies as a template file and render them via sfPartialView. E.g. inside an action:

$view = new sfPartialView($this->getContext(), $this->getModuleName(), $this->getActionName(), 'confirmation_mail');
$view->setTemplate('_confirmation_mail.php');

// values can be set e.g. by setAttibute
$view->setAttribute('name', $name);

$body = $view->render()

The body templates are located in the module's template folder, but I am sure you can somehow change this and e.g. put all email templates into one folder if you want to.

Felix Kling
That's perfect, thanks, exactly what I need. And yes, I think you can just use $view->setTemplate('../../emails/email') with a relative path to store them all in one place.
Tom
+1  A: 

How about just using the native method availible inside sfAction.

$this->getPartial('partial_name'); which works like the partial helpers for you templates.

Henrik Bjørnskov
@Henrik: Finally got around to testing some different methods. This answer is indeed the simpler way, allows variables to be passed, and also allow email bodies to be stored in a single location: $body = $this->getPartial('emails/someEmailPartial', array('var1' => $var1, 'var2' => $var2));
Tom