python 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
如何使用 python 获取每个块的内容?
还有如何获取每个块的属性? (内容类型等)
A mail can contain different blocks like:
--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
How can I get content of each block with python?
And also how to get properties of each block? (content-type, etc..)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
为了解析电子邮件,我使用了
Message.walk()
方法,如下所示:对于内容,您可以尝试:
part.get_payload()
。对于内容类型,有:part.get_content_type()
您将在这里找到文档:http://docs.python.org/library/email.message.html
您还可以尝试
email
模块及其迭代器。For parsing emails I have used
Message.walk()
method like this:For content you can try:
part.get_payload()
. For content-type there is:part.get_content_type()
You will find documetation here: http://docs.python.org/library/email.message.html
You can also try
email
module with its iterators.http://docs.python.org/library/email.html
一个非常简单的例子(msg_as_str 包含从 imap 服务器获取的原始字节):
http://docs.python.org/library/email.html
A very simple example (msg_as_str contains the raw bytes you got from the imap server):
我已经写了这段代码。如果您喜欢它来解析多部分内容,则可以使用它:
如果您想以纯文本形式取出,则将正文转换为
html2text.HTML2Text()
I have wrote this code. You can use it if you like it for parsing multipart content:
And if you want to take out in plain text then convert body into
html2text.HTML2Text()