imap 搜索来自白名单上的用户的消息?

发布于 2024-12-05 19:26:43 字数 929 浏览 0 评论 0原文

是否可以对来自白名单上的用户的消息运行 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

撩发小公举 2024-12-12 19:26:43

通过一些本地测试,我发现搜索中的 OR 是成对工作的。因此,当您仅搜索两个地址时,一切都会按预期进行。不过,当您需要第三个时,您需要执行以下操作:

'(OR (FROM "[email protected]") (OR (FROM "[email protected]") (FROM "[email protected]")))'

我发现间距很重要。如果我使用这个类似的字符串(最后一个结束括号之间有空格),它会抛出一个错误:

'(OR (FROM "[email protected]") (OR (FROM "[email protected]") (FROM "[email protected]") ) )'

这让我想起了我的 LISP 日子......无论如何,在列表上使用以下逻辑应该可以解决问题:

def buildOr(list):
    "Builds a tree structure like (OR (FROM 'term') (OR (FROM 'term') (FROM 'term')))"
    if len(list) < 2:
        raise RuntimeError('buildOr requires a list of at least 2')
    if len(list) == 2:
        return '(OR (FROM "{0}") (FROM "{1}"))'.format(list[0], list[1]) 
    else:
        return '(OR (FROM "{0}") {1})'.format(list[0], buildOr(list[1:]))

然后你可以组合它使用您的白名单进行搜索,如下所示:

m.search(None, buildOr(whitelist))

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:

'(OR (FROM "[email protected]") (OR (FROM "[email protected]") (FROM "[email protected]")))'

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:

'(OR (FROM "[email protected]") (OR (FROM "[email protected]") (FROM "[email protected]") ) )'

This reminds me of my LISP days... anyway, using the following logic on a list should do the trick:

def buildOr(list):
    "Builds a tree structure like (OR (FROM 'term') (OR (FROM 'term') (FROM 'term')))"
    if len(list) < 2:
        raise RuntimeError('buildOr requires a list of at least 2')
    if len(list) == 2:
        return '(OR (FROM "{0}") (FROM "{1}"))'.format(list[0], list[1]) 
    else:
        return '(OR (FROM "{0}") {1})'.format(list[0], buildOr(list[1:]))

Then you can combine that with your whitelist to do your search as follows:

m.search(None, buildOr(whitelist))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文