New submission from Peter Stokes <li...@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('u...@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 <rep...@bugs.python.org>
<https://bugs.python.org/issue41470>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to