我一直在尝试使用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操作系统
尝试从config_dev.php中删除
encryption: ssl
auth_mode: login
如果使用transport:gmail,则无需指定encryption和auth_mode请参阅symfony2.3食谱如何使用Gmail发送电子邮件
快速而肮脏的方式来测试电子邮件选项。初始化一个新的symfony 2.3项目,并在acme/DemoBundle中的默认控制器中执行此操作。
/**
* @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 %}
邮件程序模板。电子邮件.txt.twig
This is a test email to {{name }}
转到/邮件应该发送电子邮件。