2016-11-15 156 views
1

我想用自动生成的pdf文件发送一封电子邮件作为使用MailGun的附件,但是我从请求库中得到一个错误。由于我使用与示例中完全相同的代码,所以这让我很疯狂。用mailgun(python)发送带附件的电子邮件时出错

我得到这个错误:列表对象有没有属性“更新”

这是我的代码:

# 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": "[email protected]", 
       "to": ["Jhon Doe", "[email protected]"], 
       "subject": "Hello", 
       "text": "Trying to send an attachment!"}) 

如果删除了文件行它的工作原理,但我需要发送附件。 我试过改变我发送的文件的种类。我也试过一些更简单的东西:

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

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

请帮忙吗?

回答

2

后置参数“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": "[email protected]", 
       "to": ["Jhon Doe", "[email protected]"], 
       "subject": "Hello", 
       "text": "Trying to send an attachment!"}) 

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

+0

感谢。我确信我尝试了这个解决方案,但似乎我没有。现在我再次尝试,它的工作原理。 –

相关问题