Skip to content

Commit c51b429

Browse files
authored
fix: type validation on to_emails parameter on mail object (#920)
1 parent df75f6e commit c51b429

File tree

2 files changed

+97
-3
lines changed

2 files changed

+97
-3
lines changed

sendgrid/helpers/mail/mail.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ def __init__(
3737
:param subject: The subject of the email
3838
:type subject: Subject, optional
3939
:param to_emails: The email address of the recipient
40-
:type to_emails: To, tuple, optional
40+
:type to_emails: To, str, tuple, list(str), list(tuple),
41+
list(To), optional
4142
:param plain_text_content: The plain text body of the email
4243
:type plain_text_content: string, optional
4344
:param html_content: The html body of the email
@@ -239,7 +240,7 @@ def add_to(
239240
"""Adds a To object to the Personalization object
240241
241242
:param to_email: A To object
242-
:type to_email: To, str, tuple
243+
:type to_email: To, str, tuple, list(str), list(tuple), list(To)
243244
:param global_substitutions: A dict of substitutions for all recipients
244245
:type global_substitutions: dict
245246
:param is_multiple: Create a new personalization for each recipient
@@ -253,8 +254,12 @@ def add_to(
253254
for email in to_email:
254255
if isinstance(email, str):
255256
email = To(email, None)
256-
if isinstance(email, tuple):
257+
elif isinstance(email, tuple):
257258
email = To(email[0], email[1])
259+
elif not isinstance(email, To):
260+
raise ValueError(
261+
'Please use a tuple, To, or a str for a to_email list.'
262+
)
258263
self._set_emails(email, global_substitutions, is_multiple, p)
259264
else:
260265
if isinstance(to_email, str):

test/test_mail_helpers.py

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,95 @@ def test_multiple_emails_to_multiple_recipients(self):
284284
}''')
285285
)
286286

287+
def test_value_error_is_raised_on_to_emails_set_to_list_of_lists(self):
288+
from sendgrid.helpers.mail import (PlainTextContent, HtmlContent)
289+
self.maxDiff = None
290+
to_emails = [
291+
['test+to0@example.com', 'Example To Name 0'],
292+
['test+to1@example.com', 'Example To Name 1']
293+
]
294+
295+
with self.assertRaises(ValueError):
296+
Mail(
297+
from_email=From('test+from@example.com', 'Example From Name'),
298+
to_emails=to_emails,
299+
subject=Subject('Sending with SendGrid is Fun'),
300+
plain_text_content=PlainTextContent(
301+
'and easy to do anywhere, even with Python'),
302+
html_content=HtmlContent(
303+
'<strong>and easy to do anywhere, even with Python</strong>'))
304+
305+
def test_error_is_not_raised_on_to_emails_set_to_list_of_tuples(self):
306+
from sendgrid.helpers.mail import (PlainTextContent, HtmlContent)
307+
self.maxDiff = None
308+
to_emails = [
309+
('test+to0@example.com', 'Example To Name 0'),
310+
('test+to1@example.com', 'Example To Name 1')
311+
]
312+
313+
try:
314+
Mail(
315+
from_email=From('test+from@example.com', 'Example From Name'),
316+
to_emails=to_emails,
317+
subject=Subject('Sending with SendGrid is Fun'),
318+
plain_text_content=PlainTextContent(
319+
'and easy to do anywhere, even with Python'),
320+
html_content=HtmlContent(
321+
'<strong>and easy to do anywhere, even with Python</strong>'))
322+
except:
323+
self.fail('Mail() raised an error on list of tuples')
324+
325+
def test_error_is_not_raised_on_to_emails_set_to_list_of_strs(self):
326+
from sendgrid.helpers.mail import (PlainTextContent, HtmlContent)
327+
self.maxDiff = None
328+
to_emails = ['test+to0@example.com', 'test+to1@example.com']
329+
330+
try:
331+
Mail(
332+
from_email=From('test+from@example.com', 'Example From Name'),
333+
to_emails=to_emails,
334+
subject=Subject('Sending with SendGrid is Fun'),
335+
plain_text_content=PlainTextContent(
336+
'and easy to do anywhere, even with Python'),
337+
html_content=HtmlContent(
338+
'<strong>and easy to do anywhere, even with Python</strong>'))
339+
except:
340+
self.fail('Mail() raised an error on list of strings')
341+
342+
def test_error_is_not_raised_on_to_emails_set_to_a_str(self):
343+
from sendgrid.helpers.mail import (PlainTextContent, HtmlContent)
344+
self.maxDiff = None
345+
to_emails = 'test+to0@example.com'
346+
347+
try:
348+
Mail(
349+
from_email=From('test+from@example.com', 'Example From Name'),
350+
to_emails=to_emails,
351+
subject=Subject('Sending with SendGrid is Fun'),
352+
plain_text_content=PlainTextContent(
353+
'and easy to do anywhere, even with Python'),
354+
html_content=HtmlContent(
355+
'<strong>and easy to do anywhere, even with Python</strong>'))
356+
except:
357+
self.fail('Mail() raised an error on a string')
358+
359+
def test_error_is_not_raised_on_to_emails_set_to_a_tuple(self):
360+
from sendgrid.helpers.mail import (PlainTextContent, HtmlContent)
361+
self.maxDiff = None
362+
to_emails = ('test+to0@example.com', 'Example To Name 0')
363+
364+
try:
365+
Mail(
366+
from_email=From('test+from@example.com', 'Example From Name'),
367+
to_emails=to_emails,
368+
subject=Subject('Sending with SendGrid is Fun'),
369+
plain_text_content=PlainTextContent(
370+
'and easy to do anywhere, even with Python'),
371+
html_content=HtmlContent(
372+
'<strong>and easy to do anywhere, even with Python</strong>'))
373+
except:
374+
self.fail('Mail() raised an error on a tuple of strings')
375+
287376
def test_dynamic_template_data(self):
288377
self.maxDiff = None
289378

0 commit comments

Comments
 (0)