6.0.2 fails to send to list of lists
Issue Summary
Something I ran into while upgrading to 6.0.2 just now is that when sending a Mail object with a to_emails= parameter that is a list of [[email, name]], a confusing exception is thrown while if it's a list of [(email, name)], it succeeds.
This snippet fails
from sendgrid import SendGridAPIClient
from sendgrid.helpers.mail import Mail
email_list = [["[email protected]", "Benjamin Uno"],
["[email protected]", "Benjamin Dos"]]
mail = Mail(subject=subject,
html_content=content,
from_email=("[email protected]", "Website Team"),
to_emails=email_list,
)
sendgrid_client = SendGridAPIClient('abcd123')
sendgrid_client.send(mail)
The exception thrown is ValueError: Please use a To, Cc or Bcc object.
This snippet succeeds
email_list = [("[email protected]", "Benjamin Uno"),
("[email protected]", "Benjamin Dos")]
# (( Same code as above ))
Steps to Reproduce
- Run the initial snippet above
Expected
- Option 1: accept a list of lists as well as a list of tuples. This seems doable but there may be ancient backwards incompatibility reasons why it's hard.
- Option 2: fail immediately if you see a list of lists. Otherwise the error "Please use a To, Cc or Bcc object." is pretty confusing and took me a while to figure out why sendgrid was unhappy.
Technical details:
- sendgrid-python Version: 6.0.2
- Python Version: 3.7
Hi @plowman!
Thanks for reporting this! Yes, that error is confusing in this context and I'll leave this open for a fix.
to_emails currently requires either a list of Email/To/Cc/Bcc objects or a list of tuples. I did not think to include the ability of allowing a list of lists.
Would a better error do the trick or are you also wanting to be able to pass a list of lists?
With Best Regards,
Elmer
Hey @thinkingserious thanks for the fast reply! I don't have a strong preference on the fix. Throwing an error when I call send() with a list of lists would probably do the trick.
Is this still available? I have a couple of follow-up questions that are a bit beyond the scope of this issue, but may be worth taking a look at separately:
It looks like there may be some similar issues with parsing a list of dictionaries. Should this be fixed, too? Or is throwing a better error still an acceptable fix?
Similar parsing issues re:lists/dictionaries exist for adding BCC and CC parameters as well, but the Mail object doesn't take cc/bcc email parameters? What is the rationale for this?
Thanks :)
Going to look into this one