【问题标题】:How to get message body (or bodies) from Message object returned by email.parser.Parser?如何从 email.parser.Parser 返回的 Message 对象中获取消息正文(或正文)?
【发布时间】:2017-04-14 01:18:47
【问题描述】:

我正在阅读Python 3 docs here,我一定是盲人或什么的......它在哪里说如何获取邮件正文?

我想要做的是打开一封邮件并在邮件的基于文本的正文中执行一些循环,跳过二进制附件。伪代码:

def read_all_bodies(local_email_file):
    email = Parser().parse(open(local_email_file, 'r'))
    for pseudo_body in email.pseudo_bodies:
        if pseudo_body.pseudo_is_binary():
            continue
        # Pseudo-parse the body here

我该怎么做?甚至 Message 类是正确的类吗?不是只针对header吗?

【问题讨论】:

    标签: python rfc2822


    【解决方案1】:

    最好使用两个函数来完成:

    1. 一个打开文件。如果消息是单部分的,get_payload 在消息中返回字符串。如果消息是多部分的,则返回子消息列表
    2. 第二个处理文本/有效负载

    这是可以做到的:

    def parse_file_bodies(filename):
        # Opens file and parses email
        email = Parser().parse(open(filename, 'r'))
        # For multipart emails, all bodies will be handled in a loop
        if email.is_multipart():
            for msg in email.get_payload():
                parse_single_body(msg)
        else:
            # Single part message is passed diractly
            parse_single_body(email)
    
    def parse_single_body(email):
        payload = email.get_payload(decode=True)
        # The payload is binary. It must be converted to
        # python string depending in input charset
        # Input charset may vary, based on message
        try:
            text = payload.decode("utf-8")
            # Now you can work with text as with any other string:
            ...
        except UnicodeDecodeError:
            print("Error: cannot parse message as UTF-8")
            return  
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-11-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-16
      • 2017-01-22
      相关资源
      最近更新 更多