python含多个附件的邮件,如何使用Mailgun(来自Python)发送带有多个附件和自定义文件名的邮件...

在没有文件系统访问权限的沙箱环境中,通过Mailgun API发送邮件时,原始示例中附件的文件名仅通过open()函数隐含。解决方法是直接在requests.post()调用中明确指定每个附件的文件名,例如"attachment[0]":("FileName1.ext", open(FILE_PATH_1,'rb'))等,这样可以确保客户端能够正确识别附件的文件名。
摘要由CSDN通过智能技术生成

(As Mailgun doesn't have a python library this applies for both CURL and Python)

We are working on a sandboxed server without access to the filesystem.

This is the example provided by mailgun:

def send_complex_message():

return requests.post(

"https://api.mailgun.net/v2/samples.mailgun.org/messages",

auth=("api", "key-3ax6xnjp29jd6fds4gc373sgvjxteol0"),

files=[("attachment", open("files/test.jpg")),

("attachment", open("files/test.txt"))],

data={"from": "Excited User ",

"to": "foo@example.com",

"cc": "baz@example.com",

"bcc": "bar@example.com",

"subject": "Hello",

"text": "Testing some Mailgun awesomness!",

"html": "HTML version of the body"})

As you can see the filename is only implied on the open() calls.

Given that we have no access to the filesystem, we download the files from a remote location and pass the data.

This sends the data in the mail but the filenames are ignored, this makes it almost impossible for clients to open the files as they would have to guess the file extension for every attachment.

How do we specify the filenames manually?

Thanks!

解决方案

After digging around for a while I discovered a sample showing exactly how to do this here.

I am leaving that code here for future reference as it was very useful:

def send_complex_message():

return requests.post("https://api.mailgun.net/v2/DOMAIN/messages",

auth=("api", "key-SECRET"),

files={

"attachment[0]": ("FileName1.ext", open(FILE_PATH_1, 'rb')),

"attachment[1]": ("FileName2.ext", open(FILE_PATH_2, 'rb'))

},

data={"from": "FROM_EMAIL",

"to": [TO_EMAIL],

"subject": SUBJECT,

"html": HTML_CONTENT

})

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值