Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7bc82bb

Browse files
author
Peter Schneider-Kamp
committed
add better algorithm to get fully qualified domain name for localhost
in smtplib.ehlo() and smtplib.helo(). closes patch #101103 closes bug #110935
1 parent 10e1bf2 commit 7bc82bb

1 file changed

Lines changed: 21 additions & 17 deletions

File tree

Lib/smtplib.py

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,6 @@ def __init__(self, recipients):
9898
self.args = ( recipients,)
9999

100100

101-
102101
class SMTPDataError(SMTPResponseException):
103102
"""The SMTP server didn't accept the data."""
104103

@@ -108,6 +107,7 @@ class SMTPConnectError(SMTPResponseException):
108107
class SMTPHeloError(SMTPResponseException):
109108
"""The server refused our HELO reply."""
110109

110+
111111
def quoteaddr(addr):
112112
"""Quote a subset of the email addresses defined by RFC 821.
113113
@@ -133,6 +133,24 @@ def quotedata(data):
133133
return re.sub(r'(?m)^\.', '..',
134134
re.sub(r'(?:\r\n|\n|\r(?!\n))', CRLF, data))
135135

136+
def _get_fqdn_hostname(name):
137+
name = string.strip(name)
138+
if len(name) == 0:
139+
name = socket.gethostname()
140+
try:
141+
hostname, aliases, ipaddrs = socket.gethostbyaddr(name)
142+
except socket.error:
143+
pass
144+
else:
145+
aliases.insert(0, hostname)
146+
for name in aliases:
147+
if '.' in name:
148+
break
149+
else:
150+
name = hostname
151+
return name
152+
153+
136154
class SMTP:
137155
"""This class manages a connection to an SMTP or ESMTP server.
138156
SMTP Objects:
@@ -288,14 +306,7 @@ def helo(self, name=''):
288306
Hostname to send for this command defaults to the FQDN of the local
289307
host.
290308
"""
291-
name=string.strip(name)
292-
if len(name)==0:
293-
name = socket.gethostname()
294-
try:
295-
name = socket.gethostbyaddr(name)[0]
296-
except socket.error:
297-
pass
298-
self.putcmd("helo",name)
309+
self.putcmd("helo", _get_fqdn_hostname(name))
299310
(code,msg)=self.getreply()
300311
self.helo_resp=msg
301312
return (code,msg)
@@ -305,14 +316,7 @@ def ehlo(self, name=''):
305316
Hostname to send for this command defaults to the FQDN of the local
306317
host.
307318
"""
308-
name=string.strip(name)
309-
if len(name)==0:
310-
name = socket.gethostname()
311-
try:
312-
name = socket.gethostbyaddr(name)[0]
313-
except socket.error:
314-
pass
315-
self.putcmd("ehlo",name)
319+
self.putcmd("ehlo", _get_fqdn_hostname(name))
316320
(code,msg)=self.getreply()
317321
# According to RFC1869 some (badly written)
318322
# MTA's will disconnect on an ehlo. Toss an exception if

0 commit comments

Comments
 (0)