SwiftMailer not sending emails in Symfony 2.5

六月ゝ 毕业季﹏ 提交于 2019-11-29 17:08:52

I got it to work on both company based SMTP and Gmail

For company SMTP

config.yml:

swiftmailer:
    transport: %mailer_transport%
    encryption: %mailer_encryption%
    auth_mode:  %mailer_auth_mode%
    host:      %mailer_host%
    username:  %mailer_username%
    password:  %mailer_password%
    spool:     { type: memory }

parameters.yml:

    mailer_transport: smtp
    mailer_host: mail.domain.com
    mailer_user: username@domain.com
    mailer_password: password

For Gmail config.yml settings remain the same but in parameters.yml make the following change

    mailer_transport: gmail
    mailer_host: smtp.gmail.com
    mailer_user: username@gmail.com
    mailer_password: password

Then visit this link of google to allow apps to login into your account to send mails. You can also view any suspecious activity of login using this link

Note:

In your controller where you are telling the function to send email ->setFrom('username@gmail.com') should match the email id which is generating the email otherwise you will not get an email, I over looked this and wasted 2 days on it.

 $message = \Swift_Message::newInstance()
                ->setSubject('Contact enquiry')
                ->setFrom('username@gmail.com') \\match this to mailer_user in parameters.yml
                ->setTo('username@gmail.com')
                ->setBody(
                    $this->renderView('ContactBundle:Default:contactEmail.txt.twig', array('enquiry' => $enquiry))
                );

This is how I got it work :) finally, hopefully this will help someone out there stuck. I can confirm that this solution works for me on both development and production server.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!