MailGun API批量发送Python

问题描述 投票:0回答:1

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

我跟着他们的documentation,但似乎没有用。

所以,我有一个这样的字典:

     {'[email protected]': {'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'},
     {'[email protected]': {'filepath': 'YYY.pdf',
      ...

我创建了一个列表来获取密钥:

      maillist = ['[email protected]', '[email protected]']

我的功能看起来像这样:

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 <[email protected]>",
              "to": maillist,
              "subject": "%recipient.Titulo%",
              "text": "Testing some Mailgun awesomness, %recipient.Farmacia%, Sr. %recipient.Nome%",
              "html": "<html>HTML version of the body</html>"})

并且它不会发送,它返回ERROR CODE 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 <[email protected]>",
              "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%'

如果有人可以提供帮助,我将非常感激,因为我找不到这个。

提前致谢。

python send mailgun
1个回答
0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.