2

请考虑位于此处的 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”。这是有道理的。如果我也尝试 append() 一个条目,我会看到相同的行为。如果“密钥”相同(在这种情况下为“附件”),它将覆盖现有记录。

如果我给它提供像附件 1 和附件 2 之类的唯一键,API 会接受该帖子,但邮件是在没有附件的情况下发送的。

所以我想我的问题是:

1) 瓶子中的 MultiDict 对象是否存在差异导致此操作失败?似乎不允许在字典中有多个条目具有相同的键?

2)我应该做一些无证的事情来向mailgun提交多个文件吗?还是不可能这样做?

4

3 回答 3

15

您实际上可以在文件参数中使用元组列表并消除对 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 工作!

于 2014-01-22T18:58:08.973 回答
1

我知道这已经得到解答,但是,我想我会发布如何使用多个附件进行处理。

这是我的python函数,附件参数是文件路径列表。

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

谢谢~!

于 2014-10-14T23:24:04.640 回答
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>"})

我希望这对你们所有人都有帮助。

于 2015-03-02T09:01:58.273 回答