python imap:如何解析多部分邮件内容

Ser*_*gey 4 python email imap

邮件可以包含不同的块,如:

--0016e68deb06b58acf04897c624e
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
content_1
...

--0016e68deb06b58acf04897c624e
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
content_2
... and so on
Run Code Online (Sandbox Code Playgroud)

如何使用python获取每个块的内容?
还有如何获得每个块的属性?(内容类型等)

Mic*_*las 9

为了解析电子邮件,我使用了这样的Message.walk()方法:

if msg.is_multipart():
    for part in msg.walk():
        ...
Run Code Online (Sandbox Code Playgroud)

对于内容,您可以尝试:part.get_payload().对于内容类型,有:part.get_content_type()

您将在这里找到documetation:http://docs.python.org/library/email.message.html

您还可以email使用其迭代器来尝试模块.