从html& amp;发送电子邮件的最佳做法是什么? CSS?我的项目中有很多邮件。需要解决方案,这可以让我不再写下所有下面的代码&再次:
$msg = \Swift_Message::newInstance()
->setSubject('Test')
->setFrom('noreply@example.com')
->setTo('user@example.com')
->setBody($this->renderView('MyBundle:Default:email1.text.twig'));
$this->get('mailer')->send($msg);
答案 0 :(得分:7)
也许我的回答可以提供帮助。有一个特殊的捆绑Symfony2-MailerBundle可以从模板中呈现电子邮件正文,并允许在配置文件和设置中设置发送参数。你不必每次想要建造和传递它们时都要通过它们。发送电子邮件。
答案 1 :(得分:4)
将该代码设置为服务中的函数。功能就是为了这个。
要创建服务,请参阅以下链接。
How to inject $_SERVER variable into a Service
注意:不要忘记将邮件服务作为参数注入!
arguments: ["@mailer"]
设置服务后;
public function sendMail($data)
{
$msg = \Swift_Message::newInstance()
->setSubject($data['subject'])
->setFrom($data['from'])
->setTo($data['to'])
->setBody($this->renderView($data['view']));
$this->mailer->send($msg);
}
你可以像打电话一样打电话给你;
// this code below is just for setting the data
$data = [
'subject' => 'Hello World!',
'from' => 'from@address.com',
'to' => 'to@address.com',
'template' => 'blabla.html.twig'
];
// this code below is the actual code that will save you
$mailer = $this->get('my_mailer_service');
$mailer->sendMail($data);