diff --git a/sendgrid/helpers/mail/mail.py b/sendgrid/helpers/mail/mail.py
index 5d9490ba3..ce8bb2f0c 100644
--- a/sendgrid/helpers/mail/mail.py
+++ b/sendgrid/helpers/mail/mail.py
@@ -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
@@ -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
@@ -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):
diff --git a/test/test_mail_helpers.py b/test/test_mail_helpers.py
index fd4f3a68f..dff3de5b2 100644
--- a/test/test_mail_helpers.py
+++ b/test/test_mail_helpers.py
@@ -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 = [
+ ['test+to0@example.com', 'Example To Name 0'],
+ ['test+to1@example.com', 'Example To Name 1']
+ ]
+
+ with self.assertRaises(ValueError):
+ 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(
+ 'and easy to do anywhere, even with Python'))
+
+ 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 = [
+ ('test+to0@example.com', 'Example To Name 0'),
+ ('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(
+ 'and easy to do anywhere, even with Python'))
+ 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 = ['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(
+ 'and easy to do anywhere, even with Python'))
+ 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 = '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(
+ 'and easy to do anywhere, even with Python'))
+ 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 = ('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(
+ 'and easy to do anywhere, even with Python'))
+ except:
+ self.fail('Mail() raised an error on a tuple of strings')
+
def test_dynamic_template_data(self):
self.maxDiff = None