使用 Python 通过 Mailgun 提交变量时出错

Error when submitting variables through Mailgun with Python

我正在尝试使用 Python 通过 Mailgun 发送邮件,在那种情况下,我正在尝试将一个变量推送到旁边。

我的代码如下:

requests.post("MailGun URL",
              auth=("api", "api-key"),
              data={
                  "from":"From_Mail",
                  "to":["To_Mail"],
                  "template":"template_name",
                  "subject":"Testing some things",
                  "h:X-Mailgun-Variables": {"firstname": "John"}
              })

如果我在没有 "X-Mailgun-Variables" 的情况下使用上面的方法,我没有遇到任何问题,但是如果使用上面的方法,我会收到以下错误消息:

"delivery-status": {
        "code": 621,
        "message": "",
        "attempt-no": 1,
        "description": "Failed to decode variables",
        "session-seconds": 0
    }

我做错了什么?

提前致谢,

我自己找到了解决方案 - 以供将来参考:

"h:X-Mailgun-Variables": {"firstname": "John"}

以上ss仅供SMTP使用

API 使用:

"v:firstname":"John"

我遇到了同样的问题,用你的方法很累,但没有用。 期望值是键值对的字符串

test1 = '{"firstname": "'
test2 = str(data['first_name'])
test3 = '"}'
test4 = test1+test2+test3

"h:X-Mailgun-Variables": test4

如果转换为 JSON 应该可以工作,即

import json
...

"h:X-Mailgun-Variables": json.dumps({"firstname": "John"})

使用他们的 Node SDK mailgun-js 时,您需要使用 JSON.stringify() 来正确格式化负载。

例如。

const data = {
  from: 'John Smith <john@smith.com>',
  to: 'recipient@example.com',
  subject: 'Subject line',
  template: 'template-name',
  'h:X-Mailgun-Variables': JSON.stringify({
    name: `Morgan`,
    occupation: `Software Developer`
  })
};

await mailgun.messages().send(data);