'`KeyError: b'BODY[]'` in Python script to extract text from email

I'm working on a Python script to extract text from an email and the script so far is:

import imapclient

import imaplib

import pyzmail

i=imapclient.IMAPClient('imap.gmail.com',ssl=True)

i.login('my email', 'my password')

i.select_folder('INBOX',readonly=False)

imaplib._MAXLINE = 1000000

uids=i.search('SINCE 01-MAY-2022')

rawmsgs = i.fetch(uids, ['BODY[]','FLAGS'])

msg = pyzmail.PyzMessage.factory(rawmsgs[1][b'BODY[]'])

However one error always pops up

msg = pyzmail.PyzMessage.factory(rawmsgs[1][b'BODY[]'])
KeyError: b'BODY[]'

I have tried using Body[] instead of b'Body[]', but it still shows the same error.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source