简体   繁体   中英

Django 1.8 sending mail using gmail SMTP

I was trying send a mail using smtp.gmail.com in django 1.8

My settings.py contains:

EMAIL_BACKEND = 'django_smtp_ssl.SSLEmailBackend'  
EMAIL_HOST='smtp.gmail.com'  
EMAIL_PORT=465  
EMAIL_HOST_USER = 'sarath4coding'  
EMAIL_HOST_PASSWORD = '*********'  
DEFAULT_EMAIL_FROM = 'sarath4coding@gmail.com'
from django.core import mail
mail.send_mail('subject','message','sarath4coding@gmail.com',['sarath4coding@gmail.com'])

But got this error:

Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/home/manager/dj1.8/local/lib/python2.7/site-packages/django/core/mail/__init__.py", line 62, in send_mail
    return mail.send()
  File "/home/manager/dj1.8/local/lib/python2.7/site-packages/django/core/mail/message.py", line 303, in send
    return self.get_connection(fail_silently).send_messages([self])
  File "/home/manager/dj1.8/local/lib/python2.7/site-packages/django/core/mail/backends/smtp.py", line 100, in send_messages
    new_conn_created = self.open()
  File "/home/manager/dj1.8/local/lib/python2.7/site-packages/django_smtp_ssl.py", line 14, in open
    self.connection.login(self.username, self.password)
  File "/usr/lib/python2.7/smtplib.py", line 622, in login
    raise SMTPAuthenticationError(code, resp)
SMTPAuthenticationError: (534, '5.7.14 <https://accounts.google.com/ContinueSignIn?sarp=1&scc=1&plt=AKgnsbuze\n5.7.14 2FDKQt2Dlo2vqFIvbr6DnBItwWvh9DChPwbeTZO66N91gzmiA437Vqs80cZ9-8u13vxq5a\n5.7.14 bVahzO_BQcZ53yKbJ-YbAlmFE1XIK7MfH97O0wI1lvzpTG_WAHuTIBF0HD1GA2icUoUemt\n5.7.14 ErZn4qb942aAIMG103FnrzLp4txXTbXC-wGLpaz5yvnUN5thahvv3-RiIVW8F1QddZKZlg\n5.7.14 qQKpqWw56zr1AcO2s_oaBEt556fQ> Please log in via your web browser and\n5.7.14 then try again.\n5.7.14  Learn more at\n5.7.14  https://support.google.com/mail/answer/78754 kx14sm6579665pab.0 - gsmtp')

I tried everything the document says and followed many suggested solutions.

like https://accounts.google.com/DisplayUnlockCaptcha , enabling low security apps etc.

but I still got errors

Can anybody tell how to properly configure Django 1.8 to send mail using Gmail.

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.

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

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

replace in your settings.py file :

EMAIL_BACKEND = 'django_smtp_ssl.SSLEmailBackend'

by

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

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

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()

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM