使用utf8编码的主题的Python IMAP搜索

2024-09-30 12:28:20 发布

您现在位置:Python中文网/ 问答频道 /正文

这个问题与问题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"))

我得到了以下例外:

^{pr2}$

为什么?我怎样才能解决这个问题?在


Tags: selfsearchdatawithisoutfencodesubject
3条回答
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

然后:

^{pr2}$

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文本。在

这个对我有用

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

谢谢,李!在

相关问题 更多 >

    热门问题