Remove double dots from hostnames when sending SMTP EHLO

This commit is contained in:
Kovid Goyal 2019-09-28 18:37:10 +05:30
parent 8d520400c5
commit 162d836d90
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C

View File

@ -26,6 +26,10 @@ def decode_fqdn(fqdn):
return fqdn return fqdn
def sanitize_hostname(hostname):
return hostname.replace('..', '_')
def safe_localhost(): def safe_localhost():
# RFC 2821 says we should use the fqdn in the EHLO/HELO verb, and # RFC 2821 says we should use the fqdn in the EHLO/HELO verb, and
# if that can't be calculated, that we should use a domain literal # if that can't be calculated, that we should use a domain literal
@ -119,7 +123,7 @@ def sendmail_direct(from_, to, msg, timeout, localhost, verbose,
import polyglot.smtplib as smtplib import polyglot.smtplib as smtplib
hosts = get_mx(to.split('@')[-1].strip(), verbose) hosts = get_mx(to.split('@')[-1].strip(), verbose)
timeout=None # Non blocking sockets sometimes don't work timeout=None # Non blocking sockets sometimes don't work
kwargs = dict(timeout=timeout, local_hostname=localhost or safe_localhost()) kwargs = dict(timeout=timeout, local_hostname=sanitize_hostname(localhost or safe_localhost()))
if debug_output is not None: if debug_output is not None:
kwargs['debug_to'] = debug_output kwargs['debug_to'] = debug_output
s = smtplib.SMTP(**kwargs) s = smtplib.SMTP(**kwargs)
@ -160,7 +164,7 @@ def sendmail(msg, from_, to, localhost=None, verbose=0, timeout=None,
port = int(port) port = int(port)
if port < 0: if port < 0:
port = 25 if encryption != 'SSL' else 465 port = 25 if encryption != 'SSL' else 465
kwargs = dict(host=relay, port=port, timeout=timeout, local_hostname=localhost or safe_localhost()) kwargs = dict(host=relay, port=port, timeout=timeout, local_hostname=sanitize_hostname(localhost or safe_localhost()))
if debug_output is not None: if debug_output is not None:
kwargs['debug_to'] = debug_output kwargs['debug_to'] = debug_output
cls = get_smtp_class(use_ssl=encryption == 'SSL', debuglevel=verbose) cls = get_smtp_class(use_ssl=encryption == 'SSL', debuglevel=verbose)