簡體   English   中英

Python imaplib搜索“ NOT”關鍵字

[英]Python imaplib search 'NOT' keyword

我一直在學習將python imaplib庫用於我正在處理的項目,但是我發現有些行為使我感到困惑。 當我嘗試使用“ NOT FROM”進行搜索時,它返回的內容與“ FROM”相同,好像還沒有出現。 我的假設是我只是在錯誤輸入命令,但是我搜索了一些示例,發現的每個示例似乎都是按照我的方式做的。

import imaplib
mail = imaplib.IMAP4_SSL(host)
mail.login(email, pw)
mail.select(mailbox)
# The following two work as expeceted
# and give different results
mail.uid('search', None, '(NOT NEW)')
mail.uid('search', None, '(NEW)')
# However the following two give the same
mail.uid('search', None, '(FROM gmail)')
mail.uid('search', None, '(NOT FROM gmail)')

我已經嘗試了所有可以想到的輸入方法,但是似乎都沒有用。 我已經檢查了RFC2060,一個imaplib表示它是基於RFC2060以及一些最近的版本。

$ python --version
Python 2.7.5+

有人看到我在搞砸嗎,還是也看到了這個問題? 似乎關於這個特定庫的資料很少


編輯:我已經對此進行了進一步研究,這似乎是我要連接到的imap服務器的一個問題。在嘗試通過openssl與

$ openssl s_client -connect imap.mail.yahoo.com:993 -crlf

然后登錄並嘗試

UID SEARCH FROM "gmail"
UID SEARCH NOT FROM "gmail"

我得到相同的行為。

但是,當我連接到Gmail帳戶時

$ openssl s_client -connect imap.gmail.com:993 -crlf

並嘗試

UID SEARCH FROM "gmail"
UID SEARCH NOT FROM "gmail"

我得到了預期的行為。

我想這是“已解決”

在Gmail imap中, NOT FROM gmail不能正常工作,但是Yahoo不能正常工作。

試試吧:

import imaplib
## connect to imap server
mail = imaplib.IMAP4_SSL('imap.mail.yahoo.com')
mail.login('username','password')
mail.select('inbox')

## search all mails
all_mail = mail.search(None,'ALL')

## search all gmails
all_gmail = mail.search(None,'(FROM "gmail")')

## make them as list of int
all_mail = [x for x in all_mail[1][0].split() if x != ""]
all_gmail = [x for x in all_gmail[1][0].split() if x != ""]
not_gmail = [x for x in all_mail if x not in all_gmail]
#not_gmail = set(all_mail) - set(all_gmail)    

## get the output
print "all mail: " + str(all_mail)
print "all gmail: " + str(all_gmail)
print "all not gmail: " + str(not_gmail)

有點慢,但是效果很好。 對於RFC,請查看以下內容:

http://tools.ietf.org/html/rfc3501.html

http://tools.ietf.org/html/draft-ietf-imapext-regex-00

我對此進行了進一步的研究,這似乎是我要連接的imap服務器的一個問題。在嘗試通過openssl與

$ openssl s_client -connect imap.mail.yahoo.com:993 -crlf

然后登錄並嘗試

UID SEARCH FROM "gmail"
UID SEARCH NOT FROM "gmail"

我得到相同的行為。

但是,當我連接到Gmail帳戶時

$ openssl s_client -connect imap.gmail.com:993 -crlf

並嘗試

UID SEARCH FROM "gmail"
UID SEARCH NOT FROM "gmail"

我得到了預期的行為。

我想這是“已解決”

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM