2016-08-03 59 views
1

我有這個Python代碼:蟒蛇IMAP4提取主題郵件

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() 

代碼返回此錯誤:

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

我需要從收件箱中提取出所有科目和文本文件描述。在另一個電子郵件服務中,我的代碼沒有問題。我該如何解決這個問題?哪裏有問題 ?

回答

0

當你這樣做......

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

... data持有message sequence numbers,不uids。消息序號和UID不一樣。

所以,修正代碼,代替與上述行:

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

的UID是一個唯一的標識符,不會隨時間改變而 消息序列號可能會改變,每當內容郵箱 更改。

你可以閱讀更多關於屬性UIDMessage Sequence Numbers這裏:https://tools.ietf.org/html/rfc3501