2017-08-12 63 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