[英]Python IMAP: =?utf-8?Q? in subject string
I am displaying new email with IMAP
, and everything looks fine, except for one message subject shows as:我正在使用
IMAP
显示新电子邮件,一切看起来都很好,除了一个消息主题显示为:
=?utf-8?Q?Subject?=
How can I fix it?我该如何解决?
In MIME terminology, those encoded chunks are called encoded-words.在 MIME 术语中,那些编码的块称为编码词。 You can decode them like this:
您可以像这样解码它们:
import email.Header
text, encoding = email.Header.decode_header('=?utf-8?Q?Subject?=')[0]
Check out the docs for email.Header
for more details.查看
email.Header
的文档以获取更多详细信息。
This is a MIME encoded-word .这是一个 MIME编码字。 You can parse it with
email.header
:你可以用
email.header
解析它:
import email.header
def decode_mime_words(s):
return u''.join(
word.decode(encoding or 'utf8') if isinstance(word, bytes) else word
for word, encoding in email.header.decode_header(s))
print(decode_mime_words(u'=?utf-8?Q?Subject=c3=a4?=X=?utf-8?Q?=c3=bc?='))
In Python 3.3+, the parsing classes and functions in email.parser automatically decode "encoded words" in headers if their policy
argument is set to policy.default
在 Python 3.3+ 中,email.parser 中的解析类和函数会自动解码标题中的“编码词”,如果它们的
policy
参数设置为policy.default
>>> import email
>>> from email import policy
>>> msg = email.message_from_file(open('message.txt'), policy=policy.default)
>>> msg['from']
'Pepé Le Pew <pepe@example.com>'
The parsing classes and functions are:解析类和函数是:
Confusingly, up to at least Python 3.8, the default policy for these parsing functions is not policy.default
, but policy.compat32
, which does not decode "encoded words".令人困惑的是,高达至少3.8的Python,这些解析函数的默认策略不
policy.default
,但policy.compat32
,这不解码“编码字”。
>>> msg = email.message_from_file(open('message.txt'))
>>> msg['from']
'=?utf-8?q?Pep=C3=A9?= Le Pew <pepe@example.com>'
Because imaplib
is a very excessive low level library and returns results which are hard to work with因为
imaplib
是一个非常多的低级库并且返回imaplib
结果
Installation安装
pip install imbox
Usage用法
from imbox import Imbox
with Imbox('imap.gmail.com',
username='username',
password='password',
ssl=True,
ssl_context=None,
starttls=False) as imbox:
all_inbox_messages = imbox.messages()
for uid, message in all_inbox_messages:
message.subject
In Python 3, decoding this to an approximated string is as easy as:在 Python 3 中,将其解码为近似字符串非常简单:
from email.header import decode_header, make_header
decoded = str(make_header(decode_header("=?utf-8?Q?Subject?=")))
See the documentation of decode_header
and make_header
.请参阅
decode_header
和make_header
的文档。
High level IMAP lib may be useful here: imap_tools高级 IMAP 库在这里可能有用: imap_tools
from imap_tools import MailBox, AND
# get list of email subjects from INBOX folder
with MailBox('imap.mail.com').login('test@mail.com', 'pwd', 'INBOX') as mailbox:
subjects = [msg.subject for msg in mailbox.fetch()]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.