我正在尝试使用Swiftmailer和Symfony 2发送电子邮件。
这是控制器中的简单功能
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中,我有以下配置
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 }
不幸的是我无法发送电子邮件......
答案 0 :(得分:3)
Swift Mailer提供了许多发送电子邮件的方法, 包括使用SMTP服务器,使用本地安装的sendmail,或 甚至使用GMail帐户。
邮件传输示例
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示例:阅读this
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);