Trouble with Django sending email though smtp.gmail.com
Asked Answered
M

8

17

I am trying to configure Django's send_email so I can send password reset emails to users. So far I've had no luck on getting it to work. I've set up a basic Gmail account (no Google App etc) and in my Django settings.py i have:

EMAIL_HOST      = 'smtp.gmail.com'
EMAIL_HOST_PASSWORD = 'my_password'
EMAIL_HOST_USER = '[email protected]'
EMAIL_PORT      = 587
MAIL_USE_TLS   = True

EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
DEFAULT_FROM_EMAIL  = '[email protected]'

Then i try to test this by doing:

python manage.py shell
>>> from django.core.mail import send_mail
>>> send_mail('test', 'test', '[email protected]', ['[email protected]'])

and I get the error message

Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "C:\Program Files\Python27\lib\site-packages\django\core\mail\__init__.py
", line 62, in send_mail
    connection=connection).send()
  File "C:\Program Files\Python27\lib\site-packages\django\core\mail\message.py"
, line 255, in send
    return self.get_connection(fail_silently).send_messages([self])
  File "C:\Program Files\Python27\lib\site-packages\django\core\mail\backends\sm
tp.py", line 88, in send_messages
    new_conn_created = self.open()
  File "C:\Program Files\Python27\lib\site-packages\django\core\mail\backends\sm
tp.py", line 55, in open
    self.connection.login(self.username, self.password)
  File "C:\Program Files\Python27\lib\smtplib.py", line 577, in login
    raise SMTPException("SMTP AUTH extension not supported by server.")
SMTPException: SMTP AUTH extension not supported by server.

Does anyone have an idea what is going on! Any hint is appreciated!

Melisma answered 2/11, 2013 at 20:57 Comment(0)
K
38

You have made a typo in your settings. TLS should be set with EMAIL_USE_TLS not MAIL_USE_TLS. Not using TLS while connecting to 587 generates this error.

Kinescope answered 1/1, 2014 at 23:1 Comment(0)
A
6

Make sure you have also set the EMAIL_BACKEND setting:

EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
Adaadabel answered 2/11, 2013 at 21:4 Comment(0)
H
6

I found the problem... you have to use this code

EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'

instead of

EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
Haggerty answered 29/8, 2020 at 13:3 Comment(0)
U
5

This response is late for the question; but will hopefully help future readers of this thread.

As pointed out by @WAF, EMAIL_USE_TLS = True resovles this error.

Additionally, if the resolution is being validated via the shell command of the django manage tool, we'll have to restart the shell after adding the above setting & repeat the validation steps.

The following summarizes the minimal config for gmail validation from django shell:

In settings.py:

EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_HOST_USER = '<email.id>'
EMAIL_HOST_PASSWORD = '<pw>'
EMAIL_USE_TLS = True

In the shell of the django manage tool:

from django.core.mail import send_mail
send_mail('From django', 'Test email from dj manage', '[email protected]', ['[email protected]'])
... 1
Universalism answered 6/4, 2018 at 8:29 Comment(0)
P
1

Once you fix the error by deleting the mail extension, you may receive other errors. In most cases extension is not the real problem. Once you did complete your setup correctly it works both with extension and without.

EMAIL_HOST_USER = 'my_account'

Visit https://myaccount.google.com/security and check if it works when you turn on Allow less secure apps setting at the bottom.

Pokeweed answered 18/11, 2015 at 12:57 Comment(0)
C
1

You need to set either TLS or SSL to be True

Also, use STARTTLS port which is 587

Like that:

EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_USE_SSL = False

Note: You don't need to include this in your settings:

EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
Corrinacorrine answered 19/12, 2019 at 17:37 Comment(0)
R
0

This is the problem with your SMTP server configuration. It is not configured with TLS. If you are using postfix please refer to the document -
http://postfix.state-of-mind.de/patrick.koetter/smtpauth/postfix_tls_support.html

It would also helpful for you -
How to send an email with Gmail as provider using Python?

Residuum answered 2/11, 2013 at 23:14 Comment(1)
Although the question did specify gmail. you're answer actually helped me for a NONE gmail setupTopper
M
-1

i deleted : EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend' and my settings.py looks like :

    EMAIL_HOST_USER
    EMAIL_HOST_PASSWORD
    EMAIL_HOST
    EMAIL_PORT
    EMAIL_USE_TLS

and its worked !!!!

Marston answered 21/10, 2023 at 10:59 Comment(1)
Won't work everytime, the issue was in EMAIL_USE_TLSCrepuscular

© 2022 - 2024 — McMap. All rights reserved.