使用python邮箱读取mbox文件的邮件内容 [英] Reading the mail content of an mbox file using python mailbox

查看:261
本文介绍了使用python邮箱读取mbox文件的邮件内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Python邮箱打印邮件的内容(邮件正文)。

I am trying to print the content of the mail ( Mail body) using Python mailbox.

import mailbox

mbox = mailbox.mbox('Inbox')
i=1
for message in mbox:
    print i
    print "from   :",message['from']
    print "subject:",message['subject']
    print "message:",message['**messages**']
    print "**************************************" 
    i+=1

但是我感觉消息['消息']不是在此处打印邮件内容的正确消息。我从文档

But I feel message['messages'] is not the right one to print the mail content here. I could not understand it from the documentation

推荐答案

要获取消息内容,您想使用 get_payload() mailbox.Message email.message.Message 。您还需要检查 is_multipart() ,因为这会影响 get_payload()的返回值。例如:

To get the message content, you want to use get_payload(). mailbox.Message is a subclass of email.message.Message. You'll also want to check is_multipart() because that will affect the return value of get_payload(). Example:

if message.is_multipart():
    content = ''.join(part.get_payload(decode=True) for part in message.get_payload())
else:
    content = message.get_payload(decode=True)

这篇关于使用python邮箱读取mbox文件的邮件内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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