Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes Issue 451 #454

Merged
merged 3 commits into from
May 30, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 8 additions & 1 deletion sendgrid/helpers/mail/mail.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,14 @@ def add_content(self, content):

:type content: Content
"""
self._contents.append(content)
if self._contents is None:
self._contents = []

# Text content should be before HTML content
if content._type == "text/plain":
self._contents.insert(0, content)
else:
self._contents.append(content)

@property
def attachments(self):
Expand Down
33 changes: 33 additions & 0 deletions test/test_mail.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,39 @@ def test_helloEmail(self):

self.assertTrue(isinstance(str(mail), str))

def test_helloEmailAdditionalContent(self):
"""Tests bug found in Issue-451 with Content ordering causing a crash"""

self.maxDiff = None

"""Minimum required to send an email"""
mail = Mail()

mail.from_email = Email("test@example.com")

mail.subject = "Hello World from the SendGrid Python Library"

personalization = Personalization()
personalization.add_to(Email("test@example.com"))
mail.add_personalization(personalization)

mail.add_content(Content("text/html", "<html><body>some text here</body></html>"))
mail.add_content(Content("text/plain", "some text here"))

self.assertEqual(
json.dumps(
mail.get(),
sort_keys=True),
'{"content": [{"type": "text/plain", "value": "some text here"}, '
'{"type": "text/html", '
'"value": "<html><body>some text here</body></html>"}], '
'"from": {"email": "test@example.com"}, "personalizations": '
'[{"to": [{"email": "test@example.com"}]}], '
'"subject": "Hello World from the SendGrid Python Library"}'
)

self.assertTrue(isinstance(str(mail), str))

def test_kitchenSink(self):
self.maxDiff = None

Expand Down