0

I want to list all the email subject in my gmail account.

import imaplib
import email
user="xxxx"
password="yyyy"
con=imaplib.IMAP4_SSL('imap.gmail.com')
con.login(user,password)
num=int(con.select("[Gmail]/&YkBnCZCuTvY-")[1][0])

The num is 1200,there are 1200 emails in my gmail account. I want to list all the email subject.If i want to get the first email subject ,

result,data=con.fetch(b'1', '(RFC822)')
print email.message_from_string(data[0][1])["Subject"]

The two lines can get it. But i can't list all the email subject as the following,how to fix it?

for item in range(num+1):
    result,data=con.fetch(str(item), '(RFC822)')
    print email.message_from_string(data[0][1])["Subject"]
0

1 Answer 1

0

Try modifying your range in your for loop. I believe the message ids in fetch() are '1 based'

for item in range(1,num+1):

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.