symfony2无法通过Gmail发送电子邮件

时间:2013-07-25 17:57:52

标签: php gmail swiftmailer symfony-2.2

我一直在尝试使用swiftmailer发送电子邮件,但是我无法这样做,我正在跟踪异常

request.CRITICAL: Uncaught PHP Exception Swift_TransportException: "Failed to authenticate on SMTP server with username "myusername@gmail.com" using 1 possible authenticators" at /home/ubuntu/Symfony/vendor/swiftmailer/swiftmailer/lib/classes/Swift/Transport/Esmtp/AuthHandler.php line 184 [] []

有谁可以告诉我在这里缺少什么?看起来认证由于某种原因失败了。

这是我的配置文件中的代码段:

swiftmailer:    

  transport:  gmail
  encryption: ssl
  auth_mode:  login
  host:       smtp.gmail.com
  username:   myusername@gmail.com
  password:   password

我的开发服务器有ubuntu v13操作系统

1 个答案:

答案 0 :(得分:1)

尝试从config_dev.php中删除

encryption: ssl
auth_mode:  login

无需指定加密:和auth_mode:如果您使用的是传输:gmail 请参阅symfony2.3 cookbook How to use Gmail to send Emails

快速而肮脏的方式来测试电子邮件选项。 初始化一个 new symfony 2.3项目,并在acme / DemoBundle的Default控制器中执行此操作。

/**
 * @Route("/mail")
 * @Template()
 */
public function mailAction() {


    $message = \Swift_Message::newInstance()
            ->setSubject('Hello Email')
            ->setFrom('send@example.com')
            ->setTo('mymail@example.com')
            ->setBody(
            $this->renderView(
                    'DemoBundle:Default:email.txt.twig', array('name' => 'User1')
            )
            )
    ;
    $this->get('mailer')->send($message);

    return array('name' => 'whatever');
}

Acme / Demo / Bundle / Resources / views / Default中的Twig模板 前端模板

mail.html.twig

{% extends '::base.html.twig' %}
  {% block body %}
     This is mail template 
  {% endblock %}

邮件模板。 email.txt.twig

This is a test email to {{name }}

转到/ mail应发送电子邮件。

相关问题