MailGun API 批量发送 Python

MailGun API Batch Sending Python

我正在尝试使用 Python 在 MailGun API 上定义发送协议。

我关注了他们documentation,但似乎没有用。

所以,我有这样一本字典:

     {'email1@domain.pt': {'filepath': 'XXX.pdf',
      'ANF': 0000,
      'folderfilepath': 'C:\XXX\XXX.pdf',
      'index': 77,
      'idt': XXX,
      'Titulo': 'Estimado Dr. ',
      'Farmacia': 'XXX',
      'Nome': 'XXX XXX',
      'Reports': 1,
      'Campanhas': 1,
      'Morada': 'XXX, XXX',
      'CP': 'XXXX-XXX',
      'Localidade': 'XXX',
      'Distrito': 'XXX',
      'Estado': 'Efetiva'},
     {'email2@domain.pt': {'filepath': 'YYY.pdf',
      ...

我创建了一个列表来获取像这样的键:

      maillist = ['email1@domain.pt', 'email2@domain.pt']

我的函数如下所示:

def send_complex_message_batch():
    return requests.post(
        "https://api.mailgun.net/v3/rede.XXX/messages",
        auth=("api", "key-XXXXXXX"),
        data={'recipient-variables': recipientVars,
              "from": "Excited User <geral@XXX.pt>",
              "to": maillist,
              "subject": "%recipient.Titulo%",
              "text": "Testing some Mailgun awesomness, %recipient.Farmacia%, Sr. %recipient.Nome%",
              "html": "<html>HTML version of the body</html>"})

它只是不会发送,它 returns 错误代码 400:

Bad Request - Often missing a required parameter

问题是它没有显示在 MailGun 日志中,因为邮件没有发送,所以我无法检查丢失了什么。

我迷路了,有人遇到过这样的问题吗?

如果我想更进一步,添加一个变量附件,情况会变得更糟:

def send_complex_message_batch():
    return requests.post(
        "https://api.mailgun.net/v3/rede.XXX/messages",
        auth=("api", "key-XXX"),
        data={'recipient-variables': recipientVars,
              "from": "Excited User <geral@XXX.pt>",
              "to": maillist,
              "subject": "%recipient.Titulo%",
              "text": "Testing some Mailgun awesomness, %recipient.Farmacia%, Sr. %recipient.Nome%",
              "html": "<html>HTML version of the body</html>"},
        files=[("attachment", ("%recipient.filepath%", open("%recipient.folderfilepath%", "rb").read()))])

有错误

FileNotFoundError: [Errno 2] No such file or directory: '%recipient.folderfilepath%'

如果有人能提供帮助,我将不胜感激,因为我在这方面找不到太多。

提前致谢。

由于您正在发出 POST 请求,作为 recipientVars 传递的数据应转换为 JSON 字符串。所以简单 json.dumps(recipientVars) 应该有望解决问题(当然是在导入 json 之后)。