【问题标题】:Python imaplib search 'NOT' keywordPython imaplib 搜索“NOT”关键字
【发布时间】:2016-10-18 23:09:12
【问题描述】:

我一直在学习将 python imaplib 库用于我正在从事的项目,但我看到一些让我难过的行为。当我尝试使用“NOT FROM”搜索时,它返回的结果与“FROM”相同,就好像 not 甚至不存在一样。我的假设是我只是把命令放错了,但我搜索了几个例子,我发现的每一个似乎都是按照我的方式做的。

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 说它是基于和一些更新的。

$ 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"

我得到了预期的行为。

我猜这是“解决”了

【问题讨论】:

  • 我检查了你的代码,没问题。
  • 谢谢!但我不确定你的意思是什么,我知道它运行得很好。问题是 NOT FROM "foo" 返回的结果与 FROM "foo" 相同。你的意思是这没有发生在你身上? (您是否在 yahoo 邮件上尝试过?因为这似乎是问题的根源。)
  • 我尝试使用 Gmail ,但使用 NOT FROM 'gmail' 它让我:('OK', ['21 23 24 25 35 37 38 41 42']) 和 FROM 'gmail' : ( 'OK', ['12 13 14 15 16 17 18 19 20 22 26 27 28 29 30 31 32 33 34 36'])
  • 是的!使用 gmail 也为我工作,我检查了每一个的“功能”,似乎 Yahoo imap 不完全支持搜索,而 gmail 支持。
  • NOT (FROM "gmail") 工作吗?

标签: python imap imaplib


【解决方案1】:

我对此进行了更深入的研究,这似乎是我正在连接的 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"

我得到了预期的行为。

我猜这是“解决”了

【讨论】:

    【解决方案2】:

    NOT FROM gmail 在 Gmail imap 中可以正常工作,但在 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,请查看:

    https://www.rfc-editor.org/rfc/rfc3501.html

    https://datatracker.ietf.org/doc/html/draft-ietf-imapext-regex-00

    【讨论】:

    • 我想出了一个类似的技巧,但我只是让它们设置,因为顺序对我来说并不重要 :) 在我看来,雅虎在工作状态下没有 SEARCH 有点傻!
    • @user3591723 我就是这样找到的。希望它对你有用。 ;)
    猜你喜欢
    • 2016-03-07
    • 2011-10-03
    • 2013-08-05
    • 2020-05-12
    • 1970-01-01
    • 2017-04-05
    • 1970-01-01
    • 2021-07-09
    • 1970-01-01
    相关资源
    最近更新 更多