0

I have Used Django EmailMessage function .
Here is views.py file.

def send_bill_mail(request):
    context_dict={
                "name": "abc"
                }
    html_message = render_to_string('home/email_templates/bill_generation.html', context_dict)
    try:
        email = EmailMessage(
            ' Welcome to store',
            html_message,
            to=['abc@gmail.com']
            )
        email.content_subtype = "html" 
        email.send()
    except Exception as e:
        messages.error(request,"Something Went Wrong !")
    return redirect('home:dashboard')

Here Is settings.py

EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = 'xyz@gmail.com'
EMAIL_HOST_PASSWORD = 'xyz'
EMAIL_PORT = 587

Following Exception occured during processing of views.

    Exception happened during processing of request from ('127.0.0.1', 50522)
Traceback (most recent call last):
  File "C:\Users\sanke\AppData\Local\Programs\Python\Python37\lib\socketserver.py", line 650, in process_request_thread
    self.finish_request(request, client_address)
  File "C:\Users\sanke\AppData\Local\Programs\Python\Python37\lib\socketserver.py", line 360, in finish_request
    self.RequestHandlerClass(request, client_address, self)
  File "C:\Users\sanke\AppData\Local\Programs\Python\Python37\lib\socketserver.py", line 720, in __init__
    self.handle()
  File "C:\Users\sanke\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\servers\basehttp.py", line 174, in handle
    self.handle_one_request()
  File "C:\Users\sanke\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\servers\basehttp.py", line 182, in handle_one_request
    self.raw_requestline = self.rfile.readline(65537)
  File "C:\Users\sanke\AppData\Local\Programs\Python\Python37\lib\socket.py", line 589, in readinto
    return self._sock.recv_into(b)
ConnectionAbortedError: [WinError 10053] An established connection was aborted by the software in your host machine
yedpodtrzitko
  • 9,035
  • 2
  • 40
  • 42

1 Answers1

2

This issue generally happens when you run project locally , may be you should try hosting it somewhere on internet

or

Using connections can solve your problem like below

  def send_bill_mail(request):
        context_dict={
                    "name": "abc"
                    }
        html_message = render_to_string('home/email_templates/bill_generation.html', context_dict)
        email_list = []
        try:
            email = EmailMessage(
                ' Welcome to store',
                html_message,
                to=['abc@gmail.com']
                )
            email.content_subtype = "html" 
            email_list.append(email)
            connection = mail.get_connection() 
            connection.send_messages(email_list)
        except Exception as e:
            messages.error(request,"Something Went Wrong !")
        return redirect('home:dashboard')
Amit Sarje
  • 21
  • 2