简体   繁体   English

python imaplib搜索多个条件

[英]python imaplib search with multiple criteria

I'm trying to use the search function and am running into an issue. 我正在尝试使用搜索功能并遇到问题。 I can download all attachments from a gmail account and sort them according to the file extension. 我可以从Gmail帐户下载所有附件,并根据文件扩展名对其进行排序。 I have all of that code working right except when I add extra criteria to the search. 我将所有代码都正常工作,除非我在搜索中添加额外条件。 Originally the search criteria was only for UNSEEN emails, which works and then flags the email as seen and moves it to the trash. 最初的搜索条件仅适用于UNSEEN电子邮件,它可以正常工作,然后标记电子邮件并将其移至垃圾箱。 I then decided to add to it. 然后我决定添加它。 Here is the example: original: 这是一个例子:原文:

resp, items = m.search (None, 'UNSEEN') 

new: 新:

resp, items = m.search (None, '(FROM "email" SUBJECT "some text")', 'UNSEEN')

It results with the emails moved to the trash, still unread and none of the attachments downloaded. 结果是电子邮件已移至垃圾箱,仍未读取且未下载任何附件。 Anyone have any idea what I may be doing wrong here? 任何人都知道我在这里做错了什么? Thanks. 谢谢。

To build on JithPS comment the right syntax is like this: 要构建JithPS注释,正确的语法是这样的:

result, data = mail.search(None,'(FROM "email" SUBJECT "the subject" UNSEEN)')

The clauses are passed with capital letters and the criteria within quotes. 条款以大写字母和引号内的标准传递。

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

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