Python IMAP搜索使用utf-8编码的主题



这个问题与使用iso-8859-1编码的主题进行Python IMAP搜索的问题有关,但给出的答复对我不起作用。

我正在python中进行以下IMAP搜索:

typ, data = self.M.search("utf-8", "(SUBJECT %s)" % u"réception".encode("utf-8"))

我得到以下例外:

...
    typ, data = self.M.search("utf-8", "(SUBJECT %s)" % u"réception".encode("utf-8"))
  File "/usr/local/python/2.7.2/lib/python2.7/imaplib.py", line 625, in search
    typ, dat = self._simple_command(name, 'CHARSET', charset, *criteria)
  File "/usr/local/python/2.7.2/lib/python2.7/imaplib.py", line 1070, in _simple_command
    return self._command_complete(name, self._command(name, *args))
  File "/usr/local/python/2.7.2/lib/python2.7/imaplib.py", line 905, in _command_complete
    raise self.error('%s command error: %s %s' % (name, typ, data))
error: SEARCH command error: BAD ['Could not parse command']

为什么?我该如何解决这个问题?

import imaplib
import getpass
email = "XXXXXXX@gmail.com"
sock = imaplib.IMAP4_SSL("imap.gmail.com", 993)
sock.login(email, getpass.getpass())
# select the correct mailbox...
sock.select()
# turn on debugging if you like
sock.debug = 4

然后:

# use the undocumented IMAP4.literal attribute
sock.literal = "réception"
sock.uid('SEARCH', 'CHARSET', 'UTF-8', 'SUBJECT')

u"réception"将需要用引号包装:u'"réception"',因为IMAPLIB不会在列表中为您的字符串加引号。

更新:我无法让gmail的IMAP实现接受一个带引号的字符串,不得不使用IMAP文本语法。我不确定这是我使用socat编码的限制,还是gmail的限制。

a UID SEARCH CHARSET utf-8 SUBJECT "réception"
a BAD Could not parse command
a UID SEARCH CHARSET utf-8 SUBJECT {10}
+ go ahead
réception
* SEARCH
a OK SEARCH completed (Success)

不幸的是,imaplib并没有提供任何强制使用IMAP文本的方法。

外部库https://github.com/ikvk/imap_tools支持按编码数据进行搜索

from imap_tools import MailBox, A
# get list of emails that subject contains "réception" from INBOX folder
with MailBox('imap.mail.com').login('test@mail.com', 'pwd') as mailbox:
    for msg in mailbox.fetch(A(subject='réception'), charset='utf8'):
        print(msg.subject)

这个适用于我

# use the undocumented IMAP4.literal attribute
sock.literal = u"réception".encode('utf-8')
sock.uid('SEARCH', 'CHARSET', 'UTF-8', 'SUBJECT')

谢谢李!

最新更新