Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django: Why is console email backend not sending emails?

I'm trying to make an app to reset user password via email. I'm using the Django by Example book chapter 3, and also took guidance from this site: https://simpleisbetterthancomplex.com/tutorial/2016/09/19/how-to-create-password-reset-view.html

Settings.py contains the following line:

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

together with the required email settings.

I get to the password_reset view, enter dummy mail, click send, which finally brings me to the password_reset_done view. Below is the console output:

C:\PythonProjects\by-example-bookmarks>python manage.py runserver
Performing system checks...

System check identified no issues (0 silenced).
May 01, 2018 - 21:13:39
Django version 2.0.4, using settings 'bookmarks.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
[01/May/2018 21:13:51] "GET /account/login/ HTTP/1.1" 200 1090
[01/May/2018 21:13:57] "GET /account/password_reset/ HTTP/1.1" 200 830
[01/May/2018 21:14:04] "POST /account/password_reset/ HTTP/1.1" 302 0
[01/May/2018 21:14:05] "GET /account/password_reset/done/ HTTP/1.1" 200 602

After the "POST" output I expect to see a dump of the email in response to .console.EmailBackend setting, but got nothing, ever.

What could be the cause of the failure to simulate sending email?

Environment:

Windows 7, django 2.0, on CMD console, under pipenv shell

Edit 1 Same problem on a different machine.

Edit 2 Changing to .smtp doesn't change anything, no error.

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

Edit 3 I tried other people's work, like this one: https://github.com/ruddra/django-reset-password/tree/master

Still no luck.

Edit 4 Yet another "other people's work". Still no luck. https://github.com/wsvincent/django-auth-tutorial

Does it ever work?

Edit 5: views.py

from django.http import HttpResponse
from django.shortcuts import render
from django.contrib.auth import authenticate, login
from .forms import LoginForm
from django.contrib.auth.decorators import login_required

def user_login(request):
    if request.method == 'POST':
        form = LoginForm(request.POST)
        if form.is_valid():
            cd = form.cleaned_data
            user = authenticate( username=cd['username'],
                                 password=cd['password'])
            if user is not None:
                if user.is_active:
                    login(request, user)
                    return HttpResponse('Authenticated '\
                                        'successfully')
                else:
                    return HttpResponse('Disabled account')
            else:
                return HttpResponse('Invalid login')
    else:
        form = LoginForm()
    return render(request, 'account/login.html', {'form': form})

@login_required
def dashboard(request):
    return render(request,
                  'account/dashboard.html',
                  {'section': 'dashboard'})

urls.py

from django.conf.urls import url, include
from . import views
import django.contrib.auth.views
# i think the following does not serve any purpose anymore
from django.contrib.auth import views as auth_views

urlpatterns = [
    # previous login view
    # url(r'^login/$', views.user_login, name='login'),

    # login / logout urls
    url(r'^login/$',
        django.contrib.auth.views.login,
        name='login'),
    url(r'^logout/$',
        django.contrib.auth.views.logout,
        name='logout'),
    url(r'^logout-then-login/$',
        django.contrib.auth.views.logout_then_login,
        name='logout_then_login'),
    url(r'^$', views.dashboard, name='dashboard'),
    url(r'^password-change/$',
        django.contrib.auth.views.password_change,
        name='password_change'),
    url(r'^password-change/done/$',
        django.contrib.auth.views.password_change_done,
        name='password_change_done'),

    # restore password urls
    url('^', include('django.contrib.auth.urls')),
] 

edit 6 My github repo is here, in case anyone's curious: https://github.com/jeffplata/by-example-account

like image 328
JeffP Avatar asked May 01 '18 13:05

JeffP


People also ask

How does django send email?

Sending Emails with the Django ShellWe import the Django send_mail function. Then we import the settings object which contains all the global settings and the per-site settings (those inside the settings.py file). Finally, we pass all the needed arguments to the send_mail function.

How do I send an email notification in django REST framework?

Inside of the “send_mail.py”, create a function that takes in the following arguments. def send_mail(html,text='Email_body',subject='Hello word',from_email='',to_emails=[]): The next step would be to make sure that the “to_emails” argument is always a “list of emails” and not a string or any other data type.

How do I use django to send an email from Gmail?

send_mail() method is imported from django. The send_mail() method handles the transmission of Emails. There are some important parameters of send_mail(), which we have defined here: subject: Contains the subject part of the Email. message: Contains the Email body.

What is Email_use_tls?

The EMAIL_HOST_USER and EMAIL_HOST_PASSWORD settings, if set, are used to authenticate to the SMTP server, and the EMAIL_USE_TLS and EMAIL_USE_SSL settings control whether a secure connection is used.


1 Answers

It works now. As suggested by @Burhan Khalid, made very very sure that the there is a user with a valid email. Specifically, the email I'm typing in the reset view is found in the Users model/table.

like image 122
JeffP Avatar answered Sep 28 '22 23:09

JeffP