2016-08-03 10 views
1

Ich habe diesen Python-Code:Python imap4 Extrakt Thema Mails

import imaplib 
import email, sys 
imaplib._MAXLINE = 40000 

mail = imaplib.IMAP4('imapmail.libero.it') 
mail.login('[email protected]', '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() 

der Code Rückkehr dieser Fehler:

'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__' 

ich alle Themen aus Posteingang extrahieren müssen und in Textdatei schreiben. In einem anderen E-Mail-Dienst funktioniert mein Code ohne Probleme. Wie kann ich dieses Problem lösen? Wo ist das Problem ?

Antwort

0

Wenn Sie das tun ...

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

... data hält message sequence numbers, nicht uids. Nachrichtensequenznummern und UIDs sind nicht identisch.

Also, Ihr Code zu beheben, ersetzen Sie die obige Zeile mit:

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

Eine UID ist eine eindeutige Kennung, während eine Reihe Nachrichtenfolge wird nicht im Laufe der Zeit ändern kann sich ändern, wenn der Gehalt an Die Mailbox ändert sich.

Sie können mehr über die Attribute lesen UID und Message Sequence Numbers hier: https://tools.ietf.org/html/rfc3501