Email body in Symfony 1.4 mailer?

女生的网名这么多〃 提交于 2019-11-29 16:04:46

问题


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.


回答1:


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.




回答2:


How about just using the native method availible inside sfAction.

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



来源:https://stackoverflow.com/questions/2255109/email-body-in-symfony-1-4-mailer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!