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

时间:2017-04-11 15:50:37

标签: python email sparkpost

我尝试使用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示例,一切正常。

1 个答案:

答案 0 :(得分:3)

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

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

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

来自Python客户端的

Here's a relevant sample