加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – Symfony 2:如何使用Swiftmailer发送电子邮件

发布时间:2020-12-13 16:57:26 所属栏目:PHP教程 来源:网络整理
导读:我正在尝试使用 Swiftmailer与Symfony 2发送电子邮件. 这是控制器中的简单功能 public function sendEmailAction() { $name = 'Test'; $mailer = $this-get('mailer'); $message = $mailer-createMessage() -setSubject('Ciao') -setFrom('send@example.com'
我正在尝试使用 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 }

不幸的是我无法发送电子邮件……

解决方法

请阅读有关 “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);

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读