diff --git a/Lib/smtplib.py b/Lib/smtplib.py index 0695840df78..6f37ceea130 100755 --- a/Lib/smtplib.py +++ b/Lib/smtplib.py @@ -237,9 +237,18 @@ def __init__(self, host = '', port = 0, local_hostname = None): if code != 220: raise SMTPConnectError(code, msg) if local_hostname: - self.local_hostname = local_hostname + self.local_hostname = local_hostname else: - self.local_hostname = socket.getfqdn() + # 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 + # instead (essentially an encoded IP address like [A.B.C.D]). + fqdn = socket.getfqdn() + if '.' in fqdn: + self.local_hostname = fqdn + else: + # We can't find an fqdn hostname, so use a domain literal + addr = socket.gethostbyname(socket.gethostname()) + self.local_hostname = '[%s]' % addr def set_debuglevel(self, debuglevel): """Set the debug output level.