-
Notifications
You must be signed in to change notification settings - Fork 714
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #631 from cmccandless/support-email.message.EmailM…
…essage Allow creation of Mail from EmailMessage
- Loading branch information
Showing
2 changed files
with
57 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,13 @@ | |
import json | ||
import unittest | ||
|
||
try: | ||
from email.message import EmailMessage | ||
except ImportError: | ||
# Python2 | ||
from email import message | ||
EmailMessage = message.Message | ||
|
||
from sendgrid.helpers.mail import ( | ||
ASM, | ||
APIKeyIncludedException, | ||
|
@@ -557,3 +564,26 @@ def test_disable_tracking(self): | |
def test_directly_setting_substitutions(self): | ||
personalization = Personalization() | ||
personalization.substitutions = [{'a': 0}] | ||
|
||
def test_from_emailmessage(self): | ||
message = EmailMessage() | ||
body = 'message that is not urgent' | ||
try: | ||
message.set_content(body) | ||
except AttributeError: | ||
# Python2 | ||
message.set_payload(body) | ||
message.set_default_type('text/plain') | ||
message['Subject'] = 'URGENT TITLE' | ||
message['From'] = '[email protected]' | ||
message['To'] = '[email protected]' | ||
mail = Mail.from_EmailMessage(message) | ||
self.assertEqual(mail.subject, 'URGENT TITLE') | ||
self.assertEqual(mail.from_email.email, '[email protected]') | ||
self.assertEqual(len(mail.personalizations), 1) | ||
self.assertEqual(len(mail.personalizations[0].tos), 1) | ||
self.assertEqual(mail.personalizations[0].tos[0], {'email': '[email protected]'}) | ||
self.assertEqual(len(mail.contents), 1) | ||
content = mail.contents[0] | ||
self.assertEqual(content.type, 'text/plain') | ||
self.assertEqual(content.value, 'message that is not urgent') |