Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sendgrid Python library and templates

I am trying to use templates which I have created on Sendgrid. What I want to do is, use the template that I have created on Sendgrid to send an email (with substituting the substitution tags).

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 an error that '{"errors":["Missing email body"],"message":"error"}'. I tried adding the following lines but still the same error -

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

So, the exact code I am using 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 doesn't seem to contain anything.

Edit - One of the workaround that I found is derived from the answer below. I do set message.set_html(' ') and message.set_text(' '), that is basically, give a whitespace string. This is weird, though, but works for this case.

like image 818
Siddharth Avatar asked Sep 28 '22 01:09

Siddharth


1 Answers

The official documentation it is indeed poor, since it just mentions an example. However, it provides you a link to its Github sendgrid-python repository in which you can find broader examples and the code itself (sometimes it is faster to look at this!).

Find here a full example listed in the Github page:

import sendgrid

sg = sendgrid.SendGridClient('YOUR_SENDGRID_USERNAME', 'YOUR_SENDGRID_PASSWORD')

message = sendgrid.Mail()
message.add_to('John Doe <[email protected]>')
message.set_subject('Example')
message.set_html('Body')
message.set_text('Body')
message.set_from('Doe John <[email protected]>')
status, msg = sg.send(message)

#or

message = sendgrid.Mail(to='[email protected]', subject='Example', html='Body', text='Body', from_email='[email protected]')
status, msg = sg.send(message)

However, in this case you are using a template and in Sendgrid's blog they mention:

Template Engine Demo – Q&A

Do I have to use the body and subject substitution tags in my template? What If I don’t want to pass any substitution variables in my email?

No. You can pass a blank value through with your API call and not specify any variables.

although apparently it is not like this and you need to append some kind of html and text in your message instance.

So the workaround here is what you discovered: to set the html and text to a non-empty string:

message.set_html(' ')
message.set_text(' ')
like image 125
fedorqui 'SO stop harming' Avatar answered Oct 04 '22 18:10

fedorqui 'SO stop harming'