IMAP search for email address in the from field

淺唱寂寞╮ 提交于 2019-12-02 09:18:03

问题


I am trying to search and filter my imap mails using python's imaplib. I am running into a quite strange problem with the search command, searching for email addresses in the FROM field. 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. The second search should've matched this mail right?

Is this not the way to search for email address in the From field? 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).


回答1:


use this as your filter:

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


来源:https://stackoverflow.com/questions/5877529/imap-search-for-email-address-in-the-from-field

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!