REG: Why my django email settings not sending email notifications?

64 views
Skip to first unread message

Amitesh Sahay

unread,
Feb 17, 2019, 4:19:35 AM2/17/19
to Django Users
I have created a registration page that sends an email notification on successful registration. I do have my email notification in place. But it doesn't seem to be working, neither it is throwing any error. Below is the code snippet.

views.py

def register(request):
    validators = [MinimumLengthValidator, NumberValidator, UppercaseValidator]
    if request.method == 'POST':
        first_name = request.POST['first_name']
        last_name = request.POST['last_name']
        email = request.POST['email']
        username = request.POST['username']
        password = request.POST['password']
        try:
            for validator in validators:
                validator().validate(password)
        except ValueError as e:
            messages.error(request, str(e))
            return redirect('register')
        password2 = request.POST['password2']

        # check if the password match
        if password == password2:

            if User.objects.filter(username=username).exists():
                messages.error(request, 'username already exist')
                return redirect('register')
            else:
                if User.objects.filter(email=email).exists():
                    messages.error(request, 'Registration Failed - Try different email address')
                    return redirect('register')
                else:
                    user = User.objects.create_user(username=username, password=password, email=email,
                                                    first_name=first_name, last_name=last_name)
                    user.save()
                    messages.success(request, 'Registration complete, please proceed to login')
                    return redirect('register')
        else:
            messages.error(request, 'password dose not match')
            return redirect('register')
    else:
        return render(request, 'ACCOUNTS/register.html')


def ThankYou(request, register):
    if request.method == 'POST':
        if register.is_valid():
                save_it = register.save(commit=False)
                save_it.save()
                subject = 'Registration successful'
                message = 'Thank you for registration, please continue with the login'
                from_email = settings.EMAIL_HOST_USER
                to_list = [save_it.email, settings.EMAIL_HOST_USER]
                try:
                    send_mail(
                        subject,
                        message,
                        from_email,
                        [to_list],
                        fail_silently=False,
                    )
                except ValueError:
                    return HttpResponse('Invalid header found.')
        else:
            messages.success(request, 'thank you ')
            return redirect('register')
    else:
        return redirect('index')

Below is my settings.py

EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_USE_TLS = False
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_HOST_USER = 'ami....@gmail.com'
EMAIL_HOST_PASSWORD = '123'
DEFAULT_FROM_EMAIL = 'ami....@gmail.com'

I have gone through a couple of Stackoverflow posts and made some changes as suggested. But they do not seem to have any effect. Below is one of the link.

Django Doesn't Send Email Notifications

Regards,
Amitesh Sahay
91-750 797 8619

Siddharth Tamang

unread,
Feb 17, 2019, 10:43:22 PM2/17/19
to django...@googlegroups.com
can you check if you your email server is somehow blocking your emails? Have you enabled your debug settings ON?

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/1493971974.505339.1550394996512%40mail.yahoo.com.
For more options, visit https://groups.google.com/d/optout.


--
Thank you
Siddharth Tamang
AWS Certified Solutions Architect - Associate

Amitesh Sahay

unread,
Feb 17, 2019, 11:03:28 PM2/17/19
to django...@googlegroups.com
The email server is a gmail smtp server. People across the globe use that for their development purpose. So, I guess that shouldn't be a issue. The debug is "ON" by default.

Regards,
Amitesh Sahay
91-750 797 8619

Simon A

unread,
Feb 17, 2019, 11:12:40 PM2/17/19
to Django users
Just to add, please try to create a python file with the simplest implementation of send mail just to check if it actually works. 

Siddharth Tamang

unread,
Feb 18, 2019, 12:09:13 AM2/18/19
to django...@googlegroups.com
When I used gmail for my testing, I had to go to my gmail settings to turn on some settings. Sorry cannot remember it but I will try to find what it was. By the way what does the debug error says?


For more options, visit https://groups.google.com/d/optout.

Onkar Narkar

unread,
Feb 18, 2019, 2:38:22 AM2/18/19
to Django users

Hi Amitesh,

 Are you getting some error like this:
smtplib.SMTPAuthenticationError: (534, b'5.7.9 Application-specific password required. Learn more at\n5.7.9  https://support.google.com/mail/?p=InvalidSecondFactor 128sm18480521pfx.7 - gsmtp')

If this is the case, you need to specify application specific password instead of your nornal in gmail password.

Siddharth Tamang

unread,
Feb 18, 2019, 3:11:49 AM2/18/19
to django...@googlegroups.com
if you have MFA enabled on your gmail, you may have to disable it 

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.

For more options, visit https://groups.google.com/d/optout.

Kiran Capoor

unread,
Feb 18, 2019, 2:04:08 PM2/18/19
to django...@googlegroups.com
Hi Amitesh,

From my experience, You need to go to Google’s settings and enable access to ‘less secure apps’. That will let you send emails freely from your django dev environment. Do so at your own risk, as any less secure app can use your email and password. I would suggest you create a dummy/ Dev only gmail account.

Regards,
Kiran Capoor

Sent from my iPhone
Reply all
Reply to author
Forward
0 new messages