Symfony 2 : Swiftmailer exception
-
27-10-2019 - |
سؤال
I've been working on the Symfony 2 emails sending using Swiftmailer.
Here is my swiftmailer configuration in app/config/config.yml
swiftmailer:
transport: gmail
username: myusername@gmail.com
host: smtp.gmail.com
password: mypassword
I have also configured my xampp localhost to send emails.
I have a form where user enters email addresses (max 5 emails) , Subject and Message. For first few tests on submitting the form the emails where sent. I also actually got emails. But after that i've been getting this error below repeatedly.
Failed to authenticate on SMTP server with username "myusername@gmail.com" using 1 possible authenticators 500 Internal Server Error - Swift_TransportException
What is the problem over here? I also tried stopping my xampp and then re-starting it, but still the problem persists. Does any one know whats the problem here?
Thank You.
المحلول
You're facing this issue because you haven't properly setup your mailing host. First off, I strongly recommend that you use a service such as SendGrid and configure it as detailed in this Symfony2 Mailing Setup Tutorial:
mailer_transport: smtp
mailer_host: smtp.sendgrid.net
mailer_user: your_sendgrid_username
mailer_password: 'your_sendgrid_password'
mailer_port: 587
The tutorial also contains details on how to properly setup a mailing service in Symfony2 and how to build mailing templates.
But you if you still want to use Gmail, then the correct configuration is:
mailer_transport: smtp
mailer_host: smtp.gmail.com
mailer_user: your_gmail_address@gmail.com
mailer_password: 'your_gmail_password'
mailer_port: 587
نصائح أخرى
As far as I remember host
value is ignored, when transport
is set to gmail
. Regarding your issue, make sure that you are using latest version of SwiftMailer (v4.1.5), because there it has been fixed. From changelog:
- prevented stream_socket_client() from generating an error and throw a Swift_TransportException instead
In general, if authentication fails, an Exception of type Swift_TransportException
will
be thrown.