Django 1.8 使用 gmail SMTP 发送邮件
Posted
技术标签:
【中文标题】Django 1.8 使用 gmail SMTP 发送邮件【英文标题】:Django 1.8 sending mail using gmail SMTP 【发布时间】:2015-09-28 05:07:13 【问题描述】:我尝试在 django 1.8 中使用 smtp.gmail.com
发送邮件
我的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'
from django.core import mail
mail.send_mail('subject','message','sarath4coding@gmail.com',['sarath4coding@gmail.com'])
但是得到了这个错误:
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')
我尝试了文档中的所有内容,并遵循了许多建议的解决方案。
like https://accounts.google.com/DisplayUnlockCaptcha,启用低安全性应用等
但我仍然有错误
谁能告诉我如何正确配置 Django 1.8 以使用 Gmail 发送邮件。
【问题讨论】:
我有类似的配置,但有:EMAIL_HOST_USER='sarath4coding@gmail.com'
和 EMAIL_PORT=587
和 EMAIL_USE_TLS = True
这里也一样。尝试了下面的每个答案,但它仍然不适合我。
【参考方案1】:
在 settings.py 中对我来说:
EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = 'test@gmail.com'
EMAIL_HOST_PASSWORD = 'test'
EMAIL_PORT = 587
和views.py:
from django.core.mail import EmailMessage
email = EmailMessage('title', 'body', to=[email])
email.send()
和:https://accounts.google.com/DisplayUnlockCaptcha
并确保为不太安全的应用打开权限。
【讨论】:
【参考方案2】:我将它用于 django 1.11
在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
在view.py中
send_mail('mail subject', 'body content',settings.EMAIL_HOST_USER,
['receiver@mail.com'], fail_silently=False)
然后转到 https://myaccount.google.com/u/0/security?hl=en 以启用不太安全的应用访问
【讨论】:
【参考方案3】:在 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
【讨论】:
【参考方案4】:记住:
转到您的 Google 帐户设置,找到安全 -> 帐户权限 -> 访问不太安全的应用,启用此选项。
关于此选项:https://support.google.com/accounts/answer/6010255
【讨论】:
可能更安全的选择是添加应用专用密码:security.google.com/settings/u/1/security/apppasswords 链接已损坏@ptim :( 发送@Vini.g.fer!这个帮助页面解释了这个过程:support.google.com/accounts/answer/185833?hl=en【参考方案5】:我对此进行了测试,并在 django 1.8 中完美运行:
首先你应该检查this link,由你做的谷歌提供:)
请注意,由于一些我不知道的奇怪原因,您必须在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)
这也是我在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
【讨论】:
【参考方案6】:这对我有用:
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
解锁验证码: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()
【讨论】:
【参考方案7】:在你的 settings.py 文件中替换:
EMAIL_BACKEND = 'django_smtp_ssl.SSLEmailBackend'
通过
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
【讨论】:
以上是关于Django 1.8 使用 gmail SMTP 发送邮件的主要内容,如果未能解决你的问题,请参考以下文章
在 django 中发送邮件 - gmail smtp 问题
如何在 django 中使用 gmail smtp 更改回复和返回路径标头
Django 通过 smtp.gmail.com 发送电子邮件的问题
带有 smtp.gmail SMTPAuthenticationError 534 的 Django 电子邮件需要特定于应用程序的密码