简体   繁体   English

IMAP 在 from 字段中搜索 email 地址

[英]IMAP search for email address in the from field

I am trying to search and filter my imap mails using python's imaplib.我正在尝试使用 python 的 imaplib 搜索和过滤我的 imap 邮件。 I am running into a quite strange problem with the search command, searching for email addresses in the FROM field.我在搜索命令中遇到了一个非常奇怪的问题,在FROM字段中搜索 email 地址。 I have the following script,我有以下脚本,

print('search with name')
status, results = con.search(None, '(FROM "Shrikant Sharat")')
if status == 'OK':
    if results[0]:
        mid = results[0].split()[0]
        print('mail id', mid)
        print(con.fetch(mid, '(UID BODY[HEADER.FIELDS (FROM)])'))
    else:
        print('No results yielded')
else:
    print('unable to search', results)

print()
print('search with email')
status, results = con.search(None, '(FROM "shrikantsharat.k@gmail.com")')
if status == 'OK':
    if results[0]:
        mid = results[0].split()[0]
        print('mail id', mid)
        print(con.fetch(mid, '(UID BODY[HEADER.FIELDS (FROM)])'))
    else:
        print('No results yielded')
else:
    print('unable to search', results)

for which, I get the following result,为此,我得到以下结果,

search with name
mail id 2155
('OK', [('2155 (UID 5340 BODY[HEADER.FIELDS (FROM)] {54}', 'From: Shrikant Sharat <shrikantsharat.k@gmail.com>\r\n\r\n'), ' FLAGS (\\Seen))'])

search with email
No results yielded

Why does the second search fail?为什么第二次搜索失败? The email address is present in the From field as shown by the previous search. email 地址出现在From字段中,如之前的搜索所示。 The second search should've matched this mail right?第二次搜索应该匹配到这封邮件吧?

Is this not the way to search for email address in the From field?这不是在From字段中搜索email地址的方法吗? I am pulling my hair apart on this one.我在这个上把头发拉开。 Any ideas?有任何想法吗?

Edit If relevant, I am trying this with an IMAP server exposed by MS Exchange (2007, I think).编辑如果相关,我正在尝试使用 MS Exchange 公开的 IMAP 服务器(我认为是 2007 年)。

use this as your filter:将此用作您的过滤器:

(HEADER FROM "shrikantsharat.k@gmail.com")

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

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