简体   繁体   English

使用发件人域的Python imaplib搜索

[英]Python imaplib Search using senders domain

I am trying to search using the IMAP search function to only return users that have sent emails from a specific domain. 我正在尝试使用IMAP搜索功能进行搜索,以仅返回从特定域发送电子邮件的用户。 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. 例如,假设我只想获取来自@ 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 eg 当我通过用户名称的一部分搜索用户时

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

it works even if the part is nothing like their username egjsmith@stackoverflow.com. 即使该部分与其用户名egjsmith@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. 因为我只想从这些特定域中获取要自动处理的内容,而其余的都保留为未读状态,所以我希望可以做些其他的事情。

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. 将获取来自“ stackoverflow.com”域中电子邮件地址的所有未读电子邮件。

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

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