Here is my code:
import imaplib
from email.parser import HeaderParser
conn = imaplib.IMAP4_SSL('imap.gmail.com')
conn.login('example@gmail.com', 'password')
conn.select()
conn.search(None, 'ALL')
data = conn.fetch('1', '(BODY[HEADER])')
header_data = data[1][0][1]
newdata = header_data.decode('utf-8')
parser = HeaderParser()
msg = parser.parsestr(newdata)
print (msg.keys())
This prints out the keys(headers) : Mime-version, x-no-auto-attachment, received, date, messageID, subject, from. to, content type.
However there is 2 missing, which are X-GM-THRID and X-Gmail-Labels? Any ideas why and how can i solve this?