Search code examples
pythondjangoauthenticationsmtpemail-validation

DJANGO EMAIL CONFIRMATION: [WinError 10061] No connection could be made because the target machine actively refused it


I'm trying to send an email verification code for account confirmation.

Now, the thing is that I am trying to send an email to myself first, and my other email. I have turned off my antivirus, so that shouldn't be a problem. Other than this I can't figure our what I did wrong that it is not sending emails on the gmail account. Please point out what I'm doing wrong. I even applied all the fixes mentioned by this thread.

views.py

from django.core.mail import send_mail
from django.shortcuts import render,redirect
from django.contrib import messages,auth
from django.contrib.auth.models import User # this table already exists in django we import it 
from django.contrib.auth import authenticate, login
from django.conf import settings
from django.core.mail import EmailMessage

def register(request):
    if request.method=='POST':
        fname = request.POST['fname']
        lname=request.POST['lname'] #request.post[id of the input field]
        email = request.POST['email']
        password = request.POST['pass']
        password2 = request.POST['confirmpass']
        agree=request.POST.get('agree')
        if fname == '':
             messages.warning(request, 'Please enter First name!')
             return redirect('register')

        if lname == '':
             messages.warning(request, 'Please enter Last name!')
             return redirect('register')

        if email == '':
             messages.warning(request, 'Please enter Email!')
             return redirect('register')

        if password == '':
             messages.warning(request, 'Please enter Password!')
             return redirect('register')

        if password2 == '':
             messages.warning(request, 'Please enter Confirm Password!')
             return redirect('register')
        if ('agree' not in request.POST):
             messages.warning(request, 'Please agree to our terms and conditions!')
             return redirect('register')

        if password==password2:

            if User.objects.filter(username=email):
                messages.error(request,"Email Already Exists")
                return redirect('register')
            else:
                user=User.objects.create_user(username=email,first_name=fname,last_name=lname,password=password) #these are postgres first_name,last_name
                user.save()
                messages.success(request,"Successfully Registered")
                subject = 'Site Contact Form'
                contact_message = "%s : %s via %s "%(fname, lname, email)
                from_email = settings.EMAIL_HOST_USER
                to_email = [from_email, 'myotheremail@gmail.com']
                send_mail(subject, contact_message, from_email, to_email, fail_silently=False)
    
                return redirect('login')
        
        

        else:
            messages.error(request,"Passwords don't match")
            return redirect('register')
    else:
        return render(request, 'signupdiv.html')

settings.py

EMAIL_HOSTS = 'smtp.gmail.com'
EMAIL_HOST_USER = 'myemail@gmail.com'
EMAIL_HOST_PASSWORD = '*****'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
DEFAULT_FROM_EMAIL = EMAIL_HOST_USER

urls.py

from django.urls import path
from . import views

urlpatterns = [
    path('login', views.login, name='login'), 
    path('register', views.register, name='register'), 
    path('logout', views.logout, name='logout'),
]

When I register, the data successfuly stores into the database (as expected). But it gives error on send_email(params) from view.py of the code. Following is the error:

ConnectionRefusedError at /account/register
[WinError 10061] No connection could be made because the target machine actively refused it
Request Method: POST
Request URL:    http://127.0.0.1:8000/account/register
Django Version: 3.1.2
Exception Type: ConnectionRefusedError
Exception Value:    
[WinError 10061] No connection could be made because the target machine actively refused it
Exception Location: C:\Users\User\AppData\Local\Programs\Python\Python37\lib\socket.py, line 716, in create_connection
Python Executable:  C:\Users\User\Desktop\pfa\venv\Scripts\python.exe
Python Version: 3.7.5
Python Path:    
['C:\\Users\\User\\Desktop\\pfa',
 'C:\\Users\\User\\AppData\\Local\\Programs\\Python\\Python37\\python37.zip',
 'C:\\Users\\User\\AppData\\Local\\Programs\\Python\\Python37\\DLLs',
 'C:\\Users\\User\\AppData\\Local\\Programs\\Python\\Python37\\lib',
 'C:\\Users\\User\\AppData\\Local\\Programs\\Python\\Python37',
 'C:\\Users\\User\\Desktop\\pfa\\venv',
 'C:\\Users\\User\\Desktop\\pfa\\venv\\lib\\site-packages']
Server time:    Sun, 25 Oct 2020 23:17:08 +0000

Solution

  • You missed a little bit trivial thing. in settings.py

    It should be EMAIL_HOST = 'smtp.gmail.com'

    Not EMAIL_HOSTS = 'smtp.gmail.com'