Skip to content

Commit d1ef61d

Browse files
committed
Mailgun: add AMPHTML support
1 parent d33c9ea commit d1ef61d

File tree

4 files changed

+30
-0
lines changed

4 files changed

+30
-0
lines changed

CHANGELOG.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ vNext
3030

3131
*Unreleased changes*
3232

33+
Features
34+
~~~~~~~~
35+
36+
* **Mailgun:** Add support for AMP for Email
37+
(via ``message.attach_alternative(..., "text/x-amp-html")``).
38+
3339
Fixes
3440
~~~~~
3541

anymail/backends/mailgun.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,14 @@ def set_html_body(self, body):
310310
self.unsupported_feature("multiple html parts")
311311
self.data["html"] = body
312312

313+
def add_alternative(self, content, mimetype):
314+
if mimetype.lower() == "text/x-amp-html":
315+
if "amp-html" in self.data:
316+
self.unsupported_feature("multiple html parts")
317+
self.data["amp-html"] = content
318+
else:
319+
super().add_alternative(content, mimetype)
320+
313321
def add_attachment(self, attachment):
314322
# http://docs.python-requests.org/en/v2.4.3/user/advanced/#post-multiple-multipart-encoded-files
315323
if attachment.inline:

docs/esps/mailgun.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,13 @@ Limitations and quirks
237237
Your tracking webhooks will receive metadata values (either that you provided or the
238238
default empty string) for *every* key used with *any* recipient in the send.
239239

240+
**AMP for Email**
241+
Mailgun supports sending AMPHTML email content. To include it, use
242+
``message.attach_alternative("...AMPHTML content...", "text/x-amp-html")``
243+
(and be sure to also include regular HTML and/or text bodies, too).
244+
245+
.. versionadded:: 8.2
246+
240247

241248
.. _undocumented API requirement:
242249
https://mailgun.uservoice.com/forums/156243-feature-requests/suggestions/35668606

tests/test_mailgun_backend.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,15 @@ def test_html_alternative(self):
301301
with self.assertRaises(AnymailUnsupportedFeature):
302302
self.message.send()
303303

304+
def test_amp_html_alternative(self):
305+
# Mailgun *does* support text/x-amp-html alongside text/html
306+
self.message.attach_alternative("<p>HTML</p>", "text/html")
307+
self.message.attach_alternative("<p>And AMP HTML</p>", "text/x-amp-html")
308+
self.message.send()
309+
data = self.get_api_call_data()
310+
self.assertEqual(data["html"], "<p>HTML</p>")
311+
self.assertEqual(data["amp-html"], "<p>And AMP HTML</p>")
312+
304313
def test_alternatives_fail_silently(self):
305314
# Make sure fail_silently is respected
306315
self.message.attach_alternative("{'not': 'allowed'}", "application/json")

0 commit comments

Comments
 (0)