使用 utf-8 编码的主题的 Python IMAP 搜索 [英] Python IMAP search using a subject encoded with utf-8

查看:22
本文介绍了使用 utf-8 编码的主题的 Python IMAP 搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此问题与问题 Python 有关使用 iso-8859-1 编码的主题进行 IMAP 搜索,但那里给出的回复对我不起作用.

This question is related to question Python IMAP search using a subject encoded with iso-8859-1, but the reply given there is not working for me.

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

I am doing the following IMAP search in python:

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']

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

Why is that? How can I solve this problem?

推荐答案

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')

这篇关于使用 utf-8 编码的主题的 Python IMAP 搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆