无法使用 python 客户端通过 SparkPost 保存的模板发送邮件

Unable to send mail via SparkPost saved templates using python client

我正在尝试使用 SparkPost SMTP 发送邮件API

response = spark.transmissions.send(
    recipients=['somemail@example.com'],
    text='hi',
    content={
        "template_id": "my-first-email",
        "from":{"name": "someone", "email": "no-reply@jhuthmatbolo.com"},
    },
    subject='Subject Line',
    substitution_data={
       "name": "Your Name"
    }
)

根据 documentation 它应该可以工作,但我收到以下错误。

Traceback (most recent call last):
  File "sendmail.py", line 69, in <module>
    sparkpost()
  File "sendmail.py", line 65, in sparkpost
    "name": "Rupesh"
  File "/usr/local/lib/python2.7/dist-packages/sparkpost/transmissions.py", line 254, in send
    results = self.request('POST', self.uri, data=json.dumps(payload))
  File "/usr/local/lib/python2.7/dist-packages/sparkpost/base.py", line 41, in request
    **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/sparkpost/base.py", line 16, in request
    raise SparkPostAPIException(response)
sparkpost.exceptions.SparkPostAPIException: Call to https://api.sparkpost.com/api/v1/transmissions returned 422, errors:

        required field is missing Code: 1400 Description: content.from is a required field

PS- Sparkpost.

提供的 Postman 示例一切正常

当您使用存储的模板时(通过设置 template_id),您不需要设置 contentsubject,因为它们嵌入在模板中。您可以这样拨打电话:

response = sp.transmissions.send(
    recipients=['you@me.com'],
    template='my-template-id'
)

此外,python-sparkpost API 客户端在 SparkPost API 之上提供了一个层,因此 SparkPost.transmissions.send() 的参数与 1:1 不匹配对于 API。 python-sparkpost 的完整文档是 available here.

Here's a relevant sample 也来自 Python 客户端。