Symfony 2:如何使用 Swiftmailer 发送电子邮件 [英] Symfony 2: how to send an email using Swiftmailer
本文介绍了Symfony 2:如何使用 Swiftmailer 发送电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用 Swiftmailer 和 Symfony 2 发送电子邮件.
I am trying to send emails using Swiftmailer with Symfony 2.
这是控制器中的简单函数
This is the simple function in the controller
public function sendEmailAction() {
$name = 'Test';
$mailer = $this->get('mailer');
$message = $mailer->createMessage()
->setSubject('Ciao')
->setFrom('send@example.com')
->setTo('recipient@example.com')
->setBody($this->renderView('dashboard/email.html.twig',array('name' => $name)), 'text/html');
$mailer->send($message);
return $this->redirectToRoute('dashboard');
在parameters.yml中我有以下配置
In the parameters.yml I have following configuration
parameters:
database_host: 127.0.0.1
database_port: null
database_name: symfony
database_user: root
database_password: null
mailer_transport: smtp
mailer_host: 127.0.0.1
mailer_user: null
mailer_password: null
secret: e23a8d7b075fa3c7e56b10186a24cf2790a3169a
这是config.yml
# Swiftmailer Configuration
swiftmailer:
transport: "%mailer_transport%"
host: "%mailer_host%"
username: "%mailer_user%"
password: "%mailer_password%"
spool: { type: memory }
很遗憾我无法发送电子邮件...
Unfortunately I cannot send emails...
推荐答案
请阅读关于"如何发送电子邮件"
Swift Mailer 提供了多种发送电子邮件的方法,包括使用 SMTP 服务器、使用本地安装的 sendmail,或甚至使用 GMail 帐户.
Swift Mailer provides a number of methods for sending emails, including using an SMTP server, using a local install of sendmail, or even using a GMail account.
邮件传输示例
mailer_transport: mail
mailer_host: 127.0.0.1
mailer_user: null
mailer_password: null
smtp 示例
mailer_transport: smtp
mailer_encryption: ssl
mailer_auth_mode: login
mailer_host: smtp.gmail.com
mailer_user: your@gmail.com
mailer_password: *******
sendmail 示例:阅读这个一个>
mailer_transport: sendmail
mailer_host: /usr/bin/sendmail # wherever your mail is
#mailer_user: ~
#mailer_password: ~
GMail 帐户示例
mailer_transport: gmail
mailer_encryption: ssl
mailer_auth_mode: login
mailer_host: smtp.gmail.com
mailer_user: your@gmail.com
mailer_password: *******
然后像这样使用
$message = \Swift_Message::newInstance()
->setSubject('Hello Email')
->setFrom('from@example.com')
->setTo('to@example.com')
->setBody(
$this->renderView(
'HelloBundle:Hello:email.txt.twig',
array('name' => $name)
)
)
;
$this->get('mailer')->send($message);
这篇关于Symfony 2:如何使用 Swiftmailer 发送电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文