2017-08-12 65 views
0

我已经使用Django实现了重置密码功能。在这里,我将重置密码链接发送到已注册的电子邮件,并且没有邮件进入收件箱。我在下面解释我的代码。无法使用Django和Python发送电子邮件

settings.py:

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

EMAIL_HOST = 'smtp.gmail.com' 
EMAIL_PORT = 587 
EMAIL_HOST_USER = '[email protected]' 
EMAIL_HOST_PASSWORD = '*********' 
EMAIL_USE_TLS = False 
DEFAULT_FROM_EMAIL = '[email protected]' 

注册/ password_reset_form.html:

{% extends 'base.html' %} 

{% block content %} 
    <h3>Forgot password</h3> 
    <form method="post"> 
    {% csrf_token %} 
    {{ form.as_p }} 
    <button type="submit">Submit</button> 
    </form> 
{% endblock %} 

注册/ password_reset_email.html:

{% autoescape off %} 
    To initiate the password reset process for your {{ user.get_username }} TestSite Account, 
    click the link below: 

    {{ protocol }}://{{ domain }}{% url 'password_reset_confirm' uidb64=uid token=token %} 

If clicking the link above doesn't work, please copy and paste the URL in a new browser 
window instead. 

Sincerely, 
The Nuclear Team 
{% endautoescape %} 

我的密码重置的形式看起来像下面。

enter image description here

在这里,我无法将邮件发送到指定的邮箱ID。

+1

你能告诉我们你的视图密码重置? – wencakisa

+2

只需阅读文档。你的第一行代码是错误的。 https://docs.djangoproject.com/en/1.11/topics/email/#console-backend – allcaps

+0

它不发送邮件,因为邮件被打印到控制台。就像你配置它一样。 – allcaps

回答

2

耀需要更换:

EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend' 
#          ^^^^^^^^ 

BACKEND = 'django.core.mail.backends.smtp.EmailBackend' 
#         ^^^^^^ 

更多的信息在这里:email-backend

+0

我做到了,但仍然存在'SMTP AUTH扩展不支持服务器错误。 – satya

+0

你输入正确的电子邮件吗?重置完成后重定向了吗? –

+0

它的工作,我只是改变了'EMAIL_USE_TLS = True'.Thanks – satya

0

你应该对你的看法是这样的:

from django.core.mail import EmailMessage 

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

你也需要解锁的captcha上的Gmail帐户: https://accounts.google.com/DisplayUnlockCaptcha