php - 交响乐 2 : how to send an email using Swiftmailer

标签 php symfony

我正在尝试使用带有 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/

相关文章:

没有 DB 生成框架的 PHP MySQL 页面生成

php - 将所有符号转换为 html 实体

css - 如何在 Symfony 中将 choiceType 值显示为颜色

php - 在 Twig 中获取表单名称

php - Symfony2 Twig 日期 ("H") "Military Time"

javascript - jQuery 仅从循环中获取第一个 id 值

php - MySQL 或 PHP 中的函数

php - 如何对构造函数接受一些参数的类的方法进行单元测试?

php - 可捕获的 fatal error : Object of class Proxies\__CG__\AppBundle\Entity\Ticket could not be converted to string

symfony - 使用原则 2 中的多态关系子集的多态附件