【问题标题】:Python mailbox remove repliesPython邮箱删除回复
【发布时间】:2016-09-12 17:47:29
【问题描述】:

我正在使用 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()

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

【问题讨论】:

    标签: python mbox


    【解决方案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,我会做的” 因此,要获取 Mail 的最新部分,只需删除包含“>”符号的部分

    【讨论】:

      猜你喜欢
      • 2021-04-29
      • 1970-01-01
      • 2018-03-15
      • 2010-09-07
      • 2012-05-18
      • 1970-01-01
      • 2019-05-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多