问题
- I got exception:
Connection could not be established with host smtp.gmail.com
- Same exception in old project Symfony2.8 and newest Symfony3.
- I can ping
smtp.gmail.com
.
回答1:
The Avast Antivirus was guilty.
回答2:
In Google "Account settings" enable "Access for less secured apps" by setting it to "Allow".
Mailer host is your localhost or 127.0.0.1. It's not smtp.google.com
回答3:
I'm using this configuration for my localhost application
parameters:
mailer_transport: gmail
mailer_host: smtp.gmail.com
mailer_user: youremail@gmail.com //replace by your gmail account
mailer_password: ********** //replace by your gmail password
config.yml:
swiftmailer:
transport: "%mailer_transport%"
host: "%mailer_host%"
username: "%mailer_user%"
password: "%mailer_password%"
spool: { type: memory }
your controller code seems to be the same as mine.
As napestershine said in his response do not forget to allow less secure app on your gmail account.
来源:https://stackoverflow.com/questions/40058392/connection-could-not-be-established-with-host-smtp-gmail-com-symfony-swiftma