我正在尝试使用带有 Symfony 2 的 Swiftmailer 发送电子邮件。
这是 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 我有以下配置
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 }
很遗憾,我无法发送电子邮件...
最佳答案
请阅读有关 "How to send an Email" 的文档
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 示例:阅读 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);
关于php - 交响乐 2 : how to send an email using Swiftmailer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31471559/