Fixed #30058 -- Made SMTP EmailBackend.send_messages() return 0 for empty/error cases.

This commit is contained in:
Denis Stebunov 2018-12-23 03:06:56 +02:00 committed by Tim Graham
parent 0b54ab0675
commit 277de22984
2 changed files with 8 additions and 3 deletions

View File

@ -98,13 +98,13 @@ class EmailBackend(BaseEmailBackend):
messages sent. messages sent.
""" """
if not email_messages: if not email_messages:
return return 0
with self._lock: with self._lock:
new_conn_created = self.open() new_conn_created = self.open()
if not self.connection or new_conn_created is None: if not self.connection or new_conn_created is None:
# We failed silently on open(). # We failed silently on open().
# Trying to send would be pointless. # Trying to send would be pointless.
return return 0
num_sent = 0 num_sent = 0
for message in email_messages: for message in email_messages:
sent = self._send(message) sent = self._send(message)

View File

@ -1527,7 +1527,12 @@ class SMTPBackendTests(BaseEmailBackendTests, SMTPBackendTestsBase):
backend.connection = True backend.connection = True
backend.open = lambda: None backend.open = lambda: None
email = EmailMessage('Subject', 'Content', 'from@example.com', ['to@example.com']) email = EmailMessage('Subject', 'Content', 'from@example.com', ['to@example.com'])
self.assertEqual(backend.send_messages([email]), None) self.assertEqual(backend.send_messages([email]), 0)
def test_send_messages_empty_list(self):
backend = smtp.EmailBackend()
backend.connection = True
self.assertEqual(backend.send_messages([]), 0)
def test_send_messages_zero_sent(self): def test_send_messages_zero_sent(self):
"""A message isn't sent if it doesn't have any recipients.""" """A message isn't sent if it doesn't have any recipients."""