[New-bugs-announce] [issue41470] smtplib.SMTP should reset internal 'helo' and 'ehlo' state within 'connect()'

Peter Stokes report at bugs.python.org
Tue Aug 4 05:39:24 EDT 2020


New submission from Peter Stokes <linux at dadeos.co.uk>:

Attempting to reuse an instance of 'smtplib.SMTP', via invocation of the 'smtplib.SMTP.connect(…)' function, results in an invalid SMTP command sequence being issued to the connected server:

```
import smtplib

smtp = smtplib.SMTP()
smtp.connect('smtp.gmail.com')
smtp.ehlo_or_helo_if_needed()
smtp.close()
try:
    smtp.quit()
except smtplib.SMTPServerDisconnected:
    pass
smtp.connect('smtp.gmail.com')
print(smtp.mail('user at example.com'))
```

results in "(503, b'5.5.1 EHLO/HELO first. … - gsmtp')"

The values stored in 'self.helo_resp' and 'self.ehlo_resp', as considered within 'smtplib.SMTP.ehlo_or_helo_if_needed()' [1], should be reset to a value of 'None' upon successfully establishing a new connection to a server. The correct sentiment is expressed within 'smtplib.SMTP.quit()' [2] but this resetting of state should be performed upon establishing a new connection rather than termination of an old connection. Whilst the simplified example provided is arguably an invalid use of the 'smtplib.SMTP' library the scenario can occur, specifically when the initial connection is unilaterally terminated by the server, which makes the reset logic provided in 'smtplib.SMTP.quit()' inaccessible due to the 'smtplib.SMTPServerDisconnected' exception raised as a consequence of attempting to execute an SMTP 'QUIT' command over a disconnected socket.

[1] https://github.com/python/cpython/blob/da51ba442c7bf717872633676207c1ae10e99c99/Lib/smtplib.py#L603
[2] https://github.com/python/cpython/blob/da51ba442c7bf717872633676207c1ae10e99c99/Lib/smtplib.py#L989L990

----------
components: email
messages: 374798
nosy: Dadeos, barry, r.david.murray
priority: normal
severity: normal
status: open
title: smtplib.SMTP should reset internal 'helo' and 'ehlo' state within 'connect()'
type: behavior
versions: Python 3.8

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue41470>
_______________________________________


More information about the New-bugs-announce mailing list