请从位于此处的Mailgun文档中考虑此示例:http://documentation.mailgun.com/api-sending.html#examples
def send_complex_message():
return requests.post(
"https://api.mailgun.net/v2/samples.mailgun.org/messages",
auth=("api", "key-3ax6xnjp29jd6fds4gc373sgvjxteol0"),
files=MultiDict([("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>"})
这对我不起作用。当电子邮件到达时,它只有一个附件。我在python-bottle中使用MultiDict对象。我打破了文件字典,所以我可以按如下方式检查:
files=MultiDict([("attachment", ("file1.txt", "text file 1"),
("attachment", ("file2.txt", "text file 2")])
当你执行files.values()时,它只有一个条目“file2.txt”。这是有道理的。如果我尝试追加()一个条目,我会看到相同的行为。如果“密钥”相同(在这种情况下为“附件”),它将覆盖现有记录。
如果我给它提供了附件-1和附件2这样的唯一键,那么API会接受帖子,但是邮件是在没有附件的情况下发送的。
所以我想我的问题是:
1)瓶子中的MultiDict对象是否存在差异导致其失败?它似乎在字典中有多个条目,不允许使用相同的密钥?
2)我是否应该做一些未记录的事情来向mailgun提交多个文件?还是不可能这样做?
答案 0 :(得分:14)
您实际上可以在文件参数中使用元组列表,并且无需使用Multidict。这就是它的样子:
import requests
print 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>"})
免责声明:我为Mailgun工作!
答案 1 :(得分:0)
我知道这已经得到了解答,但是,我想我会发布我如何使用多个附件。
这是我的python函数,attachments参数是文件路径列表。
import requests
def send_complex_message(to, email_from, subject, html_body, attachments=None):
'''
to, email_from, subject, and html_body should be self explanatory.
attachments is a list of file paths, like this:
['/tmp/tmp5paoks/image001.png','/tmp/tmp5paoks/test.txt']
'''
data={"from": email_from,
"to": [to,""],
"subject": subject,
"html": html_body}
files = None
if attachments:
files = {}
count=0
for attachment in attachments:
with open(attachment,'rb') as f:
files['attachment['+str(count)+']'] = (os.path.basename(attachment), f.read())
count = count+1
return requests.post("https://api.mailgun.net/v2/mydomain.com/messages",
auth=(USER, PASSWORD),
files=files,
data=data)
我知道它有点冗长,然而,它正在起作用:-)。
我从这里了解了如何构建文件字典:https://gist.github.com/adamlj/8576660
感谢〜!
答案 2 :(得分:0)
您可以通过以下方式添加带有文件名的多个附件和/或内嵌图像:
import requests
print requests.post("https://api.mailgun.net/v2/samples.mailgun.org/messages",
auth=("api", "key-3ax6xnjp29jd6fds4gc373sgvjxteol0"),
files=[("attachment", ("test.pdf", open("files/test.pdf"))),
("attachment", ("test.txt", open("files/test.txt"))),
("inline", ("test.jpg", open("img/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 with an image <img src=\"cid:test.jpg\"></html>"})
我希望这对你们所有人都有帮助。