繁体   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