Skip to content

Commit

Permalink
Merge pull request #454 from dsouzarc/issue_451
Browse files Browse the repository at this point in the history
Fixes Issue 451
  • Loading branch information
thinkingserious committed May 30, 2018
2 parents 718ff6a + aefb3bd commit 0007d66
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
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

0 comments on commit 0007d66

Please sign in to comment.