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].decode('utf-8')
at this point I get the error message
AttributeError: 'str' object has no attribute 'decode'
Python 3 doesn't have decode anymore, am I right? how can I fix this?
Also, in:
data = conn.fetch('1', '(BODY[HEADER])')
I am selecting only the 1st email. How do I select all?
Use it by this Method:
It s already decoded in Python3, Try directly it should work.
You are trying to decode an object that is already decoded. You have a
str
, there is no need to decode from UTF-8 anymore.Simply drop the
.decode('utf-8')
part:As for your
fetch()
call, you are explicitly asking for just the first message. Use a range if you want to retrieve more messages. See the documentation:I'm not familiar with the library, but if your problem is that you don't want a byte array, one easy way is to specify an encoding type straight in a cast:
Begin with Python 3, all string is unicode object.
the code before are same. So I think you should remove the
.decode('utf-8')
. Because you have already get the unicode object.For Python3