我正在尝试阅读其中包含html内容的邮件。我正在使用message_from_bytes从邮件中读取数据。
def ParseEmail(email_user,email_pass,host,port,pdf):
message_list = []
mail = imaplib.IMAP4_SSL(host, port)
mail.login(email_user, email_pass)
mail.select()
typ, data = mail.search(None, 'unseen')
mail_ids = data[0]
id_list = mail_ids.split()
for num in id_list:
typ, data = mail.fetch(num, '(RFC822)')
raw = email.message_from_bytes(data[0][1])
raw_email = data[0][1]
raw_email_string = raw_email.decode('utf-8')
email_message = email.message_from_string(raw_email_string)
soup = BeautifulSoup(get_body(raw))
content = soup.get_text('\n').replace('\xa0:\n\xa0\n', ' ')
message_list.append(content)
代码编译并运行无任何错误。当使用IPython控制台检查时,也会打印message_list数组。但是,当我通过驱动程序函数调用它时,出现错误:
Traceback (most recent call last):
File "parseemail.py", line 395, in <module>
main()
File "/Users/test/dev/Email/parseemail.py", line 384, in main
pe = ParseEmail(email_user, email_pass, host, port,pdf)
File "/Users/test/dev/Email/parseemail.py", line 320, in ParseEmail
raw = email.message_from_bytes(data[0][1])
AttributeError: 'module' object has no attribute 'message_from_bytes'
可能导致什么错误? (所有软件包都已安装并经过测试)。