Symfony2 SwiftMailer Connection could not be established with host smtp.gmail.com

坚强是说给别人听的谎言 提交于 2020-01-02 12:23:18

问题


After reading an testing a lot of solutions, I still getting this error.

My parameters.yml

parameters:
    database_host: 127.0.0.1
    database_port: null
    database_name: symfony
    database_user: root
    database_password: null
    secret: holamundo
    mailer_transport: smtp
    mailer_encryption: ssl
    mailer_port: 465
    mailer_auth_mode: login
    mailer_host: smtp.gmail.com
    mailer_user: justexample@gmail.com
    mailer_password: nottherealone

And my config.yml

# Swiftmailer Configuration
swiftmailer:
    transport: %mailer_transport%
    encryption: %mailer_encryption%
    auth_mode: %mailer_auth_mode%
    host: %mailer_host%
    port: %mailer_port%
    username: %mailer_user%
    password: %mailer_password%
    spool: { type: memory }

And testing swiftmailer trough command line...

php bin/console swiftmailer:email:send

I get this error:

[app] Exception occurred while flushing email queue: Connection could not be established with host smtp.gmail.com [ #0] [] []

If I ping 'smtp.gmail.com' I get a response back.

I'm running symfony on localhost:8000, many people say to change mailer_host to something like...

mailer_host: 127.0.0.1

But I'm running it on a different port, so if I put this

mailer_host: 127.0.0.1:8000 

or even this

mailer_host: 127.0.0.1
mailer_port: 8000

I get:

ERROR     [app] Exception occurred while flushing email queue: Expected response code 220 but got code "", with message "" [] []

Thanks !


回答1:


If you want to use gmail for testing purpose your parameter.yml file should look like this

mailer_transport: gmail
mailer_host: null
mailer_user: email_address
mailer_password: 'password'

To use it in your Controller

    $messenger = \Swift_Message::newInstance();
    $messenger->setFrom(array("email_address" => "The name you want to be displayed "));

   $messenger->setTo(array($email1, $email2, ...));
   $messenger->setCc(array($email1, $email2, ...));      
   $messenger->setSubject($subject);
   $messenger->setBody($body, 'text/html');      
   $this->mailer->send($messenger);

For more information about sending email in symfony have a look here https://symfony.com/doc/current/email.html




回答2:


Try deactivating your antivirus. Avast was blocking my send email action and Symfony 3 gave me that same error message!! hope it helps



来源:https://stackoverflow.com/questions/48842066/symfony2-swiftmailer-connection-could-not-be-established-with-host-smtp-gmail-co

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