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

fix: type validation on to_emails parameter on mail object #920

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions sendgrid/helpers/mail/mail.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ def __init__(
:param subject: The subject of the email
:type subject: Subject, optional
:param to_emails: The email address of the recipient
:type to_emails: To, tuple, optional
:type to_emails: To, str, tuple, list(str), list(tuple),
list(To), optional
:param plain_text_content: The plain text body of the email
:type plain_text_content: string, optional
:param html_content: The html body of the email
Expand Down Expand Up @@ -239,7 +240,7 @@ def add_to(
"""Adds a To object to the Personalization object

:param to_email: A To object
:type to_email: To, str, tuple
:type to_email: To, str, tuple, list(str), list(tuple), list(To)
:param global_substitutions: A dict of substitutions for all recipients
:type global_substitutions: dict
:param is_multiple: Create a new personalization for each recipient
Expand All @@ -253,8 +254,12 @@ def add_to(
for email in to_email:
if isinstance(email, str):
email = To(email, None)
if isinstance(email, tuple):
elif isinstance(email, tuple):
email = To(email[0], email[1])
elif not isinstance(email, To):
raise ValueError(
'Please use a tuple, To, or a str for a to_email list.'
)
self._set_emails(email, global_substitutions, is_multiple, p)
else:
if isinstance(to_email, str):
Expand Down
89 changes: 89 additions & 0 deletions test/test_mail_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,95 @@ def test_multiple_emails_to_multiple_recipients(self):
}''')
)

def test_value_error_is_raised_on_to_emails_set_to_list_of_lists(self):
from sendgrid.helpers.mail import (PlainTextContent, HtmlContent)
self.maxDiff = None
to_emails = [
['[email protected]', 'Example To Name 0'],
['[email protected]', 'Example To Name 1']
]

with self.assertRaises(ValueError):
Mail(
from_email=From('[email protected]', '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_tuples(self):
from sendgrid.helpers.mail import (PlainTextContent, HtmlContent)
self.maxDiff = None
to_emails = [
('[email protected]', 'Example To Name 0'),
('[email protected]', 'Example To Name 1')
]

try:
Mail(
from_email=From('[email protected]', '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')

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 = ['[email protected]', '[email protected]']

try:
Mail(
from_email=From('[email protected]', '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')

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 = '[email protected]'

try:
Mail(
from_email=From('[email protected]', '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')

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 = ('[email protected]', 'Example To Name 0')

try:
Mail(
from_email=From('[email protected]', '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')

def test_dynamic_template_data(self):
self.maxDiff = None

Expand Down