2013-09-25 84 views
4
​​

錯誤:如何使用python imaplib.IMAP4.search()搜索特定的電子郵件

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

我要搜索電子郵件,因爲一個特定的時間。這是我的代碼,但它運行錯誤。可以給我一些關於如何解決它的建議。感謝很多!

+0

郵政錯誤工作對我來說 specyfing標籤。 –

+0

不要以這種方式捕捉你的異常,讓他們冒泡,讓你可以調試出了什麼問題。 – Owen

回答

0

更新:OP已導入imaplib,但它仍然產生一個錯誤消息,但沒有被放入問題。

-

這不起作用,因爲您還沒有導入imaplib。

嘗試,而不是

import smtplib, time, imaplib 

import smtplib, time 
+0

對不起,我已經導入imaplib.but但它也沒有工作。 – yayun

+0

在這種情況下,請發佈您看到的錯誤 – Owen

+0

我已經發布了它,非常感謝 – yayun

10
import imaplib 

mail = imaplib.IMAP4_SSL('imap.gmail.com') 
mail.login('[email protected]', '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

這最終與條件

相關問題