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

How can I read the mail body of a mail with Python?(如何使用Python读取邮件的邮件正文?)
本文介绍了如何使用Python读取邮件的邮件正文?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

登录并阅读主题作品。读取正文时出错。错误是什么?在互联网上,错误总是在这一部分:email.message_from_bytes(data[0][1].decode())&qot;,但我认为这部分是正确的。

# 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必须是字符串或无,而不是消息

感谢您的评论和回复

推荐答案

一切就绪。只需理解几个概念。

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

典型错误是由于输入错误。

email.message_from_bytes(data[0][1].decode())

上述函数MESSAGE_FROM_BYTES接受字节作为输入,而不是字符串。因此,解码数据[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("###########################################################")

这篇关于如何使用Python读取邮件的邮件正文?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Leetcode 234: Palindrome LinkedList(Leetcode 234:回文链接列表)
How do I read an Excel file directly from Dropbox#39;s API using pandas.read_excel()?(如何使用PANDAS.READ_EXCEL()直接从Dropbox的API读取Excel文件?)
subprocess.Popen tries to write to nonexistent pipe(子进程。打开尝试写入不存在的管道)
I want to realize Popen-code from Windows to Linux:(我想实现从Windows到Linux的POpen-code:)
Reading stdout from a subprocess in real time(实时读取子进程中的标准输出)
How to call type safely on a random file in Python?(如何在Python中安全地调用随机文件上的类型?)