MailGun API批量发送Python

2024-09-29 19:22:18 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图用Python在mailgunapi上定义一个发送协议。在

我跟踪了他们的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',
      ...

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

^{pr2}$

我的功能是这样的:

^{3}$

它只是不发送,它返回错误代码400:

Bad Request - Often missing a required parameter

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

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

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

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%'

如果有人能帮我,我会非常感激的,因为我找不到很多东西。在

提前谢谢。在


Tags: ptapi协议字典定义pdfdomainhtml
1条回答
网友
1楼 · 发布于 2024-09-29 19:22:18

因为您正在发出POST请求,所以作为recipientVars传递的数据应该转换成JSON字符串。所以只要json.dumps(recipientVars)就可以解决问题(当然,在导入json之后)。在

相关问题 更多 >

    热门问题