[Django]-Django 1.8 sending mail using gmail SMTP

48👍

for me in settings.py:

EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = 'test@gmail.com'
EMAIL_HOST_PASSWORD = 'test'
EMAIL_PORT = 587

and views.py:

from django.core.mail import EmailMessage

email = EmailMessage('title', 'body', to=[email])
email.send()
    

and: https://accounts.google.com/DisplayUnlockCaptcha

and also make sure you turn on permission for less secure apps.

22👍

Remember to:

Go to your Google Account settings, find Security -> Account permissions -> Access for less secure apps, enable this option.

About this option: https://support.google.com/accounts/answer/6010255

👤edilio

12👍

I tested this and worked perfect in django 1.8:
first you should check this link, provided by google which you did 🙂
notice that for some strange reasons that I don’t know,you have to code like this in view.py or shell:

import django
from django.conf import settings
from django.core.mail import send_mail

send_mail('Subject here', 'Here is the message.', settings.EMAIL_HOST_USER,
         ['to@example.com'], fail_silently=False)

also this is my settings in setting.py file:

EMAIL_USE_TLS = True
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_PASSWORD = 'xxxx' #my gmail password
EMAIL_HOST_USER = 'xxxx@gmail.com' #my gmail username
EMAIL_PORT = 587
DEFAULT_FROM_EMAIL = EMAIL_HOST_USER

9👍

replace in your settings.py file :

EMAIL_BACKEND = 'django_smtp_ssl.SSLEmailBackend'

by

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

8👍

In settings.py
change this

EMAIL_HOST='imap.gmail.com'
EMAIL_PORT = 587
EMAIL_HOST_USER = 'yadavabhishek260@gmail.com'
EMAIL_HOST_PASSWORD ='**********'
EMAIL_USE_SSL=False
EMAIL_USE_TLS= True
👤Abhi

1👍

This works for me:

settings.py

EMAIL_BACKEND = 'django_smtp_ssl.SSLEmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_PASSWORD = 'test'
EMAIL_HOST_USER = 'test@gmail.com'
EMAIL_PORT = 587
EMAIL_USE_TLS = True

Unlock Captcha: https://accounts.google.com/DisplayUnlockCaptcha

views.py

email = EmailMessage(
    'subject_message',
    'content_message',
    'sender smtp gmail' +'<sender@gmail.com>',
    ['receiver@gmail.com'],
    headers = {'Reply-To': 'contact_email@gmail.com' }
)
email.send()

1👍

I used this for django 1.11

In settings.py

EMAIL_USE_TLS = True
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_PASSWORD = 'sender' #sender mail password
EMAIL_HOST_USER = 'sender@mail.com' #sender mail username
EMAIL_PORT = 587
DEFAULT_FROM_EMAIL = EMAIL_HOST_USER

In view.py

send_mail('mail subject', 'body content',settings.EMAIL_HOST_USER,
                      ['receiver@mail.com'], fail_silently=False)

and goto https://myaccount.google.com/u/0/security?hl=en to enable Less secure app access

Leave a comment