简体   繁体   English

使用 iso-8859-1 编码的主题进行 Python IMAP 搜索

[英]Python IMAP search using a subject encoded with iso-8859-1

From a different account, I sent myself an email with the subject Test de réception en local .我从另一个帐户向自己发送了一封电子邮件,主题为Test de réception en local Now using IMAP, I want to find that email searching by subject.现在使用 IMAP,我想找到按主题搜索的电子邮件。

When doing a search for ALL and finding the email among the output, I see:在搜索ALL并在输出中找到电子邮件时,我看到:
Subject: =?ISO-8859-1?Q?Test_de_r=E9ception_en_local?=

So now, searching with imap, I try:所以现在,用 imap 搜索,我尝试:

M = imaplib.IMAP4_SSL('imap.gmail.com', 993)
M.login('user@gmail.com', 'password')
M.select('[Gmail]/All Mail')

subject = Header(email_model.subject, 'iso-8859-1').encode() #email_model.subject is in unicode, utf-8 encoded
typ, data = M.search('iso-8859-1', '(SUBJECT "%s")' % subject)
for num in data[0].split():
    typ, data = M.fetch(num, '(RFC822)')
    print 'Message %s\n%s\n' % (num, data[0][1])
M.close()
M.logout()

print 'Fin'

If you print out subject , you see that the result appears just the same as what I'm getting from the IMAP server on my prior, more-broad search.如果您打印出subject ,您会看到结果与我之前在更广泛的搜索中从 IMAP 服务器获得的结果相同。 Yet, it doesn't seem to make a match when doing this more specific search.然而,在进行这种更具体的搜索时,它似乎并不匹配。

For the search, I have tried everything I can think of:对于搜索,我已经尝试了所有我能想到的:

typ, data = M.search('iso-8859-1', '(HEADER subject "%s")' % subject)
typ, data = M.search('iso-8859-1', 'ALL (SUBJECT "%s")' % subject)

And others that I can't recall at the moment, all without any luck.还有一些我一时想不起来的,全都没有运气。

I can search (and match) for emails that have subjects that only use ASCII, but it doesn't work with any subject that has an encoding applied.我可以搜索(和匹配)主题仅使用 ASCII 的电子邮件,但它不适用于任何应用了编码的主题。 So...所以...

With IMAP, what is the proper way to search for an email using a subject that has an encoding applied?使用 IMAP,使用应用了编码的主题搜索电子邮件的正确方法是什么?

Thanks谢谢

When talking to IMAP servers, check with IMAP RFC . 与IMAP服务器通信时,请查看IMAP RFC

You must remove extra quotes, and you must not encode the strings. 您必须删除多余的引号,并且不得对字符串进行编码。 Also, charset specifies the charset of the search query, not the charset of the message header. 此外,charset指定搜索查询的字符集,而不是邮件头的字符集。 This should work (works for me): 这应该工作(适合我):

M.search("utf-8", "(SUBJECT %s)" % u"réception".encode("utf-8"))
# this also works:
M.search("iso8859-1", "(SUBJECT %s)" % u"réception".encode("iso8859-1"))

Edit: 编辑:

Apparently some servers (at least gmail as of August 2013) support utf-8 strings only when sent as literals. 显然,一些服务器(至少是2013年8月的gmail)仅在以文字形式发送时才支持utf-8字符串。 Python imaplib has a very limited literal arguments support, the best one can do is something like: Python imaplib有一个非常有限的文字参数支持,最好的可以做的是:

term = u"réception".encode("utf-8")
M.literal = term
M.search("utf-8", "SUBJECT")

This code work in 2021-2022.此代码在 2021-2022 年有效。 Try to count emails for others SUBJECT's.尝试为其他 SUBJECT 计算电子邮件。 And work with mails_list if you need email content.如果您需要电子邮件内容,请使用 mails_list。

import imaplib
import mailbox

user = 'your@email.com'
password = 'secure_password'
imap_url = 'imap.gmail.com'

M = imaplib.IMAP4_SSL(imap_url)
M.login(user, password)

M.select()

term = u"Test results".encode("utf-8")
M.literal = term
typ, data = M.search("utf-8", "SUBJECT")

mails_list = data[0].split()  # get all email's in list

print(len(mails_list))  # get mails quantity for search query

# close connection
M.close()
M.logout()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM