使用mailgun (python)发送带有附件的电子邮件出错



我试图使用MailGun发送一封带有自动生成的pdf文件作为附件的电子邮件,但我从请求库得到一个错误。这简直要把我逼疯了,因为我用的代码和例子里的完全一样。

我得到这个错误:列表对象没有属性'update'

这是我的代码:

# Generation of the pdf file        
pdf = StringIO.StringIO()
pisa.CreatePDF("<Some html code>", dest=pdf, encoding='utf8')
# Sending the email
requests.post("https://api.mailgun.net/v3/<MY_DOMAIN>/messages",
        auth=("api", "<MY_API_KEY>"),
        files = [("attachment", pdf.getvalue())],
        data={"from": "sender@email.com",
              "to": ["Jhon Doe", "destiny@email.com"],
              "subject": "Hello",
              "text": "Trying to send an attachment!"})

如果我删除文件行,它可以工作,但我需要发送附件。我试过更改我发送的文件类型。我还尝试了一些更简单的方法:

files = [("attachment", "Bla, bla bla")]

但是我得到的错误是关于那一行的格式(列表)。

请帮忙好吗?

post参数"files"必须是字典!

试试这个:

# Generation of the pdf file        
pdf = StringIO.StringIO()
pisa.CreatePDF("<Some html code>", dest=pdf, encoding='utf8')
# Sending the email
requests.post("https://api.mailgun.net/v3/<MY_DOMAIN>/messages",
        auth=("api", "<MY_API_KEY>"),
        files={"attachment": pdf.getvalue()},
        data={"from": "sender@email.com",
              "to": ["Jhon Doe", "destiny@email.com"],
              "subject": "Hello",
              "text": "Trying to send an attachment!"})

关于使用请求库上传文件的更多信息可以在这里找到:http://docs.python-requests.org/en/master/user/quickstart/#post-a-multipart-encoded-file

相关内容

最新更新