imap 搜索来自白名单上的用户的消息?
是否可以对来自白名单上的用户的消息运行 imap 搜索?我已经找到了适用于两个名称的几种变体,但我不知道如何概括它。已经尝试阅读 RFC3501 和谷歌搜索示例。我正在使用 python 的 imaplib 和 gmail,但我相信这并不重要,因为我的问题是弄清楚搜索字符串的语法。
m = imap(...)
m.search(None, '(OR (FROM "[email protected]") (FROM "[email protected]"))') # works
whitelist = ['[email protected]', '[email protected]']
searchstring = '(OR ' + ' '.join(['(FROM "' + x + '")' for x in whitelist]) + ')'
m.search(None, searchstring) # works, but doesn't generalize.
Is it possible to run an imap search for messages from users on a whitelist? I have figured out several variations that work with two names, but I can't figure out how to generalize it. Already tried reading RFC3501 and googling for examples. I am using python's imaplib and gmail, but I believe that doesn't matter since my problem is figuring out the syntax of the search string.
m = imap(...)
m.search(None, '(OR (FROM "[email protected]") (FROM "[email protected]"))') # works
whitelist = ['[email protected]', '[email protected]']
searchstring = '(OR ' + ' '.join(['(FROM "' + x + '")' for x in whitelist]) + ')'
m.search(None, searchstring) # works, but doesn't generalize.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
通过一些本地测试,我发现搜索中的 OR 是成对工作的。因此,当您仅搜索两个地址时,一切都会按预期进行。不过,当您需要第三个时,您需要执行以下操作:
我发现间距很重要。如果我使用这个类似的字符串(最后一个结束括号之间有空格),它会抛出一个错误:
这让我想起了我的 LISP 日子......无论如何,在列表上使用以下逻辑应该可以解决问题:
然后你可以组合它使用您的白名单进行搜索,如下所示:
Through some local testing I found that the OR in the search works in pairs. So when you search on just two addresses everything works as expected. The minute you need a third though, you need to do the following:
I found that the spacing was important. If I used this similar string (with spaces between the final closing parens) it would throw an error:
This reminds me of my LISP days... anyway, using the following logic on a list should do the trick:
Then you can combine that with your whitelist to do your search as follows: