Sendgrid-python: redundant to: field and content may not be empty

Created on 23 Nov 2016  路  5Comments  路  Source: sendgrid/sendgrid-python

Hello,

i'm currently trying to send personalized emails to different recipients. However i have two open questions. The code can be found here: https://gist.github.com/cl1ent/f6abeeec37ba9b65327aa5507ef80363

  • when i try to send an email with a personalization (without adding another to field) i get the following error:

The to array is required for all personalization objects, and must have at least one email object with a valid email address

But i've already declared a to field when i initially created the mail (line 9&11). why do i have to set two to: fields when i only want to send the mail to one recipient?

  • second, my mails are static templates with placeholders. why do i have to create the mail object with a forced content? I've tried mail = Mail(from_email, subject, to_email, None) but that just gave me the following error:

The from object must be provided for every email send. It is an object that requires the email parameter, but may also contain a name parameter.

What can i do to circumvent these two errors?

Thanks in advance,
cl1ent

question

Most helpful comment

Hi Elmer,
Thanks for following up. With the help of the example you referenced, I was able to use the no-arg Mail constructor to create the Mail client and only send emails to the to emails specified within each personalization.

Thanks,
Gabe

All 5 comments

Hello @cl1ent,

Thanks for reaching out, I'm hoping this example will help you: https://github.com/sendgrid/sendgrid-python/blob/master/USE_CASES.md#transactional_templates

With Best Regards,

Elmer

Hi @thinkingserious,

thanks for your response. mail.personalizations[0].add_substitution() worked for me! However I still wonder how to create a mail object without the subject field. This is because my subject field is already defined in the template.

Thanks,
cl1ent

Hello @thinkingserious ,
I also have similar questions as @cl1ent regarding the v3 Mail python client and the use of personalizations.

The biggest issue I have been facing is the to: field as part of the mail = Mail(from_email, subject, to_email, content) constructor is required. If I use that same email for an individual personalization, that email address will then receive two emails: one with all substitutions blank, and the other with all substitutions resolved. An example would be to use the example code posted here and replace personalization.add_to(Email("[email protected]", "Example User")) with personalization.add_to(Email("[email protected]", "Example User")).

How do you recommend setting the to: fields with the v3 Mail client when using personalizations?

Thank you,
Gabe

@cl1ent, @gstanek,

Here is a full example that does not use the helper constructor (that constructor was designed for the most basic of use cases): https://github.com/sendgrid/sendgrid-python/blob/master/examples/helpers/mail/mail_example.py#L20

Please take a look and let me know if you still have any issues.

With Best Regards,

Elmer

Hi Elmer,
Thanks for following up. With the help of the example you referenced, I was able to use the no-arg Mail constructor to create the Mail client and only send emails to the to emails specified within each personalization.

Thanks,
Gabe

Was this page helpful?
0 / 5 - 0 ratings

Related issues

andriisoldatenko picture andriisoldatenko  路  4Comments

c-w picture c-w  路  4Comments

thinkingserious picture thinkingserious  路  5Comments

sohamnavadiya picture sohamnavadiya  路  4Comments

hugomallinson picture hugomallinson  路  3Comments