Email could not sent using Django and Python

I am facing one issue. I have implemented the reset password functionality using Django. Here I am sending the reset password link to registered email and no mail is coming to the inbox. I am explaining my code below.

settings.py:

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

EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_HOST_USER = 'user474@gmail.com'
EMAIL_HOST_PASSWORD = '*********'
EMAIL_USE_TLS = False
DEFAULT_FROM_EMAIL = 'user474@gmail.com'

registeration/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 %}

registration/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 %}

My password reset form looks like below.

enter image description here

Here I could not send the mail to the given email id. Please help me.

2 answers

  • answered 2017-08-12 09:33 JuanMi Gabarron

    You should have on your views something like:

    from django.core.mail import EmailMessage
    
    email = EmailMessage('title', 'body', to=[email])
    email.send()
    

    Also you need to unlock captcha on your gmail account: https://accounts.google.com/DisplayUnlockCaptcha

  • answered 2017-08-12 09:33 Bear Brown

    Yiu need replace:

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

    to

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

    more info here: email-backend