如何使用sendgrid API在django中使用设计模板?

Meg*_*tes 3 python django sendmail sendgrid

我已将发送网格与我的Django应用程序集成,并且邮件也已成功发送.但现在我想从我的django应用程序发送带有设计模板的电子邮件.我也阅读了文档,但不知道如何以编程方式使用它.这是我第一次使用send-grid.请任何人都可以帮我找出如何从django应用程序发送发送网格模板的方法.

Mar*_*ies 5

您可以使用SendGrid的模板引擎在SendGrid中存储模板.然后,在通过SendGrid API发送电子邮件时引用该模板ID,您可以在sendgrid-python库中看到该代码的示例.

这是一个完整的例子,它使用SendGrid API密钥(您可以通过阅读本指南了解如何获得该设置):

import sendgrid

sg = sendgrid.SendGridClient('sendgrid_apikey')

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>')

# This next section is all to do with Template Engine

# You pass substitutions to your template like this
message.add_substitution('-thing_to_sub-', 'Hello! I am in a template!')

# Turn on the template option
message.add_filter('templates', 'enable', '1')

# Tell SendGrid which template to use
message.add_filter('templates', 'template_id', 'TEMPLATE-ALPHA-NUMERIC-ID')

# Get back a response and status
status, msg = sg.send(message)
Run Code Online (Sandbox Code Playgroud)