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
10 changes: 8 additions & 2 deletions framework/email/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,12 @@ def _send_with_sendgrid(
# Personalization to handle To, CC, and BCC sendgrid client concept
personalization = Personalization()

personalization.add_to(To(to_addr))
to = To(to_addr)
if to.email is None:
sentry.log_message(f"Receiver email is not valid: {to_addr}. {subject} email won't be sent.")
return False

personalization.add_to(to)

if cc_addr:
if isinstance(cc_addr, str):
Expand All @@ -191,7 +196,8 @@ def _send_with_sendgrid(
mail.add_personalization(personalization)

if categories:
mail.add_category([Category(x) for x in categories])
for category in categories:
mail.add_category(Category(category))

if attachment_name and attachment_content:
attachment = Attachment(
Expand Down
25 changes: 19 additions & 6 deletions tests/framework_tests/test_email.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,14 @@ def test_send_with_sendgrid_success(self, mock_mail: MagicMock):
mock_mail.return_value.add_personalization.assert_called_once()

# Capture the categories added via add_category
mock_mail.return_value.add_category.assert_called_once()
added_categories = mock_mail.return_value.add_category.call_args.args[0]
# mock_mail.return_value.add_category.assert_called_once()
assert mock_mail.return_value.add_category.call_count == 2
added_categories = mock_mail.return_value.add_category.call_args_list
assert len(added_categories) == 2
assert isinstance(added_categories[0], Category)
assert isinstance(added_categories[1], Category)
assert added_categories[0].get() == category1
assert added_categories[1].get() == category2
assert isinstance(added_categories[0].args[0], Category)
assert isinstance(added_categories[1].args[0], Category)
assert added_categories[0].args[0].get() == category1
assert added_categories[1].args[0].get() == category2

mock_client.send.assert_called_once_with(mock_mail.return_value)

Expand Down Expand Up @@ -103,6 +104,18 @@ def test_send_with_sendgrid_failure_returns_false(self, mock_mail, sentry_mock):

mock_client.send.assert_called_once_with(mock_mail.return_value)

mock_client.send.return_value = mock.Mock(status_code=200, body='correct')
to_addr = 'not-an-email'
ret = _send_with_sendgrid(
from_addr=from_addr,
to_addr=to_addr,
subject=subject,
message=message,
client=mock_client
)
assert not ret
sentry_mock.assert_called()


if __name__ == '__main__':
unittest.main()