Jatin Singh Bhati
Jatin Singh Bhati

Reputation: 133

ConnectionRefusedError while sending email through gmail using Django

I am trying to send e-mails through my Gmail account for my Django application. I'm using below configurations in settings.py file.

EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST_USER = '[email protected]'
EMAIL_HOST = 'smtp.gmail.com' # tried smtp-relay.gmail.com also
EMAIL_PORT = '587'
EMAIL_USE_TLS = True
EMAIL_HOST_PASSWORD = 'Gmail password / app password' 

Import statements in this regard:

from django.core.mail import send_mail, EmailMessage
from django.conf import settings

Following is in my views function:

    message = EmailMessage(subject=email_subject, body=email_message, from_email=settings.EMAIL_HOST_USER,  to=[email, '[email protected]'])
    message.send(fail_silently=False)

I have tried this also:

send_mail(email_subject, email_message , settings.EMAIL_HOST_USER, [email, '[email protected]'] , fail_silently=False, )

Any help is much appreciated. Please note that I have tried:

  1. enabling less secure apps button in Gmail
  2. Then, I disabled less secure apps, enabled 2 factor authentication and use app passwords option of Google.

Here is the complete traceback:

    Traceback (most recent call last):
  File "/Users/jatinsinghbhati/Documents/workspaces/djangoenv/lib/python3.9/site-packages/django/core/handlers/exception.py", line 47, in inner
    response = get_response(request)
  File "/Users/jatinsinghbhati/Documents/workspaces/djangoenv/lib/python3.9/site-packages/django/core/handlers/base.py", line 181, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/Users/jatinsinghbhati/Documents/workspaces/pollsite/signup/views.py", line 40, in index
    message.send(fail_silently=False)
  File "/Users/jatinsinghbhati/Documents/workspaces/djangoenv/lib/python3.9/site-packages/django/core/mail/message.py", line 284, in send
    return self.get_connection(fail_silently).send_messages([self])
  File "/Users/jatinsinghbhati/Documents/workspaces/djangoenv/lib/python3.9/site-packages/django/core/mail/backends/smtp.py", line 102, in send_messages
    new_conn_created = self.open()
  File "/Users/jatinsinghbhati/Documents/workspaces/djangoenv/lib/python3.9/site-packages/django/core/mail/backends/smtp.py", line 62, in open
    self.connection = self.connection_class(self.host, self.port, **connection_params)
  File "/Users/jatinsinghbhati/.pyenv/versions/3.9.0/lib/python3.9/smtplib.py", line 253, in __init__
    (code, msg) = self.connect(host, port)
  File "/Users/jatinsinghbhati/.pyenv/versions/3.9.0/lib/python3.9/smtplib.py", line 339, in connect
    self.sock = self._get_socket(host, port, self.timeout)
  File "/Users/jatinsinghbhati/.pyenv/versions/3.9.0/lib/python3.9/smtplib.py", line 310, in _get_socket
    return socket.create_connection((host, port), timeout,
  File "/Users/jatinsinghbhati/.pyenv/versions/3.9.0/lib/python3.9/socket.py", line 843, in create_connection
    raise err
  File "/Users/jatinsinghbhati/.pyenv/versions/3.9.0/lib/python3.9/socket.py", line 831, in create_connection
    sock.connect(sa)
ConnectionRefusedError: [Errno 61] Connection refused
[25/Feb/2021 19:38:20] "POST /signup/ HTTP/1.1" 500 101708

Upvotes: 2

Views: 771

Answers (1)

I. Kash
I. Kash

Reputation: 21

It should be

EMAIL_PORT = 587

But not

EMAIL_PORT = '587'

PORT number is an integer not a string.

Upvotes: 2

Related Questions