socket.gaierror: [Errno 10109] getaddrinfo failed

This is my email configuration in project settings.py file

# Email Configuration
EMAIL_BACKEND = "django.core.mail.backends.smtp.EmailBackend"
EMAIL_HOST = config("EMAIL_HOST", cast=str, default="smtp.gmail.com")
EMAIL_PORT = config("EMAIL_PORT", cast=str, default='587') # Recommended
EMAIL_HOST_USER = config("EMAIL_HOST_USER", cast=str, default=None)
EMAIL_HOST_PASSWORD = config("EMAIL_HOST_PASSWORD", cast=str, default=None)
EMAIL_USE_TLS = config("EMAIL_USE_TLS", cast=bool, default=True)  # Use EMAIL_PORT 587 for TLS
EMAIL_USE_SSL = config("EMAIL_USE_SSL", cast=bool, default=False)  # Use MAIL_PORT 465 for SSL

The above variables are getting values from .env file. Here’s how .env file looks like:

EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = "587" # TLS
EMAIL_USE_TLS = True
EMAIL_USE_SSL = False
EMAIL_HOST_USER = "the123456@gmail.com"
EMAIL_HOST_PASSWORD = "jads zwer kghj wtyj"

*I used arbitary user and password as a sample here but these are accurate in my actual .env file

Full error details:

(D:\Study\Data_Science\Projects\pisf\env) D:\Study\Data_Science\Projects\pisf\src>python manage.py sendtestemail --admin
Traceback (most recent call last):
  File "D:\Study\Data_Science\Projects\pisf\src\manage.py", line 22, in <module>
    main()
  File "D:\Study\Data_Science\Projects\pisf\src\manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "D:\Study\Data_Science\Projects\pisf\env\lib\site-packages\django\core\management\__init__.py", line 442, in execute_from_command_line
    utility.execute()
  File "D:\Study\Data_Science\Projects\pisf\env\lib\site-packages\django\core\management\__init__.py", line 436, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "D:\Study\Data_Science\Projects\pisf\env\lib\site-packages\django\core\management\base.py", line 413, in run_from_argv
    self.execute(*args, **cmd_options)
  File "D:\Study\Data_Science\Projects\pisf\env\lib\site-packages\django\core\management\base.py", line 459, in execute
    output = self.handle(*args, **options)
  File "D:\Study\Data_Science\Projects\pisf\env\lib\site-packages\django\core\management\commands\sendtestemail.py", line 46, in handle
    mail_admins(subject, "This email was sent to the site admins.")
  File "D:\Study\Data_Science\Projects\pisf\env\lib\site-packages\django\core\mail\__init__.py", line 135, in mail_admins
    mail.send(fail_silently=fail_silently)
  File "D:\Study\Data_Science\Projects\pisf\env\lib\site-packages\django\core\mail\message.py", line 301, in send
    return self.get_connection(fail_silently).send_messages([self])
  File "D:\Study\Data_Science\Projects\pisf\env\lib\site-packages\django\core\mail\backends\smtp.py", line 128, in send_messages
    new_conn_created = self.open()
  File "D:\Study\Data_Science\Projects\pisf\env\lib\site-packages\django\core\mail\backends\smtp.py", line 86, in open
    self.connection = self.connection_class(
  File "D:\Study\Data_Science\Projects\pisf\env\lib\smtplib.py", line 255, in __init__
    (code, msg) = self.connect(host, port)
  File "D:\Study\Data_Science\Projects\pisf\env\lib\smtplib.py", line 341, in connect
    self.sock = self._get_socket(host, port, self.timeout)
  File "D:\Study\Data_Science\Projects\pisf\env\lib\smtplib.py", line 312, in _get_socket
    return socket.create_connection((host, port), timeout,
  File "D:\Study\Data_Science\Projects\pisf\env\lib\socket.py", line 824, in create_connection
    for res in getaddrinfo(host, port, 0, SOCK_STREAM):
  File "D:\Study\Data_Science\Projects\pisf\env\lib\socket.py", line 955, in getaddrinfo
    for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno 10109] getaddrinfo failed

Getting response from this so I don’t think this issue is causing by firewall

import smtplib

try:
    server = smtplib.SMTP('smtp.gmail.com', 587)
    server.starttls()
    print("Connection successful")
    server.quit()
except Exception as e:
    print("Failed to connect:", e)

The PORT should be an integer, not a string.

I’ve tried with integer as well but getting the same error.

What exactly did you change?

.env file

EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587 # TLS
EMAIL_USE_TLS = True
EMAIL_USE_SSL = False
EMAIL_HOST_USER = "the123456@gmail.com"
EMAIL_HOST_PASSWORD = "jads zwer kghj wtyj"

settings.py

# Email Configuration
EMAIL_BACKEND = "django.core.mail.backends.smtp.EmailBackend"
EMAIL_HOST = config("EMAIL_HOST", cast=str, default="smtp.gmail.com")
EMAIL_PORT = config("EMAIL_PORT") # Recommended
EMAIL_HOST_USER = config("EMAIL_HOST_USER", cast=str, default=None)
EMAIL_HOST_PASSWORD = config("EMAIL_HOST_PASSWORD", cast=str, default=None)
EMAIL_USE_TLS = config("EMAIL_USE_TLS", cast=bool, default=True)  # Use EMAIL_PORT 587 for TLS
EMAIL_USE_SSL = config("EMAIL_USE_SSL", cast=bool, default=False)  # Use MAIL_PORT 465 for SSL

You may need to explicitly specify cast=int here.

Finally solved the issue.

The error occurred because using comment in the environment file after declaring a value

EMAIL_PORT = "587" # TLS

So the fix is to just remove the comment. Better not use comments in the environment file.

EMAIL_PORT = "587"