我正在尝试使用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>"})
它只是不会发送,它返回错误代码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%'
如果有人可以提供帮助,我将不胜感激。
谢谢。
答案 0 :(得分:0)
由于您正在发出POST请求,因此应将recipientVars
传递的数据转换为JSON字符串。因此,简单地json.dumps(recipientVars)
应该有望解决该问题(当然,在导入json
之后)。