I have the following code:
import imaplib
import email
imap = imaplib.IMAP4_SSL('imap.naver.com')
id = 'abc@naver.com'
pw = '123'
imap.login(id, pw)
imap.select('inbox')
status, data = imap.uid('search', None, '(HEADER FROM "abd@gmail.com")')
if status == 'OK':
if data[0]:
mid = data[0].split()[0]
print('mail id', mid)
print(imap.fetch(mid, '(UID BODY[HEADER.FIELDS (HEADER FROM)])'))
It raised
FETCH command error: BAD [b'Error in IMAP command FETCH: Invalid messageset']
print (status, data)
It returned
OK [b'3769 3838 3845 3896 3907 3916 3961 3978 3989 3991 3993 4002 4017 4059 4069 4607 4608 4612 4613']
what is wrong in my code?
How can I read specific email?
I think it's just a matter of encoding. Try converting it from bytes to string:
mid = mid.decode('ASCII')
For the full data set, try:
msgnums = data[0].decode('ASCII').replace(' ',',')
Hope this helps! :)