Django 1.8使用gmail SMTP发送邮件

时间:2021-08-31 08:30:18

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

我正在尝试使用django 1.8中的smtp.gmail.com发送邮件

My settings.py contains:

我的settings.py包含:

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'

EMAIL_BACKEND ='django_smtp_ssl.SSLEmailBackend'EMAIL_HOST ='smtp.gmail.com'EMAMA_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.

例如https://accounts.google.com/DisplayUnlockCaptcha,启用低安全性应用等。

but I still got errors

但我仍然有错误

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

任何人都可以告诉,如何正确配置Django 1.8使用Gmail发送邮件。

6 个解决方案

#1


32  

for me in settings.py:

在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:

和views.py:

from django.core.mail import EmailMessage

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

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

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

#2


16  

Remember to:

记得:

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

转到您的Google帐户设置,找到安全性 - >帐户权限 - >访问安全性较低的应用,启用此选项。

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

关于此选项:https://support.google.com/accounts/answer/6010255

#3


9  

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:

我测试了这个并且在django 1.8中工作得很完美:首先你应该检查这个链接,谷歌提供你做的:)注意,由于一些我不知道的奇怪的原因,你必须在view.py或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:

这也是我在setting.py文件中的设置:

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

#4


5  

replace in your settings.py file :

在settings.py文件中替换:

EMAIL_BACKEND = 'django_smtp_ssl.SSLEmailBackend'

by

通过

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

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

#5


2  

In settings.py change this

在settings.py中更改此设置

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

#6


1  

This works for me:

这对我有用:

settings.py

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

解锁验证码:https://accounts.google.com/DisplayUnlockCaptcha

views.py

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


32  

for me in settings.py:

在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:

和views.py:

from django.core.mail import EmailMessage

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

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

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

#2


16  

Remember to:

记得:

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

转到您的Google帐户设置,找到安全性 - >帐户权限 - >访问安全性较低的应用,启用此选项。

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

关于此选项:https://support.google.com/accounts/answer/6010255

#3


9  

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:

我测试了这个并且在django 1.8中工作得很完美:首先你应该检查这个链接,谷歌提供你做的:)注意,由于一些我不知道的奇怪的原因,你必须在view.py或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:

这也是我在setting.py文件中的设置:

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

#4


5  

replace in your settings.py file :

在settings.py文件中替换:

EMAIL_BACKEND = 'django_smtp_ssl.SSLEmailBackend'

by

通过

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

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

#5


2  

In settings.py change this

在settings.py中更改此设置

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

#6


1  

This works for me:

这对我有用:

settings.py

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

解锁验证码:https://accounts.google.com/DisplayUnlockCaptcha

views.py

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