这个问题与问题Python IMAP search using a subject encoded with iso-8859-1有关,但那里给出的答复对我不起作用。
我在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']
为什么?我该如何解决这个问题?
答案 0 :(得分:4)
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')
答案 1 :(得分:2)
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文字的任何方法。
答案 2 :(得分:0)
外部库 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)
答案 3 :(得分:-1)
这个对我有用
# use the undocumented IMAP4.literal attribute
sock.literal = u"réception".encode('utf-8')
sock.uid('SEARCH', 'CHARSET', 'UTF-8', 'SUBJECT')
谢谢,李!