You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
smtplib doesn't clear helo/ehlo flags on quit #48392
SMTP.login() and SMTP.sendmail set one of the attributes ehlo_resp or
helo_resp to whatever the server responded (only if they're not already
set).
SMTP.quit() doesn't clear these attributes, so on the next connection,
the HELO/EHLO commands won't be sent; this will cause problems with some
servers (MDaemon in my case). The fix is obvious, and a workaround
would be to clear the attributes in the instance before calling login
(or sendmail where authentication isn't needed).
I can confirm that this issue is still present in Python 2.5.2, 2.6.1
and 3.0.1.
The current behavior of smtplib is a clear violation of the SMTP
specification. The problem can be reproduced with code like that (stub,
pseudo code-like):
smtp = smtplib.SMTP()
smtp.sendmail('from@example.com', 'foo@example.com', msg)
smtp.quit()
smtp.connect()
# This command does not send EHLO/HELO again, violating the spec!
smtp.sendmail('from@example.com', 'foo@example.com', msg)
IIUC, the bug only occurs if you use the same SMTP object for multiple
connections. I would claim that this is a bug in the application: SMTP
objects are not designed to be used for multiple connections. You need
to create a new one for each connection.
If you want to extend the SMTP class to work for multiple connections,
the provided patch is incorrect. Resetting the attributes should not be
done in .connect() (i.e. for the new connection), but in .close() (i.e.
when shutting down the previous connection). Furthermore, it should not
only reset the two response attributes, but all per-connection
attributes (which includes e.g. esmtp_features), and a test should be
provided that, after close, only known "good" attributes remain set on
the object (e.g. debuglevel).
dwig commented on Feb 17, 2009
dwigmannequin
MannequinAuthor
More actions
Re: "SMTP
objects are not designed to be used for multiple connections" ... "If
you want to extend the SMTP class to work for multiple connections".
Carefully parsing the library reference documentation, I can see that it
could be interpreted that way: "A SMTP instance encapsulates _an_ SMTP
connection" (emphasis added). I'd recommend making the documentation
clearer in this regard, whether the implementation is extended or not.
(And if non-reusability is intended, it'd be even more helpful if the
connect() method were to throw a clear exception if called again.)
loewis commented on Feb 17, 2009
loewismannequin
Mannequin
More actions
It's neither deliberately designed to work for a single connection only,
nor designed to support multiple of them. I guess (and only guess) that
the authors of the module never considered your usage.
So if you want to see something done, please contribute patches. I'm
personally neutral which way they should go (and don't feel motivated to
write patches myself - I can live with the status quo).
Of course, any patch would set a precedent, and httplib and ftplib would
then probably need to follow (requiring contributors then also).
Hello Felix/Don,
are you still interested in seeing this fixed in smtplib? If so, what about incorporating Martin's suggestions and propose a new patch for review?
dwig commented on Feb 8, 2011
dwigmannequin
MannequinAuthor
More actions
I have no pressing need for it. I'm happy enough with Martin's idea of single-use instances, as long as they're clearly documented as such, and don't "pretend" to work for a subsequent connect. I'd also be happy with instances that could be used for multiple connections, as long as that didn't increase the complexity of the code too much.
Martin's also right that the similar clients for http and ftp, as well as smtp, should all work the same.
varun commented on Mar 9, 2014
varunmannequin
Mannequin
More actions
I have added a patch and it's test case as per martin's suggestions. Now function close() resets the attributes :sock, default_port, ehlo_msg, ehlo_resp, helo_resp, source_address
But it does *not* reset : debuglevel, does_esmtp,esmtp_features, file, local_hostname
I think the attributes does_esmtp, esmtp_features and local_hostname are most probably not going to be changed even if someone uses the same object more than once.
dwig commented on Mar 10, 2014
dwigmannequin
MannequinAuthor
More actions
Varun, thanks for the patch; sounds like the right way to go, in that it should work whether the usage is single-connection or multiple-connection. Ideally, the documentation would be expanded a bit to clarify just which attributes get reset on close().
The reported case (quit, connect, then sendmail with no new EHLO) was fixed in 3.4.2 by gh-66412. test_quit_resets_greeting in test_smtplib covers it.
The state still survives when quit() raises, or when the session ends in close().
I will open a separate issue for that, with a PR once I am under the PR limit.
I fixed it in #161267 instead of a separate issue.
The reset moves from quit() into close(), which quit(), the with statement and the disconnect paths all call, so a an SMTP object sends EHLO again on reconnect.
Activity
dwig commentedon Oct 17, 2008
Found on Windows, running Python 2.4.3.
SMTP.login() and SMTP.sendmail set one of the attributes ehlo_resp or
helo_resp to whatever the server responded (only if they're not already
set).
SMTP.quit() doesn't clear these attributes, so on the next connection,
the HELO/EHLO commands won't be sent; this will cause problems with some
servers (MDaemon in my case). The fix is obvious, and a workaround
would be to clear the attributes in the instance before calling login
(or sendmail where authentication isn't needed).
FelixSchwarz commentedon Feb 15, 2009
I can confirm that this issue is still present in Python 2.5.2, 2.6.1
and 3.0.1.
The current behavior of smtplib is a clear violation of the SMTP
specification. The problem can be reproduced with code like that (stub,
pseudo code-like):
smtp = smtplib.SMTP()
smtp.sendmail('from@example.com', 'foo@example.com', msg)
smtp.quit()
smtp.connect()
# This command does not send EHLO/HELO again, violating the spec!
smtp.sendmail('from@example.com', 'foo@example.com', msg)
Real world bug reports due to this behavior were mostly visible with
Exim because this MTA rejects the 'MAIL FROM' if SMTP extension verbs
are used without EHLO:
*
http://groups.google.com/group/turbomail-devel/browse_thread/thread/a25c89a94b42724f
loewis commentedon Feb 15, 2009
IIUC, the bug only occurs if you use the same SMTP object for multiple
connections. I would claim that this is a bug in the application: SMTP
objects are not designed to be used for multiple connections. You need
to create a new one for each connection.
If you want to extend the SMTP class to work for multiple connections,
the provided patch is incorrect. Resetting the attributes should not be
done in .connect() (i.e. for the new connection), but in .close() (i.e.
when shutting down the previous connection). Furthermore, it should not
only reset the two response attributes, but all per-connection
attributes (which includes e.g. esmtp_features), and a test should be
provided that, after close, only known "good" attributes remain set on
the object (e.g. debuglevel).
dwig commentedon Feb 17, 2009
Re: "SMTP
objects are not designed to be used for multiple connections" ... "If
you want to extend the SMTP class to work for multiple connections".
Carefully parsing the library reference documentation, I can see that it
could be interpreted that way: "A SMTP instance encapsulates _an_ SMTP
connection" (emphasis added). I'd recommend making the documentation
clearer in this regard, whether the implementation is extended or not.
(And if non-reusability is intended, it'd be even more helpful if the
connect() method were to throw a clear exception if called again.)
loewis commentedon Feb 17, 2009
It's neither deliberately designed to work for a single connection only,
nor designed to support multiple of them. I guess (and only guess) that
the authors of the module never considered your usage.
So if you want to see something done, please contribute patches. I'm
personally neutral which way they should go (and don't feel motivated to
write patches myself - I can live with the status quo).
Of course, any patch would set a precedent, and httplib and ftplib would
then probably need to follow (requiring contributors then also).
jcea commentedon Mar 24, 2010
Support for smtpd too? (a stdlib addition in 2.7/3.2)
sandrotosi commentedon Feb 7, 2011
Hello Felix/Don,
are you still interested in seeing this fixed in smtplib? If so, what about incorporating Martin's suggestions and propose a new patch for review?
dwig commentedon Feb 8, 2011
I have no pressing need for it. I'm happy enough with Martin's idea of single-use instances, as long as they're clearly documented as such, and don't "pretend" to work for a subsequent connect. I'd also be happy with instances that could be used for multiple connections, as long as that didn't increase the complexity of the code too much.
Martin's also right that the similar clients for http and ftp, as well as smtp, should all work the same.
varun commentedon Mar 9, 2014
I have added a patch and it's test case as per martin's suggestions. Now function close() resets the attributes :sock, default_port, ehlo_msg, ehlo_resp, helo_resp, source_address
But it does *not* reset : debuglevel, does_esmtp,esmtp_features, file, local_hostname
I think the attributes does_esmtp, esmtp_features and local_hostname are most probably not going to be changed even if someone uses the same object more than once.
dwig commentedon Mar 10, 2014
Varun, thanks for the patch; sounds like the right way to go, in that it should work whether the usage is single-connection or multiple-connection. Ideally, the documentation would be expanded a bit to clarify just which attributes get reset on close().
vasiliyk commentedon Oct 29, 2026
The reported case (quit, connect, then sendmail with no new EHLO) was fixed in 3.4.2 by gh-66412.
test_quit_resets_greetingin test_smtplib covers it.The state still survives when
quit()raises, or when the session ends inclose().I will open a separate issue for that, with a PR once I am under the PR limit.
vasiliyk commentedon Nov 3, 2026
I fixed it in #161267 instead of a separate issue.
The reset moves from
quit()intoclose(), whichquit(), thewithstatement and the disconnect paths all call, so a anSMTPobject sends EHLO again on reconnect.pythongh-48392: Do not link SMTP.close() in NEWS