Django - Gmail - SMTPAuthenticationError

December 10, 2013 19k views
I have a Django project, in which I'm attempting to send an email using my gmail account. Note this works fine on my dev machine. Code : from django.core.mail import send_mail ... send_mail(subject, message, settings.EMAIL_HOST_USER, [settings.EMAIL_HOST_USER]) Settings : EMAIL_HOST = 'smtp.gmail.com' EMAIL_HOST_USER = 'xxxxx@gmail.com' EMAIL_HOST_PASSWORD = 'xxxxxxxxxxxxx' EMAIL_PORT = 587 # Tried 465 as mentioned in; # https://www.digitalocean.com/community/articles/how-to-use-google-s-smtp-server EMAIL_USE_TLS = True Anyone know why I'm getting the exception, when running the code in a VPS. Note : running Gunicorn & Nginx Any help or advice appreciated.
6 Answers
Going to https://accounts.google.com/DisplayUnlockCaptcha
and then retrying the operation, worked a treat!

Thank You Pablo.
What do the error logs show? Do you have a firewall in place, by chance?
No firewall, currently; just a minimal install to try things out.

Internal Server Error: /contact/
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 114, in get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/views/generic/base.py", line 69, in view
return self.dispatch(request, *args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/views/generic/base.py", line 87, in dispatch
return handler(request, *args, **kwargs)
File "/opt/officeowlsproject/officeowls/source/contact/views.py", line 39, in post
File "/opt/officeowlsproject/officeowls/source/contact/views.py", line 49, in send_notification_mail
send_mail(subject, message, settings.EMAIL_HOST_USER, [settings.EMAIL_HOST_USER])
File "/usr/local/lib/python2.7/dist-packages/django/core/mail/__init__.py", line 50, in send_mail
File "/usr/local/lib/python2.7/dist-packages/django/core/mail/message.py", line 255, in send
return self.get_connection(fail_silently).send_messages([self])
File "/usr/local/lib/python2.7/dist-packages/django/core/mail/backends/smtp.py", line 88, in send_messages
new_conn_created = self.open()
File "/usr/local/lib/python2.7/dist-packages/django/core/mail/backends/smtp.py", line 55, in open
self.connection.login(self.username, self.password)
File "/usr/lib/python2.7/smtplib.py", line 613, in login
raise SMTPAuthenticationError(code, resp)
SMTPAuthenticationError: (534, '5.7.14 Please log in via your web browser and then try again.\n5.7.14 Learn more at https://support.google.com/mail/bin/answer.py?answer=787\n5.7.14 54 c1sm9978624qad.1 - gsmtp')
I was having a similar problem and i solve it changing the order of the settings.
It worked exactly like this

EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = "xxxx@gmail.com"

You can try it easy with the django shell:

>>> from django.core.mail import send_mail
>>> send_mail('test email', 'hello world', 'xxxx@gmail.com', ['xxx@yyyy.com'])

hope it helps
  • I tried everything in the books to get my email to work. Your solution ended up working for me! At first, I had USE_TLS at the bottom. Moving it to the top, like yours, fixed it.

It is because Google blocks sign-in attempts from apps which do not use modern security standards (mentioned on their support page). You can however, turn on/off this safety feature by going to the link below:

Go to this link and select Turn On


Moreover google block an ip when you try to send a email since a unusual location, so, you can unblock in the next link


and clicked in


More info...smtp mail

Have another answer? Share your knowledge.