简体   繁体   English

Sendgrid Python库和模板

[英]Sendgrid Python library and templates

I am trying to use templates which I have created on Sendgrid. 我正在尝试使用我在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). 我想要做的是,使用我在Sendgrid上创建的模板发送电子邮件(替换替换标签)。

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"}' . 但是,这给了我一个'{"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 - 所以,使用set_html后我使用的确切代码是 -

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. 该库的python文档似乎不包含任何内容。

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. 我设置了message.set_html(' ')message.set_text(' ') ,基本上,给出一个空白字符串。 This is weird, though, but works for this case. 但这很奇怪,但适用于这种情况。

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!). 但是,它为您提供了一个指向其Github sendgrid-python存储库的链接,您可以在其中找到更广泛的示例和代码本身(有时可以更快地查看它!)。

Find here a full example listed in the Github page : 在这里找到Github页面中列出的完整示例:

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 are using a template and in Sendgrid's blog they mention: 但是,在这种情况下,您使用的是模板,在Sendgrid的博客中,他们提到:

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. 不可以。您可以通过API调用传递空白值,而不指定任何变量。

although apparently it is not like this and you need to append some kind of html and text in your message instance. 虽然显然它不是这样的,你需要在你的message实例中附加某种html和文本。

So the workaround here is what you discovered: to set the html and text to a non-empty string: 因此,您发现的解决方法是:将html和文本设置为非空字符串:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM