Symfony 1.4邮件中的电子邮件正文?

时间:2010-02-12 21:06:56

标签: email symfony1 swiftmailer

我正在使用Symfony 1.4邮件程序,我在其中构建了电子邮件所需的各种位,然后使用以下方式将其发送出去:

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

在电子邮件正文中,我需要能够利用操作中生成的变量,所以现在我可能会在我的操作中执行此操作:

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

有没有人知道存储/组织各种电子邮件正文的优雅方式,而不是像这样直接对我的行为进行编码?我将拥有许多电子邮件模板,并且还将提供多种语言版本。

我发现一个旧的Askeet教程正在讨论这个问题但是它似乎与SwiftMailer的新symfony 1.4集成有些过时了,SwiftMailer文档本身对此并不十分清楚。

谢谢。

2 个答案:

答案 0 :(得分:5)

我将电子邮件正文存储为模板文件,并通过sfPartialView呈现它们。例如。在行动中:

$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()

正文模板位于模块的模板文件夹中,但我相信您可以以某种方式更改此内容,例如如果您愿意,请将所有电子邮件模板放入一个文件夹中。

答案 1 :(得分:5)

如何在sfAction中使用本机方法。

$这 - > getPartial( 'partial_name');它的作用类似于模板的部分助手。