Skip to content

Commit 39844d6

Browse files
feat: Support for AMP HTML Email (#945)
1 parent 6807622 commit 39844d6

File tree

7 files changed

+454
-5
lines changed

7 files changed

+454
-5
lines changed

sendgrid/helpers/mail/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
from .groups_to_display import GroupsToDisplay
2828
from .header import Header
2929
from .html_content import HtmlContent
30+
from .amp_html_content import AmpHtmlContent
3031
from .ip_pool_name import IpPoolName
3132
from .mail_settings import MailSettings
3233
from .mail import Mail
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
from .content import Content
2+
from .validators import ValidateApiKey
3+
4+
5+
class AmpHtmlContent(Content):
6+
"""AMP HTML content to be included in your email."""
7+
8+
def __init__(self, content):
9+
"""Create an AMP HTML Content with the specified MIME type and content.
10+
11+
:param content: The AMP HTML content.
12+
:type content: string
13+
"""
14+
self._content = None
15+
self._validator = ValidateApiKey()
16+
17+
if content is not None:
18+
self.content = content
19+
20+
@property
21+
def mime_type(self):
22+
"""The MIME type for AMP HTML content.
23+
24+
:rtype: string
25+
"""
26+
return "text/x-amp-html"
27+
28+
@property
29+
def content(self):
30+
"""The actual AMP HTML content.
31+
32+
:rtype: string
33+
"""
34+
return self._content
35+
36+
@content.setter
37+
def content(self, value):
38+
"""The actual AMP HTML content.
39+
40+
:param value: The actual AMP HTML content.
41+
:type value: string
42+
"""
43+
self._validator.validate_message_dict(value)
44+
self._content = value
45+
46+
def get(self):
47+
"""
48+
Get a JSON-ready representation of this AmpContent.
49+
50+
:returns: This AmpContent, ready for use in a request body.
51+
:rtype: dict
52+
"""
53+
content = {}
54+
if self.mime_type is not None:
55+
content["type"] = self.mime_type
56+
57+
if self.content is not None:
58+
content["value"] = self.content
59+
return content

sendgrid/helpers/mail/content.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def __init__(self, mime_type, content):
2929
@property
3030
def mime_type(self):
3131
"""The MIME type of the content you are including in your email.
32-
For example, "text/plain" or "text/html".
32+
For example, "text/plain" or "text/html" or "text/x-amp-html".
3333
3434
:rtype: string
3535
"""
@@ -38,11 +38,11 @@ def mime_type(self):
3838
@mime_type.setter
3939
def mime_type(self, value):
4040
"""The MIME type of the content you are including in your email.
41-
For example, "text/plain" or "text/html".
41+
For example, "text/plain" or "text/html" or "text/x-amp-html".
4242
4343
:param value: The MIME type of the content you are including in your
4444
email.
45-
For example, "text/plain" or "text/html".
45+
For example, "text/plain" or "text/html" or "text/x-amp-html".
4646
:type value: string
4747
"""
4848
self._mime_type = value

sendgrid/helpers/mail/mail.py

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ def __init__(
2727
subject=None,
2828
plain_text_content=None,
2929
html_content=None,
30+
amp_html_content=None,
3031
global_substitutions=None,
3132
is_multiple=False):
3233
"""
@@ -43,6 +44,8 @@ def __init__(
4344
:type plain_text_content: string, optional
4445
:param html_content: The html body of the email
4546
:type html_content: string, optional
47+
:param amp_html_content: The amp-html body of the email
48+
:type amp_html_content: string, optional
4649
"""
4750
self._attachments = None
4851
self._categories = None
@@ -71,6 +74,8 @@ def __init__(
7174
self.subject = subject
7275
if plain_text_content is not None:
7376
self.add_content(plain_text_content, MimeType.text)
77+
if amp_html_content is not None:
78+
self.add_content(amp_html_content, MimeType.amp)
7479
if html_content is not None:
7580
self.add_content(html_content, MimeType.html)
7681

@@ -725,9 +730,23 @@ def add_content(self, content, mime_type=None):
725730
"""
726731
if isinstance(content, str):
727732
content = Content(mime_type, content)
728-
# Content of mime type text/plain must always come first
729-
if content.mime_type == "text/plain":
733+
# Content of mime type text/plain must always come first, followed by text/x-amp-html and then text/html
734+
if content.mime_type == MimeType.text:
730735
self._contents = self._ensure_insert(content, self._contents)
736+
elif content.mime_type == MimeType.amp:
737+
if self._contents:
738+
for _content in self._contents:
739+
# this is written in the context that plain text content will always come earlier than the html content
740+
if _content.mime_type == MimeType.text:
741+
index = 1
742+
break
743+
elif _content.mime_type == MimeType.html:
744+
index = 0
745+
break
746+
else:
747+
index = 0
748+
self._contents = self._ensure_append(
749+
content, self._contents, index=index)
731750
else:
732751
if self._contents:
733752
index = len(self._contents)

sendgrid/helpers/mail/mime_type.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ class MimeType(object):
33
"""
44
text = "text/plain"
55
html = "text/html"
6+
amp = "text/x-amp-html"

0 commit comments

Comments
 (0)