Jack tileman
Jack tileman

Reputation: 871

Sendgrid error - ValueError('Please use a To, Cc or Bcc object.',)

We are using sendgrid 6.0.5 python2.7 on google app engine standard.

The following code works

subject = data_sent_obj["subject"]
body_html            = data_sent_obj["body_html"]
body_text            = data_sent_obj["body_text"]

email_id_variable = "[email protected]"
to_email = "[email protected]"         # THIS WORKS
# to_email = Email(email_id_variable)   # THIS DOES NOT WORK

email_message = Mail(
    from_email          = '[email protected]',
    to_emails           = to_email,
    subject             = subject,                   
    html_content        = body_html)

personalization = Personalization()
personalization.add_to(Email(to_email))
bcc_list = bcc_email_list
for bcc_email in bcc_list:
    personalization.add_bcc(Email(bcc_email))


email_message.add_personalization(personalization)


try:
    sg = SendGridAPIClient(os.environ.get('SENDGRID_API_KEY'))
    response = sg.send(email_message)

When we use

to_email = Email(email_id_variable) we get the following error.
ValueError('Please use a To, Cc or Bcc object.',)

Essentially we would like to send email to an address that is in a variable.

Upvotes: 3

Views: 2547

Answers (1)

CBR
CBR

Reputation: 91

It seems the issue itself is not using the variable but the Mail implementation removing the Email object as a possibility for the list in to_emails, so instead use the To object:

from sendgrid.helpers.mail import To
...
to_email = To(email_id_variable)

The same could work with cc and bcc objects.

Upvotes: 7

Related Questions