使用python imap和电子邮件包获取电子邮件的正文 [英] Get body text of an email using python imap and email package

查看:280
本文介绍了使用python imap和电子邮件包获取电子邮件的正文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用python imap和电子邮件包检索电子邮件的正文(仅文本)。

I want to retrieve body (only text) of emails using python imap and email package.

根据这个 SO线程,我使用以下代码: p>

As per this SO thread, I'm using the following code:

mail = email.message_from_string(email_body)
bodytext = mail.get_payload()[ 0 ].get_payload()

虽然在某些情况下可以正常工作,但有时候我会收到以下响应:

Though it's working fine for some instances, but sometime I get similar to following response

[<email.message.Message instance at 0x0206DCD8>, <email.message.Message instance at 0x0206D508>]


推荐答案

你假设消息具有统一的结构,一个定义明确的主要部分 。事实并非如此;可以有单个部分的消息不是文本部分(只是二进制文件的附件,而不是其他内容),或者它可以是具有多个文本部分(或者根本没有)的多部分,甚至是如果只有一个,它不需要是第一部分。此外,有嵌套的多部分(一个或多个部分是另一个MIME消息,递归地)。

You are assuming that messages have a uniform structure, with one well-defined "main part". That is not the case; there can be messages with a single part which is not a text part (just an "attachment" of a binary file, and nothing else) or it can be a multipart with multiple textual parts (or, again, none at all) and even if there is only one, it need not be the first part. Furthermore, there are nested multiparts (one or more parts is another MIME message, recursively).

在这么多单词中,您必须检查MIME结构,然后决定哪一部分(s)与您的申请相关。如果您只收到来自相当静态的小客户端的消息,您可能可以削减一些角落(至少直到Microsoft Plague命中的下一次升级),但是一般来说,根本不是任何类型的层次结构,只是收集(不一定总是直接相关)同样重要的部分。

In so many words, you must inspect the MIME structure, then decide which part(s) are relevant for your application. If you only receive messages from a fairly static, small set of clients, you may be able to cut some corners (at least until the next upgrade of Microsoft Plague hits) but in general, there simply isn't a hierarchy of any kind, just a collection of (not necessarily always directly related) equally important parts.

这篇关于使用python imap和电子邮件包获取电子邮件的正文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆