Skip to content

Commit 50d50d2

Browse files
authored
fix: allow general Email type for to_emails (#921)
1 parent f12689c commit 50d50d2

File tree

2 files changed

+53
-47
lines changed

2 files changed

+53
-47
lines changed

sendgrid/helpers/mail/mail.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -256,9 +256,9 @@ def add_to(
256256
email = To(email, None)
257257
elif isinstance(email, tuple):
258258
email = To(email[0], email[1])
259-
elif not isinstance(email, To):
259+
elif not isinstance(email, Email):
260260
raise ValueError(
261-
'Please use a tuple, To, or a str for a to_email list.'
261+
'Please use a To/Cc/Bcc, tuple, or a str for a to_email list.'
262262
)
263263
self._set_emails(email, global_substitutions, is_multiple, p)
264264
else:

test/test_mail_helpers.py

Lines changed: 51 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
ClickTracking, Content,
1515
DynamicTemplateData, Email, From,
1616
Mail, Personalization,
17-
Subject, Substitution, To, TrackingSettings
17+
Subject, Substitution, To, Cc, Bcc, TrackingSettings
1818
)
1919

2020

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

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')
313+
Mail(
314+
from_email=From('test+from@example.com', 'Example From Name'),
315+
to_emails=to_emails,
316+
subject=Subject('Sending with SendGrid is Fun'),
317+
plain_text_content=PlainTextContent(
318+
'and easy to do anywhere, even with Python'),
319+
html_content=HtmlContent(
320+
'<strong>and easy to do anywhere, even with Python</strong>'))
324321

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

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')
327+
Mail(
328+
from_email=From('test+from@example.com', 'Example From Name'),
329+
to_emails=to_emails,
330+
subject=Subject('Sending with SendGrid is Fun'),
331+
plain_text_content=PlainTextContent(
332+
'and easy to do anywhere, even with Python'),
333+
html_content=HtmlContent(
334+
'<strong>and easy to do anywhere, even with Python</strong>'))
341335

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

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')
341+
Mail(
342+
from_email=From('test+from@example.com', 'Example From Name'),
343+
to_emails=to_emails,
344+
subject=Subject('Sending with SendGrid is Fun'),
345+
plain_text_content=PlainTextContent(
346+
'and easy to do anywhere, even with Python'),
347+
html_content=HtmlContent(
348+
'<strong>and easy to do anywhere, even with Python</strong>'))
358349

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

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')
355+
Mail(
356+
from_email=From('test+from@example.com', 'Example From Name'),
357+
to_emails=to_emails,
358+
subject=Subject('Sending with SendGrid is Fun'),
359+
plain_text_content=PlainTextContent(
360+
'and easy to do anywhere, even with Python'),
361+
html_content=HtmlContent(
362+
'<strong>and easy to do anywhere, even with Python</strong>'))
363+
364+
def test_error_is_not_raised_on_to_emails_includes_bcc_cc(self):
365+
from sendgrid.helpers.mail import (PlainTextContent, HtmlContent)
366+
self.maxDiff = None
367+
to_emails = [
368+
To('test+to0@example.com', 'Example To Name 0'),
369+
Bcc('test+bcc@example.com', 'Example Bcc Name 1'),
370+
Cc('test+cc@example.com', 'Example Cc Name 2')
371+
]
372+
373+
Mail(
374+
from_email=From('test+from@example.com', 'Example From Name'),
375+
to_emails=to_emails,
376+
subject=Subject('Sending with SendGrid is Fun'),
377+
plain_text_content=PlainTextContent(
378+
'and easy to do anywhere, even with Python'),
379+
html_content=HtmlContent(
380+
'<strong>and easy to do anywhere, even with Python</strong>'))
375381

376382
def test_dynamic_template_data(self):
377383
self.maxDiff = None

0 commit comments

Comments
 (0)