【问题标题】:Send multiple file attachments with Mailgun in Python在 Python 中使用 Mailgun 发送多个文件附件
【发布时间】:2015-03-02 17:08:52
【问题描述】:

请考虑位于此处的 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 提交多个文件?还是不可能?

【问题讨论】:

  • 您是否使用 mailgun webapi 发送附件?如果你能帮忙

标签: python python-requests bottle mailgun


【解决方案1】:

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

【讨论】:

  • 谢谢 Travis,当你说“touples 列表”时,灯泡熄灭了。对于其他任何人虽然 mailgun 将采用 MultiDict,但它还将采用文件的元组列表。这就是您可以附加多个文件的方式。
  • 有没有办法在提交时命名这些文件?我注意到一旦发送,它们就会显示为“附件”
  • 我和Stephn_R有同样的问题,提交时如何命名这些文件?
  • @ColoradoTechie:根据this post,文件元组的第二个元素本身可以是一个元组:“附件”:(“文件名1.ext”,打开(文件路径_1,'rb')) `
  • 我正在使用相同的方法,但它说:“'utf-8'编解码器无法解码位置 0 的字节 0x89:无效的起始字节”,因为我将它们包含在文件格式中打开(文件路径)
【解决方案2】:

我知道这已经得到解答,但是,我想我会发布我如何使用多个附件来解决这个问题。

这是我的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

谢谢~!

【讨论】:

  • 请问,您如何使用 httplib2 而不是 request 使用 Mailgun 发送附件
  • 我无法按原样工作,但以下内容对我有用:如果附件:文件 = [] 用于附件中的附件:with open(attachment, 'rb') as f: files。 append(('attachment', (os.path.basename(attachment), f.read()))) 附件的键必须是'attachment'。将它们命名为“附件-X”似乎不起作用。
【解决方案3】:

您可以通过这种方式添加多个附件和/或带有文件名的内嵌图像:

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>"})

希望对大家有所帮助。

【讨论】:

    猜你喜欢
    • 2016-10-22
    • 1970-01-01
    • 1970-01-01
    • 2012-12-23
    • 2017-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多