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

发布于 2024-09-30 06:03:36 字数 371 浏览 5 评论 0原文

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

--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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

你不是我要的菜∠ 2024-10-07 06:03:36

为了解析电子邮件,我使用了 Message.walk() 方法,如下所示:

if msg.is_multipart():
    for part in msg.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:

if msg.is_multipart():
    for part in msg.walk():
        ...

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.

寄居人 2024-10-07 06:03:36

http://docs.python.org/library/email.html

一个非常简单的例子(msg_as_str 包含从 imap 服务器获取的原始字节):

import email
msg = email.message_from_string(msg_as_str)
print msg["Subject"]

http://docs.python.org/library/email.html

A very simple example (msg_as_str contains the raw bytes you got from the imap server):

import email
msg = email.message_from_string(msg_as_str)
print msg["Subject"]
冬天的雪花 2024-10-07 06:03:36

我已经写了这段代码。如果您喜欢它来解析多部分内容,则可以使用它:

if mime_msg.is_multipart():
        for part in mime_msg.walk():
            if part.is_multipart():
                for subpart in part.get_payload():
                    if subpart.is_multipart():
                        for subsubpart in subpart.get_payload():
                            body = body + str(subsubpart.get_payload(decode=True)) + '\n'
                    else:
                        body = body + str(subpart.get_payload(decode=True)) + '\n'
            else:
                body = body + str(part.get_payload(decode=True)) + '\n'
else:
    body = body + str(mime_msg.get_payload(decode=True)) + '\n'

body = bytes(body,'utf-8').decode('unicode-escape')

如果您想以纯文本形式取出,则将正文转换为 html2text.HTML2Text()

I have wrote this code. You can use it if you like it for parsing multipart content:

if mime_msg.is_multipart():
        for part in mime_msg.walk():
            if part.is_multipart():
                for subpart in part.get_payload():
                    if subpart.is_multipart():
                        for subsubpart in subpart.get_payload():
                            body = body + str(subsubpart.get_payload(decode=True)) + '\n'
                    else:
                        body = body + str(subpart.get_payload(decode=True)) + '\n'
            else:
                body = body + str(part.get_payload(decode=True)) + '\n'
else:
    body = body + str(mime_msg.get_payload(decode=True)) + '\n'

body = bytes(body,'utf-8').decode('unicode-escape')

And if you want to take out in plain text then convert body into html2text.HTML2Text()

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文