diff --git a/postmark/django_backend.py b/postmark/django_backend.py index 593dc43..481528c 100644 --- a/postmark/django_backend.py +++ b/postmark/django_backend.py @@ -156,14 +156,14 @@ def _send(self, messages): if to_send is False: # The message was missing recipients. # Bail. - return False + return False, None else: pm_messages = list(map(self._build_message, messages)) pm_messages = [m for m in pm_messages if m] if len(pm_messages) == 0: # If after filtering, there aren't any messages # to send, bail. - return False + return False, None to_send = PMBatchMail(messages=pm_messages) try: to_send.send(test=self.test_mode) diff --git a/tests.py b/tests.py index c1f5042..2f16dac 100644 --- a/tests.py +++ b/tests.py @@ -342,6 +342,39 @@ def test_message_count_batch(self): sent_messages = connection.send_messages([message1, message2]) self.assertEqual(sent_messages, 2) + def test_send_messages_nothing_to_send_single(self): + """Make sure no errors when send results in zero messages.""" + with self.settings(POSTMARK_RETURN_MESSAGE_ID=False): + message1 = EmailMessage( + connection=EmailBackend(api_key='dummy'), + from_email='from@test.com', to=[], subject='html test', body='hello there' + ) + + with mock.patch('postmark.core.urlopen') as transport: + # Directly send bulk mail via django + connection = mail.get_connection() + sent_messages = connection.send_messages([message1]) + self.assertEqual(0, sent_messages) + + def test_send_messages_nothing_to_send_double(self): + """Make sure no errors when send results in zero messages.""" + with self.settings(POSTMARK_RETURN_MESSAGE_ID=False): + message1 = EmailMessage( + connection=EmailBackend(api_key='dummy'), + from_email='from@test.com', to=[], subject='html test', body='hello there' + ) + + message2 = EmailMessage( + connection=EmailBackend(api_key='dummy'), + from_email='from@test.com', to=[], subject='html test', body='hello there' + ) + + with mock.patch('postmark.core.urlopen') as transport: + # Directly send bulk mail via django + connection = mail.get_connection() + sent_messages = connection.send_messages([message1, message2]) + self.assertEqual(0, sent_messages) + def test_message_id_single(self): """Test backend returns message sending single message with setting True""" with self.settings(POSTMARK_RETURN_MESSAGE_ID=True): @@ -415,7 +448,7 @@ def test_send_attachment_bytes(self): with mock.patch('postmark.core.urlopen', side_effect=HTTPError('', 200, '', {}, None)): message.send() - + def test_message_stream(self): message = EmailMultiAlternatives( connection=EmailBackend(api_key='dummy'),