1

我正在使用 python 邮箱模块来加载一系列电子邮件。有没有一种简单的方法可以删除电子邮件中除最新消息之外的所有消息?不同的客户似乎对回复文本的处理方式不同,所以我找不到一种适用于所有电子邮件的过滤方式。

有没有办法在邮箱模块中内置此功能?这是我正在运行以获取电子邮件并将纯文本部分打印到输出文件的代码:

import mailbox

def main():
    text = ""
    for message in mailbox.mbox('test.mbox'):
        text += str(getPayload(message))

    with open('output.txt','wb') as outputfile:
        outputfile.write(text)
        outputfile.close()

def getPayload(msg):
    body = ''
    if msg.is_multipart():
        for part in msg.walk():
            ctype = part.get_content_type()
            cdispo = str(part.get('Content-Disposition'))

            if ctype == 'text/plain' and 'attachment' not in cdispo:
                body = part.get_payload(decode=True)
                break

    else:
        body = msg.get_payload(decode=True)

    return body

if __name__ == "__main__": main()

有没有办法只获取链中的最新消息?

4

1 回答 1

-1

您可以看到电子邮件的正文必须分开: - 邮件 - 链中的上一个邮件

像这样

Re: Do Smthg

ok,i ll do it

On Fri, Oct 5, 2018 at 6:10 PM abc <abc@.edu.vn>
wrote:

> Hi X
>
> Can you do this for me
> 
> ABC
> ---------- Forwarded message ----------
> From: ...
> Date: 3 October 2018 at 16:34
> Subject: Do Smthing

邮件正文是“okai,我会做”所以,要获取邮件的最新部分,只需删除包含“>”符号的部分

于 2019-01-05T13:49:02.857 回答