1

I'm working on a Django project. I need to send my users the emails and receive the contact us form when users submit the form. I'm using Gmail. As I watched tutorials, the email which receives the emails need to turn on less secure on security section. However, as May 2022 google changed the policy and now the option is not there.

https://support.google.com/accounts/answer/6010255?authuser=1&hl=en&authuser=1&visit_id=638033119094066220-3031639860&p=less-secure-apps&rd=1

So, now when I'm trying to send an email I face the following error.

ConnectionRefusedError at /contact/
[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/contact/
Django Version: 4.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\Sed AKH\AppData\Local\Programs\Python\Python38\lib\socket.py, line 796, in create_connection
Raised during:  main.views.contact
Python Executable:  C:\Users\Sed AKH\AppData\Local\Programs\Python\Python38\python.exe
Python Version: 3.8.1
Python Path:    
['E:\\Projects\\Python\\Django\\weblimey',
 'C:\\Users\\Sed AKH\\AppData\\Local\\Programs\\Python\\Python38\\python38.zip',
 'C:\\Users\\Sed AKH\\AppData\\Local\\Programs\\Python\\Python38\\DLLs',
 'C:\\Users\\Sed AKH\\AppData\\Local\\Programs\\Python\\Python38\\lib',
 'C:\\Users\\Sed AKH\\AppData\\Local\\Programs\\Python\\Python38',
 'C:\\Users\\Sed '
 'AKH\\AppData\\Local\\Programs\\Python\\Python38\\lib\\site-packages',
 'C:\\Users\\Sed '
 'AKH\\AppData\\Local\\Programs\\Python\\Python38\\lib\\site-packages\\win32',
 'C:\\Users\\Sed '
 'AKH\\AppData\\Local\\Programs\\Python\\Python38\\lib\\site-packages\\win32\\lib',
 'C:\\Users\\Sed '
 'AKH\\AppData\\Local\\Programs\\Python\\Python38\\lib\\site-packages\\Pythonwin']
Server time:    Sun, 06 Nov 2022 06:26:20 +0000

And here my code :

def contact(request):
    if request.method == 'POST':
        try:
            contact = ContactForm()
            name = request.POST.get('name')
            email = request.POST.get('email')
            message = request.POST.get('message')
            contact.name = name
            contact.email = email
            contact.message = message
            contact.save()
        except Exception  as ve:
            print('Something happend')
        finally:
            send_mail(name,message,email,['sportold4@gmail.com'],fail_silently=False)
            return render(request, 'contact.html',{'page': 'contact'})
    return render(request, 'contact.html',{'page': 'contact'})

Can Anyone tell me what's the cause and if there is any another way to send email in Django?

Sunderam Dubey
  • 1
  • 11
  • 20
  • 40
Abdu4
  • 1,269
  • 2
  • 11
  • 19

0 Answers0