Skip to content

Commit

Permalink
feat: Support for AMP HTML Email (#945)
Browse files Browse the repository at this point in the history
  • Loading branch information
modernwarfareuplink authored Jan 8, 2021
1 parent 6807622 commit 39844d6
Show file tree
Hide file tree
Showing 7 changed files with 454 additions and 5 deletions.
1 change: 1 addition & 0 deletions sendgrid/helpers/mail/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
from .groups_to_display import GroupsToDisplay
from .header import Header
from .html_content import HtmlContent
from .amp_html_content import AmpHtmlContent
from .ip_pool_name import IpPoolName
from .mail_settings import MailSettings
from .mail import Mail
Expand Down
59 changes: 59 additions & 0 deletions sendgrid/helpers/mail/amp_html_content.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
from .content import Content
from .validators import ValidateApiKey


class AmpHtmlContent(Content):
"""AMP HTML content to be included in your email."""

def __init__(self, content):
"""Create an AMP HTML Content with the specified MIME type and content.
:param content: The AMP HTML content.
:type content: string
"""
self._content = None
self._validator = ValidateApiKey()

if content is not None:
self.content = content

@property
def mime_type(self):
"""The MIME type for AMP HTML content.
:rtype: string
"""
return "text/x-amp-html"

@property
def content(self):
"""The actual AMP HTML content.
:rtype: string
"""
return self._content

@content.setter
def content(self, value):
"""The actual AMP HTML content.
:param value: The actual AMP HTML content.
:type value: string
"""
self._validator.validate_message_dict(value)
self._content = value

def get(self):
"""
Get a JSON-ready representation of this AmpContent.
:returns: This AmpContent, ready for use in a request body.
:rtype: dict
"""
content = {}
if self.mime_type is not None:
content["type"] = self.mime_type

if self.content is not None:
content["value"] = self.content
return content
6 changes: 3 additions & 3 deletions sendgrid/helpers/mail/content.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def __init__(self, mime_type, content):
@property
def mime_type(self):
"""The MIME type of the content you are including in your email.
For example, "text/plain" or "text/html".
For example, "text/plain" or "text/html" or "text/x-amp-html".
:rtype: string
"""
Expand All @@ -38,11 +38,11 @@ def mime_type(self):
@mime_type.setter
def mime_type(self, value):
"""The MIME type of the content you are including in your email.
For example, "text/plain" or "text/html".
For example, "text/plain" or "text/html" or "text/x-amp-html".
:param value: The MIME type of the content you are including in your
email.
For example, "text/plain" or "text/html".
For example, "text/plain" or "text/html" or "text/x-amp-html".
:type value: string
"""
self._mime_type = value
Expand Down
23 changes: 21 additions & 2 deletions sendgrid/helpers/mail/mail.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ def __init__(
subject=None,
plain_text_content=None,
html_content=None,
amp_html_content=None,
global_substitutions=None,
is_multiple=False):
"""
Expand All @@ -43,6 +44,8 @@ def __init__(
:type plain_text_content: string, optional
:param html_content: The html body of the email
:type html_content: string, optional
:param amp_html_content: The amp-html body of the email
:type amp_html_content: string, optional
"""
self._attachments = None
self._categories = None
Expand Down Expand Up @@ -71,6 +74,8 @@ def __init__(
self.subject = subject
if plain_text_content is not None:
self.add_content(plain_text_content, MimeType.text)
if amp_html_content is not None:
self.add_content(amp_html_content, MimeType.amp)
if html_content is not None:
self.add_content(html_content, MimeType.html)

Expand Down Expand Up @@ -725,9 +730,23 @@ def add_content(self, content, mime_type=None):
"""
if isinstance(content, str):
content = Content(mime_type, content)
# Content of mime type text/plain must always come first
if content.mime_type == "text/plain":
# Content of mime type text/plain must always come first, followed by text/x-amp-html and then text/html
if content.mime_type == MimeType.text:
self._contents = self._ensure_insert(content, self._contents)
elif content.mime_type == MimeType.amp:
if self._contents:
for _content in self._contents:
# this is written in the context that plain text content will always come earlier than the html content
if _content.mime_type == MimeType.text:
index = 1
break
elif _content.mime_type == MimeType.html:
index = 0
break
else:
index = 0
self._contents = self._ensure_append(
content, self._contents, index=index)
else:
if self._contents:
index = len(self._contents)
Expand Down
1 change: 1 addition & 0 deletions sendgrid/helpers/mail/mime_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ class MimeType(object):
"""
text = "text/plain"
html = "text/html"
amp = "text/x-amp-html"
Loading

0 comments on commit 39844d6

Please sign in to comment.