diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 276b1f4..ab96fd2 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -30,6 +30,12 @@ vNext *Unreleased changes* +Features +~~~~~~~~ + +* **Mailgun:** Add support for AMP for Email + (via ``message.attach_alternative(..., "text/x-amp-html")``). + Fixes ~~~~~ diff --git a/anymail/backends/mailgun.py b/anymail/backends/mailgun.py index 76ddabc..914b864 100644 --- a/anymail/backends/mailgun.py +++ b/anymail/backends/mailgun.py @@ -310,6 +310,14 @@ class MailgunPayload(RequestsPayload): self.unsupported_feature("multiple html parts") self.data["html"] = body + def add_alternative(self, content, mimetype): + if mimetype.lower() == "text/x-amp-html": + if "amp-html" in self.data: + self.unsupported_feature("multiple html parts") + self.data["amp-html"] = content + else: + super().add_alternative(content, mimetype) + def add_attachment(self, attachment): # http://docs.python-requests.org/en/v2.4.3/user/advanced/#post-multiple-multipart-encoded-files if attachment.inline: diff --git a/docs/esps/mailgun.rst b/docs/esps/mailgun.rst index d771fa0..5282a46 100644 --- a/docs/esps/mailgun.rst +++ b/docs/esps/mailgun.rst @@ -237,6 +237,13 @@ Limitations and quirks Your tracking webhooks will receive metadata values (either that you provided or the default empty string) for *every* key used with *any* recipient in the send. +**AMP for Email** + Mailgun supports sending AMPHTML email content. To include it, use + ``message.attach_alternative("...AMPHTML content...", "text/x-amp-html")`` + (and be sure to also include regular HTML and/or text bodies, too). + + .. versionadded:: 8.2 + .. _undocumented API requirement: https://mailgun.uservoice.com/forums/156243-feature-requests/suggestions/35668606 diff --git a/tests/test_mailgun_backend.py b/tests/test_mailgun_backend.py index 856519b..408007e 100644 --- a/tests/test_mailgun_backend.py +++ b/tests/test_mailgun_backend.py @@ -301,6 +301,15 @@ class MailgunBackendStandardEmailTests(MailgunBackendMockAPITestCase): with self.assertRaises(AnymailUnsupportedFeature): self.message.send() + def test_amp_html_alternative(self): + # Mailgun *does* support text/x-amp-html alongside text/html + self.message.attach_alternative("
HTML
", "text/html") + self.message.attach_alternative("And AMP HTML
", "text/x-amp-html") + self.message.send() + data = self.get_api_call_data() + self.assertEqual(data["html"], "HTML
") + self.assertEqual(data["amp-html"], "And AMP HTML
") + def test_alternatives_fail_silently(self): # Make sure fail_silently is respected self.message.attach_alternative("{'not': 'allowed'}", "application/json")