9
 import imaplib,time
 T=time.time()
 M=imaplib.IMAP4_SSL("imap.gmail.com")
 M.login(user,psw)
 M.select() 
 typ, data = M.search(None, 'UNSEEN SINCE T')
 for num in string.split(data[0]):
    try :
       typ, data=M.fetch(num,'(RFC822)')
       msg=email.message_from_string(data[0][1])
       print msg["From"]
       print msg["Subject"]  
       print msg["Date"]  
   except Exception,e:
      print "hello world"
M.close()
M.logout()

错误:

Traceback (most recent call last):
File "mail.py", line 37, in <module>
 typ, data = M.search(None, 'UNSEEN SINCE T')
 File "/usr/lib/python2.7/imaplib.py", line 627, in search
 typ, dat = self._simple_command(name, *criteria)
 File "/usr/lib/python2.7/imaplib.py", line 1070, in _simple_command
 return self._command_complete(name, self._command(name, *args))
 File "/usr/lib/python2.7/imaplib.py", line 905, in _command_complete
 raise self.error('%s command error: %s %s' % (name, typ, data))
 imaplib.error: SEARCH command error: BAD ['Parse command error']

我想搜索特定时间以来的电子邮件。这是我的代码。但它运行错误。你能给我一些关于如何解决它的建议。非常感谢!

4

2 回答 2

28
import imaplib

mail = imaplib.IMAP4_SSL('imap.gmail.com')
mail.login('test@gmail.com', 'test')
mail.list()
# Out: list of "folders" aka labels in gmail.
mail.select("inbox") # connect to inbox.

result, data = mail.search(None, '(FROM "anjali sinha" SUBJECT "test")' )

ids = data[0] # data is a list.
id_list = ids.split() # ids is a space separated string
latest_email_id = id_list[-1] # get the latest

result, data = mail.fetch(latest_email_id, "(RFC822)") # fetch the email body (RFC822)             for the given ID

raw_email = data[0][1] # here's the body, which is raw text of the whole email
# including headers and alternate payloads

打印 raw_email

这最终对我有条件地指定标签有用

于 2014-01-04T06:56:33.790 回答
0

更新: OP 已导入 imaplib 但它仍在生成一条错误消息,该消息尚未放入问题中。

--

这不起作用,因为您还没有导入 imaplib。

尝试

import smtplib, time, imaplib

代替

import smtplib, time
于 2013-09-25T09:38:02.683 回答