Python IMAPLIB HTML body parsing

半腔热情 提交于 2019-12-02 12:03:29

Comment: The "body" returned by imap.fetch are usually bytes, not a string, which throws an exception

Change to:

msg = email.message_from_bytes(body)

Question: I cant get it do get the body of the email

For example:

import email, imaplib

username = 'xxx.xxx@xxx.xx'
password = 'xxxxxx'
imap = imaplib.IMAP4_SSL('imap-mail.outlook.com')
imap.login(username, password)
imap.select("inbox")

resp, items = imap.search(None, "(UNSEEN)")

for n, num in enumerate(items[0].split(), 1):
    resp, data = imap.fetch(num, '(RFC822)')

    body = data[0][1]
    msg = email.message_from_string(body)
    content = msg.get_payload(decode=True)

    print("Message content[{}]:{}".format(n, content))
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!