Skip to content
Merged
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
4 changes: 2 additions & 2 deletions postmark/django_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I send three messages, but one of the messages doesn't have a recipient, then at this stage the pm_messages list will have only two elements.

How is user-code supposed to figure out which of the three messages didn't get send? That is, after this point, there's no difference between:

connection.send_messages([wrong_message, correct_message1, correct_message2])
connection.send_messages([correct_message1, wrong_message, correct_message2])
connection.send_messages([correct_message1, correct_message2, wrong_message])

Would be great if the return value could then be

[None, message_id1, message_id2]
[message_id1, None, message_id2]
[message_id1, message_id2, None]

in these cases?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call. Will track via #108

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)
Expand Down
35 changes: 34 additions & 1 deletion tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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='<b>hello</b> 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='<b>hello</b> there'
)

message2 = EmailMessage(
connection=EmailBackend(api_key='dummy'),
from_email='from@test.com', to=[], subject='html test', body='<b>hello</b> 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):
Expand Down Expand Up @@ -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'),
Expand Down