[英]How to send a mail with multiple attachments and custom file names using Mailgun (from Python)
(因為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>"})
正如您所看到的,文件名僅隱含在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
})
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.