Python imaplib Search using senders domain

我是研究僧i 提交于 2019-12-08 11:24:32

问题


I am trying to search using the IMAP search function to only return users that have sent emails from a specific domain. I am hoping this is actually possible.

For example let's say I want to only fetch emails from users sending from addresses coming from @stackoverflow.com.

We connect, login etcetera then search as below.

c = imaplib.IMAP4(imapserver)    
c.starttls() 
c.login(username, password)    
c.list()    
c.select('INBOX')
c.search(None, 'UNSEEN FROM "@stackoverflow.com"') 

However this returns no results. When I search for the user by part of their name e.g.

c.search(None, 'UNSEEN FROM "John"')

it works even if the part is nothing like their username e.g. j.smith@stackoverflow.com.

As I only want to fetch those that are to be automatically handled from these specific domains and leave the rest unread I am hoping there is something else I can do.


回答1:


Just needed to delve a little bit further into what was happening

c.search(None, 'UNSEEN HEADER FROM "stackoverflow.com"')

Will get all unread emails coming from an email address from the 'stackoverflow.com' domain.



来源:https://stackoverflow.com/questions/30389333/python-imaplib-search-using-senders-domain

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