简体   繁体   English

Python imaplib无法通过地址搜索

[英]Python imaplib can't search by from address

I'm trying to find mails matching a particular FROM address. 我正在尝试查找与特定发件人地址匹配的邮件。 I've looked at this answer but it doesn't work for me. 我已经看了这个答案,但对我来说不起作用。

Here's the relevant code: 以下是相关代码:

import imaplib

conn = imaplib.IMAP4_SSL(IMAPserver)
conn.login(IMAPuserName, IMAPpassword)
retVal, data = conn.select("INBOX")
    if retVal != "OK":
        <PRINT SOME ERROR MESSAGE>
        sys.exit(1)

All this works. 所有这些工作。 Here are some variations of the search command that don't work: 以下是搜索命令无法使用的一些变体:

retVal, data = conn.search(None, 'UNSEEN HEADER FROM "foo@example.com"')

retVal, data = conn.search(None, 'UNSEEN FROM "foo@example.com"')

retVal, data = conn.search(None, 'FROM "foo@example.com"')

retVal, data = conn.search(None, 'HEADER FROM "foo@example.com"')

All of these result in errors like these: 所有这些都会导致如下错误:

    imaplib.error: SEARCH command error: BAD ['Error in IMAP command SEARCH: 
Unexpected string as search key: FROM "foo@example.com"']

I've referred to the relevant section of the IMAP v4 RFC but I can't figure out what exactly I'm doing wrong. 我已经提到了IMAP v4 RFC的相关部分,但是我无法弄清楚我到底在做什么错。 Any help would be greatly appreciated. 任何帮助将不胜感激。

I managed to get info from the guys managing the IMAP server. 我设法从管理IMAP服务器的人员那里获取信息。 This was a server problem and not something to do with the code. 这是服务器问题,与代码无关。

The IMAP server in question is a customised version of Dovecot. 有问题的IMAP服务器是Dovecot的自定义版本。 It actually indexes all mailboxes to make searching etc. faster. 它实际上索引所有邮箱,以加快搜索等速度。 In this particular case the index for my mailbox had gotten corrupted. 在这种情况下,我邮箱的索引已损坏。 Re-indexing fixed the problem. 重新索引解决了该问题。

As shown in the examples in the docmentation, IMAP.search() expects the search arguments as individual, positional arguments, not as a single string: 如文档中的示例所示, IMAP.search()期望搜索参数作为单独的位置参数,而不是单个字符串:

conn.search(None, 'UNSEEN', 'HEADER', 'FROM', 'foo@example.com')
conn.search(None, 'UNSEEN', 'FROM', 'foo@example.com')
conn.search(None, 'FROM', 'foo@example.com')
conn.search(None, 'HEADER', 'FROM', 'foo@example.com')

If you pass in a single argument, imaplib will pass it as a single quoted argument to the SEARCH command. 如果您传递单个参数,则imaplib会将其作为单个带引号的参数传递给SEARCH命令。

Again, however, you cannot rely on this, since the quoting only happens for Python 2.x imaplib . 同样,您不能依赖于此,因为仅对Python 2.x imaplib Python 3.x imaplib does not do the quoting for you and that particular bug is still open . Python 3.x imaplib不会为您提供报价,并且该特定错误仍处于打开状态

One way around that --as stated by the documentation-- is to "pre-quote" the arguments as a list: 如文档所述,一种解决方法是将参数“加引号”为列表:

conn.search(None, '(UNSEEN HEADER FROM foo@example.com)')
conn.search(None, '(UNSEEN FROM foo@example.com)')
conn.search(None, '(FROM foo@example.com)')
conn.search(None, '(HEADER FROM foo@example.com)')

But as noted by @Max, this is not supported by some IMAP servers. 但正如@Max所指出的,某些IMAP服务器不支持此功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM