来自 jython 中解析的电子邮件对象的电子邮件正文
我有一个对象。
fp = open(self.currentEmailPath, "rb")
p = email.Parser.Parser()
self._currentEmailParsedInstance= p.parse(fp)
fp.close()
self.currentEmailParsedInstance,从这个对象我想获取电子邮件的正文,只有文本没有 HTML...
我该怎么做?
像这样的东西?
newmsg=self._currentEmailParsedInstance.get_payload()
body=newmsg[0].get_content....?
然后从正文中剥离 html。 那是什么......返回实际文本的方法......也许我误解了你的
msg=self._currentEmailParsedInstance.get_payload()
print type(msg)
输出=类型“列表”
电子邮件
返回路径:
收到:来自 xx.xx.net(示例),作者:mxx3.xx.net (xxx)
[电子邮件受保护] 的 id 485EF65F08EDX5E12; 2008 年 10 月 23 日星期四 06:07:51 +0200
已收到:来自 xxxxx2 (ccc),通过 example.net (ccc)(验证为 [电子邮件受保护] ])
id 48798D4001146189 for [电子邮件受保护]; 2008 年 10 月 23 日星期四 06:07:51 +0200
来自:“示例”
致:
主题:固件:示例
日期:2008 年 10 月 23 日星期四 12:07:45 +0800
组织:示例
消息 ID:<001601c934c4$xxxx30$a9ff460a@xxx>
MIME 版本:1.0
内容类型:多部分/混合;
边界=“----=_NextPart_000_0017_01C93507.F6F64E30”
X-Mailer:Microsoft Office Outlook 11
X-MimeOLE:微软出品 MimeOLE V6.00.2900.3138
线程索引:Ack0wLaumqgZo1oXSBuIpUCEg/wfOAABAFEA
这是 MIME 格式的多部分消息。
------=_NextPart_000_0017_01C93507.F6F64E30
内容类型:多部分/替代;
边界=“----=_NextPart_001_0018_01C93507.F6F64E30”
------=_NextPart_001_0018_01C93507.F6F64E30
内容类型:文本/纯文本;
字符集=“us-ascii”
内容传输编码:7 位
来自:example.example[mailto:[电子邮件受保护]< /a>]
发送时间:2008 年 10 月 23 日星期四上午 11:37
收件人:[电子邮件受保护]
主题:例如S/I(B/L
编号:4357-0120-810.044)
请参阅附件中的示例.doc),
谢谢。
B.rgds,
xxx xxx
------=_NextPart_001_0018_01C93507.F6F64E30
内容类型:text/html;
字符集=“us-ascii”
内容传输编码:引用可打印
xmlns:o=3D"urn:schemas-microsoft-com:office:office" =
xmlns:w=3D"urn:schemas-microsoft-com:office:word" =
xmlns:st1=3D"urn:schemas-microsoft-com:office:smarttags" =
xmlns=3D"http://www.w3.org/TR/REC-html40“>
HTML 内容直到
------=_NextPart_001_0018_01C93507.F6F64E30--
------=_NextPart_000_0017_01C93507.F6F64E30
内容类型:application/msword;
名称=“xxxx.doc”
内容传输编码:base64
内容处置:附件;
文件名=“xxxx.doc”
0M8R4KGxGuEAAAAAAAAAAAAAAAAAAAAPgADAP7/CQAGAAAAAAAAAAAAAABAAAAYAAAAAAAAAAA EAAAYgAAAAAAAAD+////AAAAAF8AAAD/////////////////////////////////////////////// / /////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////// PCEAI2AJBAAA+FK/AAAAAAAAEAAAAAAABgAAAnEIAAA4AYmpiaqEVoRUAAAAAAAAAAAAAAAAAAAAA AAAECBYAMlAAAMN/AADDfwAAQQ4AAAAAAAAAPAAAAAAAAAAAAAAAAAAAAAAAAAAAAD//w8AAAAA AAAAAAD//w8AAAAAAAAAAD//w8AAAAAAAAAAAAAAAAAAAKQAAAAAAEYEAAAAAAAARgQAAEYE AAAAAARgQAAAAAAABGBAAAAAAAAEYEAAAAAAAARgQAABQAAAAAAAAAAAAAAFoEAAAAAAAAA4hsA AAAAAADiGwAAAAAAAOIbAAA4AAAAGhwAAHwAAACWHAAARAAAAFoEAAAAAAAABzcAAEgBAADmHAAA FgAAAPwcAAAAAAAA/BwAAAAAAAD8HAAAAAAAAPwcAAAAAAAA/BwAAAAAAAD8HAAAAAAAAPwcAAAA AAAAMjYAAAIAAAA0NgAAAAAAADQ2AAAAAAAANDYAAAAAAAA0NgAAAAAAADQ2AAAAAAAANDYAACQA AABPOAAAaAIAALc6AACOAAAWWDYAAGkAAAAAAAAAAAAAAAAAAAAAARgQAAAAAAABHLAAAAAAA AAAAAAAAAAAAAAAAAAAAAAD8HAAAAAAAAPwcAAAAAAAARywAAAAAAABHLAAAAAAAAFg2AAAAAAAA
------=_NextPart_000_0017_01C93507.F6F64E30--
我只想获取:
来自: xxxx.xxxx [mailto:[电子邮件受保护]]
发送时间:2008 年 10 月 23 日星期四上午 11:37
收件人:[电子邮件受保护]
主题: xxxxx 的 S/I(提单
编号:4357-0120-810.044)
请找到附件中的xxxx.doc),
谢谢。
B.rgds,
xxx xxx
不确定邮件是否格式错误! 似乎如果你得到一个 html 页面,你必须这样做:
parts=self._currentEmailParsedInstance.get_payload()
print parts[0].get_content_type()
..._multipart/alternative_
textParts=parts[0].get_payload()
print textParts[0].get_content_type()
..._text/plain_
body=textParts[0].get_payload()
print body
...get the text without a problem!!
非常感谢 Vinko。
所以它有点像处理 xml,本质上是递归的。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这将为您提供消息的内容
至于纯文本部分,您必须自己剥离 HTML,例如使用 BeautifulSoup。
检查此链接了解有关解析器返回的消息类。 如果您的意思是获取包含 HTML 和纯文本版本的消息的文本部分,您可以指定 get_payload() 的索引来获取您想要的部分。
我尝试使用不同的 MIME 电子邮件,因为您粘贴的内容似乎格式错误,希望您在编辑它时格式错误。
parts 将包含多部分消息的所有部分,您可以检查其内容类型,如图所示,并仅获取文本/纯文本。
祝你好运。
This will get you the contents of the message
As for the text only part you will have to strip HTML on your own, for example using BeautifulSoup.
Check this link for more information about the Message class the Parser returns. If you mean getting the text part of messages containing both HTML and plain text version of themselves, you can specify an index to get_payload() to get the part you want.
I tried with a different MIME email because what you pasted seems malformed, hopefully it got malformed when you edited it.
parts will contain all parts of the multipart message, you can check their content types as shown and get only the text/plain ones, for instance.
Good luck.
结束了这个
ended up with this