如何使用Python阅读邮件的邮件正文?

时间:2020-10-15 18:09:13

标签: python python-3.x imap

登录并阅读主题作品。读取身体时发生错误。有什么错误?在互联网上,该错误始终存在于此部分:“ email.message_from_bytes(data[0][1].decode())”,但我认为该部分是正确的。

# Connection settings
        HOST = 'imap.host'
        USERNAME = 'name@domain.com'
        PASSWORD = 'password'

        m = imaplib.IMAP4_SSL(HOST, 993)
        m.login(USERNAME, PASSWORD)
        m.select('INBOX')

        result, data = m.uid('search', None, "UNSEEN")
        if result == 'OK':
              for num in data[0].split()[:5]:
                    result, data = m.uid('fetch', num, '(RFC822)')
                    if result == 'OK':
                          email_message_raw = email.message_from_bytes(data[0][1])
                          email_from = str(make_header(decode_header(email_message_raw['From'])))
                          # von Edward Chapman -> https://stackoverflow.com/questions/7314942/python-imaplib-to-get-gmail-inbox-subjects-titles-and-sender-name
                          subject = str(email.header.make_header(email.header.decode_header(email_message_raw['Subject'])))
                          # content = email_message_raw.get_payload(decode=True)
                          # von Todor Minakov -> https://stackoverflow.com/questions/17874360/python-how-to-parse-the-body-from-a-raw-email-given-that-raw-email-does-not
                          b = email.message_from_string(email_message_raw)
                          body = ""

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

                                  # skip any text/plain (txt) attachments
                                  if ctype == 'text/plain' and 'attachment' not in cdispo:
                                      body = part.get_payload(decode=True)  # decode
                                      break
                          # not multipart - i.e. plain text, no attachments, keeping fingers crossed
                          else:
                              body = b.get_payload(decode=True)
                          
        m.close()
        m.logout()


        txt = body
        regarding = subject
        print("###########################################################")
        print(regarding)
        print("###########################################################")
        print(txt)
        print("###########################################################")

错误消息:

TypeError:initial_value必须为str或None,而不是Message

感谢您的评论和回复

2 个答案:

答案 0 :(得分:0)

一切就绪。只需了解一些概念即可。

“电子邮件”库允许您使用其解析器API将典型的电子邮件字节转换为名为Message的易于使用的对象,例如message_from_bytes(),message_from_string()等。

典型错误是由于输入错误引起的。

email.message_from_bytes(数据[0] [1] .decode())

上面的函数message_from_bytes将字节作为输入而不是str。因此,解码data [0] [1]并通过解析器API输入是多余的。

简而言之,您尝试使用message_from_bytes(data [0] [1])和message_from_string(email_message_raw)两次解析原始电子邮件。摆脱其中之一,您将大功告成!

尝试这种方法:

    HOST = 'imap.host'
    USERNAME = 'name@domain.com'
    PASSWORD = 'password'

    m = imaplib.IMAP4_SSL(HOST, 993)
    m.login(USERNAME, PASSWORD)
    m.select('INBOX')

    result, data = m.uid('search', None, "UNSEEN")
    if result == 'OK':
          for num in data[0].split()[:5]:
                result, data = m.uid('fetch', num, '(RFC822)')
                if result == 'OK':
                      email_message = email.message_from_bytes(data[0][1])
                      email_from = str(make_header(decode_header(email_message_raw['From'])))
                      # von Edward Chapman -> https://stackoverflow.com/questions/7314942/python-imaplib-to-get-gmail-inbox-subjects-titles-and-sender-name
                      subject = str(email.header.make_header(email.header.decode_header(email_message_raw['Subject'])))
                      # content = email_message_raw.get_payload(decode=True)
                      # von Todor Minakov -> https://stackoverflow.com/questions/17874360/python-how-to-parse-the-body-from-a-raw-email-given-that-raw-email-does-not
                      # b = email.message_from_string(email_message_raw)
                      # this is already set as Message object which have many methods (i.e. is_multipart(), walk(), etc.)
                      b = email_message 
                      body = ""

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

                              # skip any text/plain (txt) attachments
                              if ctype == 'text/plain' and 'attachment' not in cdispo:
                                  body = part.get_payload(decode=True)  # decode
                                  break
                      # not multipart - i.e. plain text, no attachments, keeping fingers crossed
                      else:
                          body = b.get_payload(decode=True)
                      
    m.close()
    m.logout()


    txt = body
    regarding = subject
    print("###########################################################")
    print(regarding)
    print("###########################################################")
    print(txt)
    print("###########################################################")

答案 1 :(得分:0)

from imap_tools import MailBox, AND

# get email bodies from INBOX 
with MailBox('imap.mail.com').login('test@mail.com', 'password', 'INBOX') as mailbox:
    for msg in mailbox.fetch():
        body = msg.text or msg.html
    

https://github.com/ikvk/imap_tools

相关问题