Python IMAP:=?utf-8?Q?在主题字符串中

发布于 2024-11-29 19:04:25 字数 119 浏览 2 评论 0 原文

我正在使用 IMAP 显示新电子邮件,一切看起来都很好,除了一个邮件主题显示为:

=?utf-8?Q?Subject?=

我该如何修复它?

I am displaying new email with IMAP, and everything looks fine, except for one message subject shows as:

=?utf-8?Q?Subject?=

How can I fix it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

冧九 2024-12-06 19:04:25

在 MIME 术语中,这些编码块称为编码字。您可以像这样解码它们:

import email.header
text, encoding = email.header.decode_header('=?utf-8?Q?Subject?=')[0]

查看 email.header 了解更多详细信息。

In MIME terminology, those encoded chunks are called encoded-words. 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.

时间你老了 2024-12-06 19:04:25

文本被编码为 MIME 编码字。这是 RFC2047 中定义的一种机制,用于对包含非 ASCII 文本的标头进行编码,以便编码输出仅包含 ASCII 字符。

在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 <[email protected]>'

解析类和函数为:

令人困惑的是,至少在 Python 3.10 之前,这些解析函数的默认策略不是 policy.default,但是policy.compat32,它解码“编码的单词”。

>>> msg = email.message_from_file(open('message.txt'))
>>> msg['from']
'=?utf-8?q?Pep=C3=A9?= Le Pew <[email protected]>'

The text is encoded as a MIME encoded-word. This is a mechanism defined in RFC2047 for encoding headers that contain non-ASCII text such that the encoded output contains only ASCII characters.

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

>>> import email
>>> from email import policy

>>> msg = email.message_from_file(open('message.txt'), policy=policy.default)
>>> msg['from']
'Pepé Le Pew <[email protected]>'

The parsing classes and functions are:

Confusingly, up to at least Python 3.10, the default policy for these parsing functions is not policy.default, but policy.compat32, which does not decode "encoded words".

>>> msg = email.message_from_file(open('message.txt'))
>>> msg['from']
'=?utf-8?q?Pep=C3=A9?= Le Pew <[email protected]>'
一个人的旅程 2024-12-06 19:04:25

这是一个 MIME 编码字。您可以使用 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?='))

This is a MIME encoded-word. You can parse it with 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?='))
春风十里 2024-12-06 19:04:25

尝试 Imbox

因为 imaplib 是一个非常低级的库,返回的结果是很难使用

安装

pip install imbox

使用

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

Try Imbox

Because imaplib is a very excessive low level library and returns results which are hard to work with

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
心安伴我暖 2024-12-06 19:04:25

在 Python 3 中,将其解码为近似字符串非常简单:

from email.header import decode_header, make_header

decoded = str(make_header(decode_header("=?utf-8?Q?Subject?=")))

请参阅 decode_headermake_header

In Python 3, decoding this to an approximated string is as easy as:

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.

智商已欠费 2024-12-06 19:04:25

高级 IMAP 库可能在这里有用: imap_tools

from imap_tools import MailBox, AND

# get list of email subjects from INBOX folder
with MailBox('imap.mail.com').login('[email protected]', 'pwd', 'INBOX') as mailbox:
    subjects = [msg.subject for msg in mailbox.fetch()]
  • 已解析的电子邮件消息属性
  • 用于搜索电子邮件的查询构建器
  • 电子邮件操作:复制、删除、标记、移动、查看
  • 文件夹操作:列表、设置、获取、创建、存在、重命名、删除、状态
  • 无依赖性

High level IMAP lib may be useful here: imap_tools

from imap_tools import MailBox, AND

# get list of email subjects from INBOX folder
with MailBox('imap.mail.com').login('[email protected]', 'pwd', 'INBOX') as mailbox:
    subjects = [msg.subject for msg in mailbox.fetch()]
  • Parsed email message attributes
  • Query builder for searching emails
  • Actions with emails: copy, delete, flag, move, seen
  • Actions with folders: list, set, get, create, exists, rename, delete, status
  • No dependencies
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文