【问题标题】:How to integrate sendgrid templates with python app?`如何将 sendgrid 模板与 python 应用程序集成?
【发布时间】:2015-05-02 11:28:48
【问题描述】:

我想将我在 Sendgrid 模板引擎上制作的模板与我的 python 应用程序集成。我阅读了 GitHub 上的文档和源代码,但它给了我这样的错误:

HTTP 400; JSON in headers is valid but incompatible

我的源代码是:

import sendgrid
import json
import os


mail_to = raw_input("Reciever's Mail(Name <email>): ")
subject = raw_input('Subject: ')
mail_from = raw_input("Sender Mail(Name <email>): ")


API_USER = os.getenv('API_USER')
API_KEY = os.getenv('API_KEY')

sg = sendgrid.SendGridClient(API_USER, API_KEY)

plaintext = \
    """
        <strong>This mail is only for testing.</strong>
    """
htmlbody = \
    """
      Html Data
    """

header_json = {
    'filters': {
        'templates': {
            'setting': {
                'enabled': '1',
                'template_id': '6967292-382-43aa-89dd-41fcd09b3fec'
            }
        }
    }
}

message = sendgrid.Mail(headers={'X-SMTPAPI': header_json})
message.add_to(mail_to)
message.set_subject(subject)
message.set_html(htmlbody)
message.set_text(plaintext)
message.set_from(mail_from)

status, msg = sg.send(message)

print "HTTP STATUS", status

msg = json.loads(msg)

if status == 400:
    print msg

谁能告诉我如何将我的模板与我的应用程序集成?

【问题讨论】:

    标签: python sendgrid


    【解决方案1】:

    模板支持内置于 SendGrid-Python 库中。来自README

    message = sendgrid.Mail()
    message.add_filter('templates', 'enable', '1')
    message.add_filter('templates', 'template_id', 'TEMPLATE-ALPHA-NUMERIC-ID')
    

    最好让smtpapi-python 库为您构建 SMTPAPI 标头。这实际上是作为 POST 参数传输的,而不是 HTTP 标头。该名称来自 SendGrid 的 Web API 存在之前,它实际上是作为 SMTP 标头发送的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-09-27
      • 1970-01-01
      • 2015-05-22
      • 2014-01-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-25
      相关资源
      最近更新 更多