python imap4提取主题邮件

时间:2016-08-03 12:00:01

标签: python

我有这个python代码:

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

代码返回此错误:

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

我需要从收件箱中提取所有主题并写入文本文件。在另一个电子邮件服务中我的代码没有问题。我怎么能解决这个问题?问题在哪里?

1 个答案:

答案 0 :(得分:0)

当你这样做......

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

... data拥有message sequence numbers,而不是uids。消息序列号和UID不相同。

因此,要修复代码,请将以上行替换为:

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

UID是一个唯一的标识符,在a时不会随时间而变化   邮箱的内容可能会更改邮件序列号   变化。

您可以在此处详细了解UIDMessage Sequence Numbers属性:https://tools.ietf.org/html/rfc3501