Message instance has no attribute 'is_multipart'

最后都变了- 提交于 2019-12-13 03:12:37

问题


I am trying to write a script that gets me the contents of all the Mails in ~/Maildir. So I basically copypasted code from this question. Here is the full content of mailbox.py:

import mailbox
maildir = mailbox.Maildir("~/Maildir");
for message in maildir:
    print message["subject"]
    if message.is_multipart():
        print "ok"

It does print the subject of the first message, but instead of printing "ok" then, it dies stating

AttributeError: Message instance has no attribute 'is_multipart'

What did I do wrong?


回答1:


You have forgotten to name your Python version so let me guess — it's Python 2.7, right? In Python 2.7 mailbox.Maildir by default returns instances of rfc822.Messages, not email.Messages; rfc822.Message has a completely different API.

If you want mailbox.Maildir to return email.Messages remove default factory:

maildir = mailbox.Maildir("~/Maildir", factory=None)

In Python 3 rfc822.Message was removed so mailbox.Maildir returns email.Messages by default.



来源:https://stackoverflow.com/questions/45804579/message-instance-has-no-attribute-is-multipart

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!