Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion Lib/smtplib.py
Original file line number Diff line number Diff line change
Expand Up @@ -886,7 +886,14 @@ def sendmail(self, from_addr, to_addrs, msg, mail_options=(),
# the server refused all our recipients
self._rset()
raise SMTPRecipientsRefused(senderrs)
(code, resp) = self.data(msg)
try:
(code, resp) = self.data(msg)
except SMTPDataError as ex:
if ex.smtp_code == 421:
self.close()
else:
self._rset()
raise
if code != 250:
if code == 421:
self.close()
Expand Down
14 changes: 14 additions & 0 deletions Lib/test/test_smtplib.py
Original file line number Diff line number Diff line change
Expand Up @@ -1259,6 +1259,10 @@ def test__rest_from_mail_cmd(self):
smtp.sendmail('John', 'Sally', 'test message')
self.assertIsNone(smtp.sock)

# The following 421 response tests for sendmail ensure that sendmail handles
# 421 respones correctly by closing the connection. sendmail has to take
# care of this, as it wraps a mail transaction for users.

# Issue 5713: make sure close, not rset, is called if we get a 421 error
def test_421_from_mail_cmd(self):
smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost',
Expand Down Expand Up @@ -1297,6 +1301,16 @@ def found_terminator(self):
self.assertIsNone(smtp.sock)
self.assertEqual(self.serv._SMTPchannel.rcpt_count, 0)

def test_421_during_data_cmd(self):
smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost',
timeout=support.LOOPBACK_TIMEOUT)
smtp.noop()
self.serv._SMTPchannel.data_response = '421 closing'
with self.assertRaises(smtplib.SMTPDataError):
smtp.sendmail('[email protected]', ['[email protected]'], 'test message')
self.assertIsNone(smtp.sock)
self.assertEqual(self.serv._SMTPchannel.rcpt_count, 0)

def test_smtputf8_NotSupportedError_if_no_server_support(self):
smtp = smtplib.SMTP(
HOST, self.port, local_hostname='localhost',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Improve handling of 421 SMTP responses in :meth:`smtplib.SMTP.sendmail` by
covering a previously missing case in which 421 occurs while sending a DATA
command.
Loading