Question

Django send_email() function results in “Temporary failure in name resolution” error

Posted July 17, 2021 25 views
NginxEmailPythonDNSDjangoUbuntu 20.04DigitalOcean Droplets

Introduction

I’m hosting a Django application with Nginx and Gunicorn on a Digital Ocean droplet (Ubuntu 20.04) with a domain name and mail bought at NameCheap.

I will use the following example values in my code snippets:

domain = example.com
email = info@example.com
droplet ip address = digital_ocean_droplet_ip

I have configured everything as described in Set Up Django with Postgres, Nginx, and Gunicorn and Secure Nginx with Let’s Encrypt.

Everything is working as it should however when my application tries to send a mail I get the following error:

gunicorn[97049]:    self.connection = self.connection_class(self.host, self.port, **connection_params)
gunicorn[97049]:   File "/usr/lib/python3.8/smtplib.py", line 1043, in __init__
gunicorn[97049]:     SMTP.__init__(self, host, port, local_hostname, timeout,
gunicorn[97049]:   File "/usr/lib/python3.8/smtplib.py", line 255, in __init__
gunicorn[97049]:     (code, msg) = self.connect(host, port)
gunicorn[97049]:   File "/usr/lib/python3.8/smtplib.py", line 339, in connect
gunicorn[97049]:     self.sock = self._get_socket(host, port, self.timeout)
gunicorn[97049]:     new_socket = socket.create_connection((host, port), timeout,
gunicorn[97049]:   File "/usr/lib/python3.8/smtplib.py", line 1049, in _get_socket
gunicorn[97049]:   File "/usr/lib/python3.8/socket.py", line 787, in create_connection
gunicorn[97049]:     for res in getaddrinfo(host, port, 0, SOCK_STREAM):
gunicorn[97049]:   File "/usr/lib/python3.8/socket.py", line 918, in getaddrinfo
gunicorn[97049]:     for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
gunicorn[97049]: socket.gaierror: [Errno -3] Temporary failure in name resolution

Django settings.py

ALLOWED_HOSTS = os.environ.get('example.com,www.example.com,digital-ocean-droplet-ip')

EMAIL_BACKEND="django.core.mail.backends.smtp.EmailBackend"
EMAIL_USE_SSL=False
EMAIL_USE_TLS=True
EMAIL_PORT=587
EMAIL_HOST="mail.privateemail.com"
EMAIL_HOST_USER="info@example.com"
EMAIL_HOST_PASSWORD="***"
DEFAULT_FROM_EMAIL="info@example.com"

Django views.py

from django.core.mail import send_mail

# Send confirmation email
send_mail(
    # Subject
    'Subject',
    # Message
    'Message',
    # Sender
    'info@example.com',
    # Receiver
    [receiver@email.com],
    fail_silently=False
)

Digital Ocean DNS records Documentation

Type        Hostname                            Value                                       TTL (seconds)

SRV         _autodiscover._tcp.example.com      privateemail.com.example.com                43200
CNAME       autoconfig.example.com              privateemail.com.                           43200
CNAME       autodiscover.example.com            privateemail.com.                           43200
CNAME       mail.example.com                    privateemail.com.                           43200
TXT         example.com                         v=spf1 include:spf.privateemail.com ~all    3600
MX          example.com                         mx2.privateemail.com.   (priority 10)       14400
MX          example.com                         mx1.privateemail.com.   (priority 10)       14400
A           example.com                         digital_ocean_droplet_ip                    3600
A           www.example.com                     digital_ocean_droplet_ip                    3600
NS          example.com                         ns1.digitalocean.com.                       1800
NS          example.com                         ns2.digitalocean.com.                       1800
NS          example.com                         ns3.digitalocean.com.                       1800

NameCheap Custom DNS Documentation

ns1.digitalocean.com
ns2.digitalocean.com
ns3.digitalocean.com

When I ping the mail server from the droplet (mail.privateemail.com) I get this result.

telnet mail.privateemail.com 587

Trying 198.54.122.60...
Connected to mail.privateemail.com.
Escape character is '^]'.
220 PrivateEmail.com prod Mail Node
ehlo privateemail.com
250-mta-14.privateemail.com
250-PIPELINING
250-SIZE 81788928
250-ETRN
250-AUTH PLAIN LOGIN
250-ENHANCEDSTATUSCODES
250-8BITMIME
250-CHUNKING
250 STARTTLS
quit
221 2.0.0 Bye
Connection closed by foreign host.

Question

Does anyone have an idea how to fix this, because the rest of the application is running fine? I have a feeling that this issue is caused by DNS but I have no clue how to fix it and I feel like I’ve tried just about everything.

Hopefully someone can help me because I’m a little bit lost and don’t know how to continue.. Thanks in advance!

I’ve tried the following settings so far without any success:

EMAIL_USE_SSL       True, False (with the according ports)
EMAIL_USE_TLS       True, False (with the according ports)
EMAIL_PORT          465 port for SSL, 25/587 port for TLS
EMAIL_HOST          mail.privateemail.com
                    mx1.privateemail.com
                    mx2.privateemail.com
                    mail.example.com
                    example.com
                    digital_ocean_droplet_ip

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

×
Submit an Answer
1 answer

Hi @RvH,

It seems to me like your Droplet is not able to resolve your own domain’s DNS. SSH to you droplet to test this and type the following:

ping -c 3 mail.privateemail.com

Or try to ping the domain of the e-mail address you’ve used.

Do both work?