简体   繁体   English

Python IMAP搜索部​​分主题

[英]Python IMAP search for partial subject

I'm trying to fetch all emails whose subject starts with "New Order" but I can't seem to figure it out. 我正在尝试获取主题以“新订单”开头的所有电子邮件,但我似乎无法弄明白。 Currently I can search for an exact match with a setup like so... 目前我可以搜索与这样的设置完全匹配...

result, data = M.uid('search', None, '(HEADER Subject "Subject Here")')

However this won't retrieve any messages that aren't an exact match. 但是,这不会检索任何不完全匹配的消息。 How would I go about doing a partial match? 我将如何进行部分匹配?

If it matters I am talking to gmail's imap server(s). 如果重要的话我正在和gmail的imap服务器交谈。

Thanks 谢谢

According to the IMAP RFC SEARCH should do all of its matching as substring matches: 根据IMAP RFC SEARCH应该将其所有匹配作为子字符串匹配:

In all search keys that use strings, a message matches the key if the string is a substring of the field. 在所有使用字符串的搜索键中,如果字符串是字段的子字符串,则消息与键匹配。 The matching is case-insensitive. 匹配不区分大小写。

Therefore, a search 因此,搜索

M.uid('search', None, 'HEADER Subject "New Order"')

should match all messages where New Order occurs anywhere in the subject. 应匹配主题中任何位置发生New Order所有消息。 If it is not, you should notify Google that their server does not implement IMAP properly. 如果不是,您应该通知Google他们的服务器没有正确实施IMAP。 In the meantime you might try using the SUBJECT key as in 在此期间,您可以尝试使用SUBJECT

M.uid('search', None, 'SUBJECT "New Order"')

Also, according to Google's IMAP extension documentation you might be able to use the X-GM-RAW key and a gmail search string as in 此外,根据Google的IMAP扩展文档,您可以使用X-GM-RAW密钥和gmail搜索字符串,如

M.uid('search', None, r'X-GM-RAW "subject:\"New Order\""')

这对我有用:

mail.uid('search', None, r'(X-GM-RAW "subject:\"New Order\"")')

这对我有用:

result, data = m.search(None, 'X-GM-RAW', 'subject: New Order')

This worked for me: 这对我有用:

M.uid('search',None, '(SUBJECT "New Order")')

Format for search is (EmailAttribute "your searchstring") 搜索格式为(EmailAttribute "your searchstring")

Hope it works for you. 希望对你有效。

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

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