This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: smtplib.SMTP should reset internal 'helo' and 'ehlo' state within 'connect()'
Type: behavior Stage:
Components: email Versions: Python 3.8
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: Dadeos, Jeffrey.Kintscher, barry, r.david.murray
Priority: normal Keywords:

Created on 2020-08-04 09:39 by Dadeos, last changed 2022-04-11 14:59 by admin.

Messages (1)
msg374798 - (view) Author: Peter Stokes (Dadeos) Date: 2020-08-04 09:39
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@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
History
Date User Action Args
2022-04-11 14:59:34adminsetgithub: 85642
2020-08-05 23:25:36Jeffrey.Kintschersetnosy: + Jeffrey.Kintscher
2020-08-04 09:39:24Dadeoscreate