[New-bugs-announce] [issue5713] smtplib gets out of sync if server returns a 421 status

Mark Sapiro report at bugs.python.org
Mon Apr 6 22:30:25 CEST 2009

New submission from Mark Sapiro <mark at msapiro.net>:

RFC821 upon which smtplib was originally based does not define a 421
status code and implies the server should only disconnect in response to
a QUIT command.

Subsequent extensions in RFC2821 (and now RFC5321) define situations
under which the server may return a 421 status and disconnect. This
leads to the following problem.

An smtplib.SMTP() instance is created and its sendmail() method is
called with a list of recipients which contains several invalid, local
addresses. sendmail() processes the recipient list, calling the rcpt()
method for each. Some of these may be accepted with a 250 or 251 status
and some may be rejected with a 550 or other status. The rejects are
kept in a dictionary to be eventually returned as the sendmail() result.

However, with the Postfix server at least, after 20 rejects, the server
sends a 421 Too many errors reply and disconnects, but sendmail
continues to process and this results in raising
SMTPServerDisconnected("Connection unexpectedly closed") and the
response dictionary containing the invalid addresses and their responses
is lost.

The caller may see the exception as retryable and may retry the send
after some delay, but since the caller has received no information about
the invalid addresses, it sends the same recipient list and the scenario

components: Library (Lib)
messages: 85666
nosy: msapiro
severity: normal
status: open
title: smtplib gets out of sync if server returns a 421 status
type: behavior
versions: Python 2.4, Python 2.5, Python 2.6

Python tracker <report at bugs.python.org>

More information about the New-bugs-announce mailing list