Python Sendgrid Library and Templates

I am trying to use the templates that I created in Sendgrid. I want to use the template that I created in Sendgrid to send email (with replacement tag replacement).

Here is what I tried -

import sendgrid sg = sendgrid.SendGridClient('<username>', '<password>') message = sendgrid.Mail() message.add_to('<to-email-address>') message.set_subject('My Test Email') message.add_substitution('customer_link', 'http://my-domain.com/customer-id') message.add_filter('templates', 'enable', '1') message.add_filter('templates', 'template_id', '<alphanumeric-template-id>') message.add_from('<from-email-address>') status, msg = sg.send(message) 

However, this gives me the error that '{"errors":["Missing email body"],"message":"error"}' . I tried to add the following lines, but still the same error -

 message.set_html('') message.set_text('') 

So the exact code I use after using set_html is

 import sendgrid sg = sendgrid.SendGridClient('<username>', '<password>') message = sendgrid.Mail() message.add_to('<to-email-address>') message.set_subject('My Test Email') message.add_substitution('customer_link', 'http://my-domain.com/customer-id') message.add_filter('templates', 'enable', '1') message.add_filter('templates', 'template_id', '<alphanumeric-template-id>') message.add_from('<from-email-address>') message.set_html('') message.set_text('') status, msg = sg.send(message) 

What am I missing here? The python documentation for the library does not seem to contain anything.

Change One of the workarounds I found comes from the answer below. I set message.set_html(' ') and message.set_text(' ') , which basically gives a space bar. This is strange, but works for this case.

+4
source share
1 answer

The official documentation is really poor, as it just mentions an example. However, it provides you with a link to the Github sendgrid-python repository where you can find wider examples and the code itself (sometimes it’s faster to look at it!).

Find the full example here on the Github page :

 import sendgrid sg = sendgrid.SendGridClient('YOUR_SENDGRID_USERNAME', 'YOUR_SENDGRID_PASSWORD') message = sendgrid.Mail() message.add_to('John Doe < john@email.com >') message.set_subject('Example') message.set_html('Body') message.set_text('Body') message.set_from('Doe John < doe@email.com >') status, msg = sg.send(message) #or message = sendgrid.Mail(to=' john@email.com ', subject='Example', html='Body', text='Body', from_email=' doe@email.com ') status, msg = sg.send(message) 

However, in this case, you use the template and on the Sendgrid blog they mention:

Demo template - Q & A

Should I use body and object replacement tags in my template? What if I do not want to send any substitution variables to my email address?

Not. You can pass a null value using your API call, rather than specifying any variables.

although apparently this is not the case, and you need to add some html and text to your message instance.

So, the workaround here is what you found: install html and text in a non-empty line:

 message.set_html(' ') message.set_text(' ') 
+3
source

All Articles