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 2 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
7 changes: 6 additions & 1 deletion sendgrid/helpers/mail/mail.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,12 @@ def contents(self):
def add_content(self, content):
if self._contents is None:
self._contents = []
self._contents.append(content)

#Fix for issue-451: text content should be before html content
if content._type == "text/plain":
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What will happen here if someone provides only html content?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@hugomallinson who reported the issue mentioned that it was only when there is both plain text and HTML content and the HTML content is specified first.

If there's only HTML content, there shouldn't be any error and order wouldn't matter/make a difference

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