-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Anymail: retry sending failed emails
Before this commit, emails that couldn’t be sent were simply discarded, because the code didn’t check the status from anymail. Anymail exposes a status for each recipient of an email, raising many questions on the expected behavior should one fail to be delivered out of multiple recipients. As a first step, simply trust Mailjet API Status field. https://anymail.dev/en/stable/sending/django_email/#refused-recipients https://anymail.dev/en/stable/sending/anymail_additions/#anymail.message.AnymailStatus
- Loading branch information
1 parent
0610f47
commit eee51cd
Showing
5 changed files
with
115 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,8 @@ | ||
import pytest | ||
from django.core.mail.message import EmailMessage | ||
from factory import Faker | ||
|
||
from itou.utils.tasks import AsyncEmailBackend | ||
from itou.utils.tasks import AsyncEmailBackend, _async_send_message | ||
|
||
|
||
class TestAsyncEmailBackend: | ||
|
@@ -28,3 +29,91 @@ def test_send_messages_splits_recipients(self, django_capture_on_commit_callback | |
assert email.from_email == "[email protected]" | ||
assert email.subject == "subject" | ||
assert email.body == "body" | ||
|
||
|
||
@pytest.fixture | ||
def anymail_mailjet_settings(settings): | ||
settings.ASYNC_EMAIL_BACKEND = "anymail.backends.mailjet.EmailBackend" | ||
settings.ANYMAIL = { | ||
"MAILJET_API_URL": settings.ANYMAIL["MAILJET_API_URL"], | ||
"MAILJET_API_KEY": "MAILJET_API_SECRET", | ||
"MAILJET_SECRET_KEY": "MAILJET_SECRET_KEY", | ||
} | ||
return settings | ||
|
||
|
||
class TestAsyncSendMessage: | ||
EXC_TEXT = "Exception: Huey, please retry this task." | ||
|
||
def test_send_ok(self, anymail_mailjet_settings, caplog, requests_mock): | ||
requests_mock.post( | ||
f"{anymail_mailjet_settings.ANYMAIL['MAILJET_API_URL']}send", | ||
# https://dev.mailjet.com/email/guides/send-api-v31/#send-in-bulk | ||
json={ | ||
"Messages": [ | ||
{ | ||
"Status": "success", | ||
"To": [ | ||
{ | ||
"Email": "[email protected]", | ||
"MessageUUID": "124", | ||
"MessageID": 20547681647433001, | ||
"MessageHref": "https://api.mailjet.com/v3/message/20547681647433001", | ||
}, | ||
], | ||
}, | ||
], | ||
}, | ||
) | ||
_async_send_message({"to": ["[email protected]"], "cc": [], "bcc": [], "subject": "Hi", "body": "Hello"}) | ||
assert self.EXC_TEXT not in caplog.text | ||
|
||
def test_raises_on_error(self, anymail_mailjet_settings, caplog, requests_mock): | ||
"""An exception is raised, to make Huey retry the task.""" | ||
requests_mock.post( | ||
f"{anymail_mailjet_settings.ANYMAIL['MAILJET_API_URL']}send", | ||
# https://dev.mailjet.com/email/guides/send-api-v31/#send-in-bulk | ||
json={ | ||
"Messages": [ | ||
{ | ||
"Errors": [ | ||
{ | ||
"ErrorIdentifier": "88b5ca9f-5f1f-42e7-a45e-9ecbad0c285e", | ||
"ErrorCode": "send-0003", | ||
"StatusCode": 400, | ||
"ErrorMessage": 'At least "HTMLPart", "TextPart" or "TemplateID" must be provided.', | ||
"ErrorRelatedTo": ["HTMLPart", "TextPart"], | ||
}, | ||
], | ||
"Status": "error", | ||
}, | ||
], | ||
}, | ||
) | ||
_async_send_message({"to": ["[email protected]"], "cc": [], "bcc": [], "subject": "Hi", "body": "Hello"}) | ||
assert self.EXC_TEXT in caplog.text | ||
|
||
def test_logs_to_sentry_after_using_all_retries(self, anymail_mailjet_settings, caplog, requests_mock, mocker): | ||
error_payload = { | ||
"Errors": [ | ||
{ | ||
"ErrorIdentifier": "88b5ca9f-5f1f-42e7-a45e-9ecbad0c285e", | ||
"ErrorCode": "send-0003", | ||
"StatusCode": 400, | ||
"ErrorMessage": 'At least "HTMLPart", "TextPart" or "TemplateID" must be provided.', | ||
"ErrorRelatedTo": ["HTMLPart", "TextPart"], | ||
}, | ||
], | ||
"Status": "error", | ||
} | ||
requests_mock.post( | ||
f"{anymail_mailjet_settings.ANYMAIL['MAILJET_API_URL']}send", | ||
# https://dev.mailjet.com/email/guides/send-api-v31/#send-in-bulk | ||
json={"Messages": [error_payload]}, | ||
) | ||
sentry_mock = mocker.patch("itou.utils.tasks.sentry_sdk.capture_message") | ||
_async_send_message( | ||
{"to": ["[email protected]"], "cc": [], "bcc": [], "subject": "Hi", "body": "Hello"}, retries=0 | ||
) | ||
sentry_mock.assert_called_once_with(f"Could not send email: {error_payload}", "error") | ||
assert self.EXC_TEXT not in caplog.text |