2017-09-25 107 views
0

发送电子邮件在Synfony 3在需要帮助学习如何使用与Swift_mailer图书馆Symfony框架在开发模式下发送电子邮件。在开发模式

这里是我迄今为止

在config_dev.yml文件

swiftmailer: 
transport: '%mailer_transport%' 
host: '%mailer_host%' 
username: '%mailer_user%' 
password: '%mailer_password%' 
spool: { type: memory } 

在config.yml文件

swiftmailer: 
transport: '%mailer_transport%' 
host: '%mailer_host%' 
username: '%mailer_user%' 
password: '%mailer_password%' 
spool: { type: memory } 

在我parameters.yml文件

.... 
mailer_transport: gmail 
mailer_host: 127.0.0.1 
mailer_user: 'my_gamil_here' 
mailer_password: 'my_password_here' 

然后在我的骗局troller我有以下

public function indexAction(\Swift_Mailer $mailer) 
{ 



    $message = (new \Swift_Message('Hello Email')) 
    ->setFrom('[email protected]') 
    ->setTo('[email protected]') 
    ->setBody(
     $this->renderView(
      // app/Resources/views/Emails/registration.html.twig 
      'Emails/registration.html.twig', 
      array('name' => 'James') 
     ), 
     'text/html' 
    ); 

    $mailer->send($message); 

    return new Response('Email sent'); 
} 

} 

有人能告诉我,我什么都错了。没有什么会被发送到电子邮件帐户。

回答

0
  1. 确保,即mailer_user设置只有你的Gmail用户名(没有主机部分)。比方说,如果我有电子邮件[email protected],我的用户名是myemail

  2. 您在使用两步验证为您的Gmail帐户?如果是的话,还需要为您的帐户生成的应用程序的密码,如docs规定:

如果您的Gmail帐户使用两步骤验证,您必须generate an App password并把它作为的价值mailer_password参数。你还必须确保你allow less secure apps to access your Gmail account

不久,你可以按照here,登录,选择应用程序(在这种情况下Mail),并要使用邮件用,按Generate的设备,你会看到模式与您的应用程序的密码,与此类似snub udpz xcvt jrdp。复制此密码,并将其用作您的Gmail密码(不含空格)。

如果这没有帮助,还检查日志,一般如果没有与SMTP服务器,swiftmailer打印错误日志的一个问题。

+0

这工作我的朋友。非常感谢你! – Kaley36

0

'mailer_transport' 是发送邮件的protocole。
尝试使用'smtp'代替'gmail':)

1

也许是因为您指定了邮件主机为localhost。检查这个symfony文档http://symfony.com/doc/current/email/gmail.html。并注意这部分:

Gmail的交通很简单,使用SMTP传输 并设置这些选项的快捷方式:

encryption ssl 

auth_mode login 

host smtp.gmail.com