Zeb*_*ebs 3 python email mailgun
(因为Mailgun没有python库,这适用于CURL和Python)
我们正在开发一个无法访问文件系统的沙盒服务器.
这是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 <me@samples.mailgun.org>",
"to": "foo@example.com",
"cc": "baz@example.com",
"bcc": "bar@example.com",
"subject": "Hello",
"text": "Testing some Mailgun awesomness!",
"html": "<html>HTML version of the body</html>"})
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,文件名仅隐含在open()调用上.
鉴于我们无法访问文件系统,我们从远程位置下载文件并传递数据.
这会在邮件中发送数据但文件名会被忽略,这使得客户端几乎不可能打开文件,因为他们必须猜测每个附件的文件扩展名.
我们如何手动指定文件名?
谢谢!
经过一段时间的挖掘后,我发现了一个样本,显示了如何在这里做到这一点.
我将此代码留在此处以供将来参考,因为它非常有用:
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
})
Run Code Online (Sandbox Code Playgroud)