简体   繁体   中英

python imap4 extract subject mails

i have this python code :

import imaplib
import email, sys
imaplib._MAXLINE = 40000

mail = imaplib.IMAP4('imapmail.libero.it')
mail.login('username@libero.it', 'password')
mail.list()
mail.select('inbox')

result, data = mail.search(None, 'All')
out = open('output.txt', 'a', 0)
for latest_email_uid in data[0].split():
    try:
        result, data = mail.uid('fetch', latest_email_uid, '(RFC822)')
        raw_email = data[0][1]
        email_message = email.message_from_string(raw_email)
        tmp = email_message['Subject']
        tmp = tmp.strip().replace('\r','').replace('\n','')+'\n'
        sys.stdout.write("\r"+tmp)
        out.write(tmp.strip() + '\n')
    except Exception as e:
        print e

mail.close()
out.close()

the code return this error :

'NoneType' object has no attribute '__getitem__'
'NoneType' object has no attribute '__getitem__'
'NoneType' object has no attribute '__getitem__'
"Samsung MZ-7KE1T0BW SSD 850 PRO..."
'NoneType' object has no attribute '__getitem__'
'NoneType' object has no attribute '__getitem__'
'NoneType' object has no attribute '__getitem__'
Promozione finestre termiche in pvc Gruppo Re
Il Giubileo di Papa Francesco
'NoneType' object has no attribute '__getitem__'
'NoneType' object has no attribute '__getitem__'
'NoneType' object has no attribute '__getitem__'
'NoneType' object has no attribute '__getitem__'
'NoneType' object has no attribute '__getitem__'
'NoneType' object has no attribute '__getitem__'
'NoneType' object has no attribute '__getitem__'
'NoneType' object has no attribute '__getitem__'
'NoneType' object has no attribute '__getitem__'
'NoneType' object has no attribute '__getitem__'
'NoneType' object has no attribute '__getitem__'
'NoneType' object has no attribute '__getitem__'
'NoneType' object has no attribute '__getitem__'
'NoneType' object has no attribute '__getitem__'
'NoneType' object has no attribute '__getitem__'
'NoneType' object has no attribute '__getitem__'
'NoneType' object has no attribute '__getitem__'
'NoneType' object has no attribute '__getitem__'
'NoneType' object has no attribute '__getitem__'
'NoneType' object has no attribute '__getitem__'
'NoneType' object has no attribute '__getitem__'
'NoneType' object has no attribute '__getitem__'
'NoneType' object has no attribute '__getitem__'
'NoneType' object has no attribute '__getitem__'
'NoneType' object has no attribute '__getitem__'
'NoneType' object has no attribute '__getitem__'
'NoneType' object has no attribute '__getitem__'
'NoneType' object has no attribute '__getitem__'
'NoneType' object has no attribute '__getitem__'
'NoneType' object has no attribute '__getitem__'
'NoneType' object has no attribute '__getitem__'
'NoneType' object has no attribute '__getitem__'
'NoneType' object has no attribute '__getitem__'
'NoneType' object has no attribute '__getitem__'
'NoneType' object has no attribute '__getitem__'
'NoneType' object has no attribute '__getitem__'
'NoneType' object has no attribute '__getitem__'
'NoneType' object has no attribute '__getitem__'
'NoneType' object has no attribute '__getitem__'
'NoneType' object has no attribute '__getitem__'
'NoneType' object has no attribute '__getitem__'
'NoneType' object has no attribute '__getitem__'
'NoneType' object has no attribute '__getitem__'
'NoneType' object has no attribute '__getitem__'
'NoneType' object has no attribute '__getitem__'
'NoneType' object has no attribute '__getitem__'
'NoneType' object has no attribute '__getitem__'
'NoneType' object has no attribute '__getitem__'
'NoneType' object has no attribute '__getitem__'
'NoneType' object has no attribute '__getitem__'
'NoneType' object has no attribute '__getitem__'
'NoneType' object has no attribute '__getitem__'
'NoneType' object has no attribute '__getitem__'
'NoneType' object has no attribute '__getitem__'
'NoneType' object has no attribute '__getitem__'
'NoneType' object has no attribute '__getitem__'
'NoneType' object has no attribute '__getitem__'
'NoneType' object has no attribute '__getitem__'
'NoneType' object has no attribute '__getitem__'
'NoneType' object has no attribute '__getitem__'
'NoneType' object has no attribute '__getitem__'

i need extract all subjects from inbox and write in text file. in another email service my code works without problem. how i can resolve this problem ? Where is the problem ?

When you do...

result, data = mail.search(None, 'All')

... data holds message sequence numbers , not uids . Message sequence numbers and UIDs are not the same.

So, to fix your code, replace the above line with:

result, data = mail.uid('search', None, 'All')

An UID is a unique identifier that will not change over time while a message sequence number may change whenever the content of the mailbox changes.

You can read more about the attributes UID and Message Sequence Numbers here: https://tools.ietf.org/html/rfc3501

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM