Skip to content

Commit

Permalink
fix: allow general Email type for to_emails (#921)
Browse files Browse the repository at this point in the history
  • Loading branch information
eshanholtz authored Jul 10, 2020
1 parent f12689c commit 50d50d2
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 47 deletions.
4 changes: 2 additions & 2 deletions sendgrid/helpers/mail/mail.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,9 +256,9 @@ def add_to(
email = To(email, None)
elif isinstance(email, tuple):
email = To(email[0], email[1])
elif not isinstance(email, To):
elif not isinstance(email, Email):
raise ValueError(
'Please use a tuple, To, or a str for a to_email list.'
'Please use a To/Cc/Bcc, tuple, or a str for a to_email list.'
)
self._set_emails(email, global_substitutions, is_multiple, p)
else:
Expand Down
96 changes: 51 additions & 45 deletions test/test_mail_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
ClickTracking, Content,
DynamicTemplateData, Email, From,
Mail, Personalization,
Subject, Substitution, To, TrackingSettings
Subject, Substitution, To, Cc, Bcc, TrackingSettings
)


Expand Down Expand Up @@ -310,68 +310,74 @@ def test_error_is_not_raised_on_to_emails_set_to_list_of_tuples(self):
('test+to1@example.com', 'Example To Name 1')
]

try:
Mail(
from_email=From('test+from@example.com', 'Example From Name'),
to_emails=to_emails,
subject=Subject('Sending with SendGrid is Fun'),
plain_text_content=PlainTextContent(
'and easy to do anywhere, even with Python'),
html_content=HtmlContent(
'<strong>and easy to do anywhere, even with Python</strong>'))
except:
self.fail('Mail() raised an error on list of tuples')
Mail(
from_email=From('test+from@example.com', 'Example From Name'),
to_emails=to_emails,
subject=Subject('Sending with SendGrid is Fun'),
plain_text_content=PlainTextContent(
'and easy to do anywhere, even with Python'),
html_content=HtmlContent(
'<strong>and easy to do anywhere, even with Python</strong>'))

def test_error_is_not_raised_on_to_emails_set_to_list_of_strs(self):
from sendgrid.helpers.mail import (PlainTextContent, HtmlContent)
self.maxDiff = None
to_emails = ['test+to0@example.com', 'test+to1@example.com']

try:
Mail(
from_email=From('test+from@example.com', 'Example From Name'),
to_emails=to_emails,
subject=Subject('Sending with SendGrid is Fun'),
plain_text_content=PlainTextContent(
'and easy to do anywhere, even with Python'),
html_content=HtmlContent(
'<strong>and easy to do anywhere, even with Python</strong>'))
except:
self.fail('Mail() raised an error on list of strings')
Mail(
from_email=From('test+from@example.com', 'Example From Name'),
to_emails=to_emails,
subject=Subject('Sending with SendGrid is Fun'),
plain_text_content=PlainTextContent(
'and easy to do anywhere, even with Python'),
html_content=HtmlContent(
'<strong>and easy to do anywhere, even with Python</strong>'))

def test_error_is_not_raised_on_to_emails_set_to_a_str(self):
from sendgrid.helpers.mail import (PlainTextContent, HtmlContent)
self.maxDiff = None
to_emails = 'test+to0@example.com'

try:
Mail(
from_email=From('test+from@example.com', 'Example From Name'),
to_emails=to_emails,
subject=Subject('Sending with SendGrid is Fun'),
plain_text_content=PlainTextContent(
'and easy to do anywhere, even with Python'),
html_content=HtmlContent(
'<strong>and easy to do anywhere, even with Python</strong>'))
except:
self.fail('Mail() raised an error on a string')
Mail(
from_email=From('test+from@example.com', 'Example From Name'),
to_emails=to_emails,
subject=Subject('Sending with SendGrid is Fun'),
plain_text_content=PlainTextContent(
'and easy to do anywhere, even with Python'),
html_content=HtmlContent(
'<strong>and easy to do anywhere, even with Python</strong>'))

def test_error_is_not_raised_on_to_emails_set_to_a_tuple(self):
from sendgrid.helpers.mail import (PlainTextContent, HtmlContent)
self.maxDiff = None
to_emails = ('test+to0@example.com', 'Example To Name 0')

try:
Mail(
from_email=From('test+from@example.com', 'Example From Name'),
to_emails=to_emails,
subject=Subject('Sending with SendGrid is Fun'),
plain_text_content=PlainTextContent(
'and easy to do anywhere, even with Python'),
html_content=HtmlContent(
'<strong>and easy to do anywhere, even with Python</strong>'))
except:
self.fail('Mail() raised an error on a tuple of strings')
Mail(
from_email=From('test+from@example.com', 'Example From Name'),
to_emails=to_emails,
subject=Subject('Sending with SendGrid is Fun'),
plain_text_content=PlainTextContent(
'and easy to do anywhere, even with Python'),
html_content=HtmlContent(
'<strong>and easy to do anywhere, even with Python</strong>'))

def test_error_is_not_raised_on_to_emails_includes_bcc_cc(self):
from sendgrid.helpers.mail import (PlainTextContent, HtmlContent)
self.maxDiff = None
to_emails = [
To('test+to0@example.com', 'Example To Name 0'),
Bcc('test+bcc@example.com', 'Example Bcc Name 1'),
Cc('test+cc@example.com', 'Example Cc Name 2')
]

Mail(
from_email=From('test+from@example.com', 'Example From Name'),
to_emails=to_emails,
subject=Subject('Sending with SendGrid is Fun'),
plain_text_content=PlainTextContent(
'and easy to do anywhere, even with Python'),
html_content=HtmlContent(
'<strong>and easy to do anywhere, even with Python</strong>'))

def test_dynamic_template_data(self):
self.maxDiff = None
Expand Down

0 comments on commit 50d50d2

Please sign in to comment.