Skip to content

Commit

Permalink
Python2.7 import compatibility fix
Browse files Browse the repository at this point in the history
  • Loading branch information
cmccandless committed Oct 4, 2018
1 parent 23bc052 commit 839deb4
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
7 changes: 6 additions & 1 deletion sendgrid/helpers/mail/mail.py
Original file line number Diff line number Diff line change
Expand Up @@ -411,9 +411,14 @@ def from_EmailMessage(cls, message):
subject=message.get('Subject'),
to_email=Email(message.get('To')),
)
try:
body = message.get_content()
except AttributeError:
# Python2
body = message.get_payload()
mail.add_content(Content(
message.get_content_type(),
message.get_content()
body
))
for k, v in message.items():
mail.add_header(Header(k, v))
Expand Down
14 changes: 12 additions & 2 deletions test/test_mail.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
# -*- coding: utf-8 -*-
import json

from email.message import EmailMessage
try:
from email.message import EmailMessage
except ImportError:
# Python2
from email import message
EmailMessage = message.Message

from sendgrid.helpers.mail import (
ASM,
Expand Down Expand Up @@ -589,7 +594,12 @@ def test_dynamic_template_data(self):

def test_from_emailmessage(self):
message = EmailMessage()
message.set_content('message that is not urgent')
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'] = 'test@example.com'
Expand Down

0 comments on commit 839deb4

Please sign in to comment.