如何修复django发送邮件?

时间:2016-10-09 21:30:47

标签: python django email

我正在尝试在django项目中发送邮件好几天。我从djangoproject.com获得了文档,但这对我不起作用。 我的settings.py包含用于发送邮件的这些代码行:

EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.gmail.com' 
EMAIL_PORT = 587
EMAIL_HOST_PASSWORD = '**********' #my gmail account's password 
EMAIL_HOST_USER = 'showkatalisalim@gmail.com'

my views.py包含contactform的这些行:

def contactForm(request):
    form = LocalContactForm(request.POST or None)
    if form.is_valid():
        send_mail(
'Subject here',
'Here is the message.',
settings.EMAIL_HOST_USER,
['zellaic.showkatali@gmail.com'],
fail_silently=False,
)
        return HttpResponseRedirect('thanks')
    context = {"page":"contact_form", "title":"Contact with us", "form":form}
    return render(request, 'form.html', context)

当我尝试通过我的表单发送邮件时:它显示出来:

SMTPAuthenticationError at /contact/
(534, '5.7.14 <https://accounts.google.com/signin/continue?     sarp=1&scc=1&plt=AKgnsbtZ\n5.7.14 4CcZKxu-As7S5tfd-3YTAz6XMdwLYcJKWk7_bViejaO8v_-mx-aD8PLO5zixLUMbTv38LY\n5.7.14 qE3ifOl5aXJOXaOVN5jU9Tl-HJVDj1_bc0n9nJ4PHERsBsyu8L0JRr9rM3ED0TdFXLV3wl\n5.7.14 _GF3jCTuCHIydf-YXcFZidIIqrERHyAORvqYmuPs0qHd_rt3ecbJUBOIW9PvzOXxGBiXg2\n5.7.14 ehh9XhyakjWXfOEuJgbxiNBMdCIM0> 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 ao5sm46546625pad.1 - gsmtp')

Request Method:     POST
Request URL:    http://www.zellaic.com/contact/
Django Version:     1.10.1
Exception Type:     SMTPAuthenticationError
Exception Value:    

(534, '5.7.14 <https://accounts.google.com/signin/continue?sarp=1&scc=1&plt=AKgnsbtZ\n5.7.14 4CcZKxu-As7S5tfd-3YTAz6XMdwLYcJKWk7_bViejaO8v_-mx-aD8PLO5zixLUMbTv38LY\n5.7.14 qE3ifOl5aXJOXaOVN5jU9Tl-HJVDj1_bc0n9nJ4PHERsBsyu8L0JRr9rM3ED0TdFXLV3wl\n5.7.14 _GF3jCTuCHIydf-YXcFZidIIqrERHyAORvqYmuPs0qHd_rt3ecbJUBOIW9PvzOXxGBiXg2\n5.7.14 ehh9XhyakjWXfOEuJgbxiNBMdCIM0> 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 ao5sm46546625pad.1 - gsmtp')

Exception Location:     /usr/lib64/python2.7/smtplib.py in login, line    621
Python Executable:  /usr/local/bin/python
Python Version:     2.7.5
Python Path:    
['/home/showkatali/webapps/zellaic/lib/python2.7/Django-1.10.1-py2.7.egg',
'/home/showkatali/webapps/zellaic',
'/home/showkatali/webapps/zellaic/src',
'/home/showkatali/webapps/zellaic/lib/python2.7',
'/home/showkatali/lib/python2.7/pip-8.1.2-py2.7.egg',
'/home/showkatali/lib/python2.7',
'/usr/lib64/python27.zip',
'/usr/lib64/python2.7',
'/usr/lib64/python2.7/plat-linux2',
'/usr/lib64/python2.7/lib-tk',
'/usr/lib64/python2.7/lib-old',
'/usr/lib64/python2.7/lib-dynload',
'/usr/lib64/python2.7/site-packages',
'/usr/lib64/python2.7/site-packages/PIL',
'/usr/lib64/python2.7/site-packages/geos',
'/usr/lib/python2.7/site-packages']

当我尝试将这些行用于shell时,它会出现:

send_mail('subject', 'sometext', settings.EMAIL_HOST_USER,   ['zellaic.showkatali@gmail.com'], fail_silently = False)
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/home/endless/Desktop/project/webfaction/env/local/lib/python2.7/site-  packages/django/core/mail/__init__.py", line 62, in send_mail
return mail.send()
File   "/home/endless/Desktop/project/webfaction/env/local/lib/python2.7/site-  packages/django/core/mail/message.py", line 342, in send
return self.get_connection(fail_silently).send_messages([self])
File  "/home/endless/Desktop/project/webfaction/env/local/lib/python2.7/site- packages/django/core/mail/backends/smtp.py", line 100, in send_messages
new_conn_created = self.open()
File   "/home/endless/Desktop/project/webfaction/env/local/lib/python2.7/site-  packages/django/core/mail/backends/smtp.py", line 67, in open
self.connection.login(self.username, self.password)

此时此刻,我该怎么办?需要我的Gmail帐户的任何配置?

1 个答案:

答案 0 :(得分:1)

以下是使用标准SHELL=sh -XVemail软件包的gmail实现(请注意设置中的不同端口和主机):

smtplib

代码:

//settings.py
EMAIL_HOST = 'smtp.googlemail.com' #XXX
EMAIL_PORT = 465                   #XXX
EMAIL_HOST_PASSWORD = '**********' 
EMAIL_HOST_USER = '***@gmail.com'