From d2f60ccad51b917864e3ca1550c615fca4061dfb Mon Sep 17 00:00:00 2001 From: Elise Shanholtz Date: Thu, 9 Jul 2020 10:08:35 -0700 Subject: [PATCH 1/2] fix: allow general email type for to_emails --- sendgrid/helpers/mail/mail.py | 4 ++-- test/test_mail_helpers.py | 23 ++++++++++++++++++++++- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/sendgrid/helpers/mail/mail.py b/sendgrid/helpers/mail/mail.py index ce8bb2f0c..db2399310 100644 --- a/sendgrid/helpers/mail/mail.py +++ b/sendgrid/helpers/mail/mail.py @@ -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: diff --git a/test/test_mail_helpers.py b/test/test_mail_helpers.py index dff3de5b2..ded495242 100644 --- a/test/test_mail_helpers.py +++ b/test/test_mail_helpers.py @@ -14,7 +14,7 @@ ClickTracking, Content, DynamicTemplateData, Email, From, Mail, Personalization, - Subject, Substitution, To, TrackingSettings + Subject, Substitution, To, Cc, Bcc, TrackingSettings ) @@ -373,6 +373,27 @@ def test_error_is_not_raised_on_to_emails_set_to_a_tuple(self): except: self.fail('Mail() raised an error on a tuple of strings') + 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') + ] + + 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 From 0ee3b2f1e34d948bfac2924b87fe9d5f59e61358 Mon Sep 17 00:00:00 2001 From: Elise Shanholtz Date: Thu, 9 Jul 2020 17:06:34 -0700 Subject: [PATCH 2/2] update tests --- test/test_mail_helpers.py | 95 +++++++++++++++++---------------------- 1 file changed, 40 insertions(+), 55 deletions(-) diff --git a/test/test_mail_helpers.py b/test/test_mail_helpers.py index ded495242..202d3948b 100644 --- a/test/test_mail_helpers.py +++ b/test/test_mail_helpers.py @@ -310,68 +310,56 @@ 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( - 'and easy to do anywhere, even with Python')) - 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( + 'and easy to do anywhere, even with Python')) 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') + 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_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') + 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_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') + 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_includes_bcc_cc(self): from sendgrid.helpers.mail import (PlainTextContent, HtmlContent) @@ -382,17 +370,14 @@ def test_error_is_not_raised_on_to_emails_includes_bcc_cc(self): Cc('test+cc@example.com', 'Example Cc Name 2') ] - 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') + 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_dynamic_template_data(self): self.maxDiff = None