Cannot set Django to work with smtp.gmail.com

16,492

Solution 1

I have recently set this up and had a slightly different settings.py config.

Move:

EMAIL_USE_TLS = True 

to the top above EMAIL_HOST

Add:

DEFAULT_FROM_EMAIL = '[email protected]'
SERVER_EMAIL = '[email protected]'

Solution 2

Change your settings like this :

EMAIL_HOST = 'smtp.gmail.com'

EMAIL_HOST_USER = 'user'

EMAIL_HOST_PASSWORD = 'your-password'

EMAIL_PORT = 587

EMAIL_USE_TLS = True

Then try:

python manage.py shell
>>> from django.core.mail import EmailMessage
>>> email = EmailMessage('Mail Test', 'This is a test', to=['[email protected]'])
>>> email.send()

This should return with the status 1, which means it worked.

Solution 3

I had the same problem, and I searched for half a day to find a solution. Most of the proposed solutions are talking about where you should initialize EMAIL_USE_TLS in relation to the other settings. I don't think this is a solution for the problem.

I found the solution at: https://support.google.com/accounts/answer/185833?hl=en and finally https://security.google.com/settings/security/apppasswords

If you are testing your project on a local machine, you should go to the latter link, and enable "Access for less secure apps".

Share:
16,492
la_f0ka
Author by

la_f0ka

Updated on June 03, 2022

Comments

  • la_f0ka
    la_f0ka almost 2 years

    I've been trying to get django to work with gmail's smtp server to send mails but I always get this traceback. Any help will be most appreciated.

    ----- settings.py -----

    EMAIL_HOST = 'smtp.gmail.com'
    
    EMAIL_HOST_USER = '[email protected]'
    
    EMAIL_HOST_PASSWORD = 'your-password'
    
    EMAIL_PORT = 587
    
    EMAIL_USE_TLS = True
    

    ---- python shell -----

    from django.core.mail import EmailMessage

    email = EmailMessage('Mail Test', 'This is a test', to=['[email protected]'])

    email.send()

    Traceback (most recent call last):
    File "<console>", line 1, in <module>
    File "/home/fiodorovich/Envs/fdict/lib/python2.7/site-packages/django/core/mail/message.py", line 251, in send
    return self.get_connection(fail_silently).send_messages([self])
    File "/home/fiodorovich/Envs/fdict/lib/python2.7/site-packages/django/core/mail/backends/smtp.py", line 86, in send_messages
    sent = self._send(message)
    File "/home/fiodorovich/Envs/fdict/lib/python2.7/site-packages/django/core/mail/backends/smtp.py", line 104, in _send
    email_message.message().as_string())
    File "/usr/local/lib/python2.7/smtplib.py", line 701, in sendmail
    raise SMTPSenderRefused(code, resp, from_addr)
    SMTPSenderRefused: (530, '5.7.0 Must issue a STARTTLS command first. z15sm10449686anl.15', 'webmaster@localhost')
    

    Edit: New errors when made the modification suggested by unni. The shell won't execute and I'm getting this error message

    **EMAIL_HOST_USER  = '[email protected]'**
     ^
    SyntaxError: invalid syntax
    
  • la_f0ka
    la_f0ka over 12 years
    I just added the error message this produces to my original question
  • unni
    unni over 12 years
    @la_f0ka I just wanted to emphasize that line don't use ** in the settings file. I have edited the code above. Please try it again.
  • Sheena
    Sheena over 11 years
    it doesn't matter where EMAIL_USE_TLS is in relation to the other settings. All that stuff is executed in one swell foop
  • Will
    Will almost 9 years
    This is the correct answer! The current accepted answer (to change order of EMAIL_USE_TLS = True) is incorrect.
  • JMS
    JMS almost 8 years
    This should be the approved answer. Thank you
  • Mohammed Shareef C
    Mohammed Shareef C about 6 years
    @Sheena But this answer got 6 upvotes yet. Means there is something in it