简体   繁体   English

如何使用 Python imaplib 过滤/搜索带有自定义标头字段的电子邮件?

[英]How to filter/search for emails with a custom header field using Python imaplib?

I want to filter emails for the presence of a specific custom header field, eg "X-my-header-field: my-header-value" .我想过滤电子邮件是否存在特定的自定义标头字段,例如"X-my-header-field: my-header-value" I am using Python's imaplib .我正在使用Python 的 imaplib I unsuccessfully tried the search method : rv, data = mailbox.search(None, "X-my-header-field", 'my-header-value') .我没有成功尝试搜索方法rv, data = mailbox.search(None, "X-my-header-field", 'my-header-value')

Does anybody have an idea or hint how to accomplish this?有没有人有想法或暗示如何做到这一点? The idea is to filter the emails before downloading it from the server.这个想法是在从服务器下载电子邮件之前过滤电子邮件。

mail.list()
mail.select("inbox") # connect to inbox.
result, data = mail.search(None, '(FROM "anjali sinha" SUBJECT "test")' )
ids = data[0] # data is a list.
id_list = ids.split() # ids is a space separated string

I found the answer with the hint of Vikas reply and readingRFC 9051 :我通过 Vikas 回复的提示和阅读RFC 9051找到了答案:

M.search(None, '(HEADER "X-my-header-field" "my-header-value")')

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

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