Forums

Gmail smtp relay rejects 'localhost' hostname on SMTP HELO/ELHO commands

Hello guys, I need some help over here:

We are using GSuite (Gmail for business) as our email provider, and our Django web app uses Gmail SMTP relay service to send emails to our staff.

I used an app password in order to connect gmail and things were working fine until Django started to raise smtplib.SMTPServerDisconnected: Connection unexpectedly closed.

As the same code works on my local machine, I decided to investigate the SMTP communication steps. The problem is in the HELO/EHLO command being sent with localhost as hostname. Gmail relay does not enforce the use of a Full Qualified Domain Name, but recommends using a unique identifier and avoid the generic names like "localhost".

Once I monkey patched Django's full qualified domain name discovery, smtp connection went well and the emails were sent:

django.core.mail.utils.DNS_NAME._fqdn = "my.desired.host"

My question is: There is a way of setting the hostname for python anywhere instance (is modifing etc/hosts ok?) ?

If there is no way to change, this workaround is safe?

Thanks in advance!

Refs:

Django ticket about DNS_NAME and hostname on SMTP: https://code.djangoproject.com/ticket/6989

G Suite relay configuration help excerpt:

We recommend that you configure your mail server to present a unique identifier (such as your domain name or the name of your mail server) in the HELO or EHLO command in the SMTP relay connections your server makes to Google. Avoid using generic names such as "localhost" or "smtp-relay.gmail.com," which can occasionally result in issues with DoS limits.

Unfortunately you can't change /etc/hosts on PythonAnywhere, but yes, that solution looks like a solid one for now (though of course it does rely on an undocumented Django API, so if and when you upgrade Django you should double-check that it still works).

Thank you for your fast reply! I tested for some time and that solution is working fine. I will pay extra attention during upgrades.