]> granicus.if.org Git - python/commitdiff
__init__(): We'll try to be more RFC 2821 compliant by providing for a
authorBarry Warsaw <barry@python.org>
Tue, 26 Mar 2002 20:27:35 +0000 (20:27 +0000)
committerBarry Warsaw <barry@python.org>
Tue, 26 Mar 2002 20:27:35 +0000 (20:27 +0000)
better local_hostname default.  According to RFC 2821, it is
recommended that the fqdn hostname be provided in the EHLO/HELO verb
and if that can't be calculated, to use a domain literal.

The rationale for this change is documented in SF patch #497736 which
also had privacy concerns about leaking the fqdn in the EHLO/HELO.  We
decided this wasn't a big concern because no user data is leaked, and
the IP will always be leaked.  The local_hostname argument is provided
for those clients that are super paranoid.

Using localhost.localdomain may break some strict smtp servers so we
decided against using it as the default.

Lib/smtplib.py

index 0695840df78f7da11b5b4fa7546149a67baab7d2..6f37ceea1307d9bf49c26af27cc1d1b86425fb16 100755 (executable)
@@ -237,9 +237,18 @@ class SMTP:
             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.