Python Django 1.8 使用 gmail SMTP 发送邮件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31324005/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 09:48:30  来源:igfitidea点击:

Django 1.8 sending mail using gmail SMTP

pythondjangoemailgmail

提问by Sarath Babu

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

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

My settings.pycontains:

我的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 = '[email protected]'

EMAIL_BACKEND = 'django_smtp_ssl.SSLEmailBackend'
EMAIL_HOST='smtp.gmail.com'
EMAIL_PORT=465
EMAIL_HOST_USER = 'sarath4coding'
EMAIL_HOST_PASSWORD = '*********'
DEFAULT_EMAILsarFROM4

from django.core import mail
mail.send_mail('subject','message','[email protected]',['[email protected]'])

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 发送邮件。

回答by Ajay Gupta

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'

回答by Alex Karahanidi

for me in settings.py:

我在settings.py 中

EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = '[email protected]'
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

回答by ordenador.cl

This works for me:

这对我有用:

settings.py

设置.py

EMAIL_BACKEND = 'django_smtp_ssl.SSLEmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_PASSWORD = 'test'
EMAIL_HOST_USER = '[email protected]'
EMAIL_PORT = 587
EMAIL_USE_TLS = True

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

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

views.py

视图.py

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

回答by Iman Mirzadeh

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 中完美运行
首先,您应该检查此链接,由 google 提供,您所做的 :) 请
注意,由于一些我不知道的奇怪原因,您必须在以下代码中进行编码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,
         ['[email protected]'], fail_silently=False)

also this is my settings in setting.pyfile:

这也是我在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 = '[email protected]' #my gmail username
EMAIL_PORT = 587
DEFAULT_FROM_EMAIL = EMAIL_HOST_USER

回答by edilio

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

回答by Abhishek Yadav

In settings.py change this

在 settings.py 中改变这个

EMAIL_HOST='imap.gmail.com'
EMAIL_PORT = 587
EMAIL_HOST_USER = '[email protected]'
EMAIL_HOST_PASSWORD ='**********'
EMAIL_USE_SSL=False
EMAIL_USE_TLS= True

回答by Parthi.N

I used this for django 1.11

我将它用于 django 1.11

In settings.py

在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 = '[email protected]' #sender mail username
EMAIL_PORT = 587
DEFAULT_FROM_EMAIL = EMAIL_HOST_USER

In view.py

在视图.py

send_mail('mail subject', 'body content',settings.EMAIL_HOST_USER,
                      ['[email protected]'], fail_silently=False)

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

并转到https://myaccount.google.com/u/0/security?hl=en以启用安全性较低的应用程序访问