使用 Exchangelib 过滤器过滤特定域的电子邮件

时间:2021-04-26 14:37:24

标签: python email

我正在使用 Exchangelib 库来处理电子邮件。我想在从 Exchange 服务器读取电子邮件时应用过滤器:

  1. 排除来自特定域的电子邮件,例如,如果电子邮件以 @gmail.com 结尾,则排除所有电子邮件。
  2. 排除私人电子邮件(要排除私人电子邮件,我们可以选择标识 - sensitivity=private)。

这是我正在使用的代码片段。 你能帮我们解决过滤器问题吗?

from exchangelib import Credentials, Account , DELEGATE , Q

credentials = Credentials('test@example.com', 'topsecret')
account = Account('john@example.com', credentials=credentials, autodiscover=True, access_type  = DELEGATE)

query = Q(sender_contains = '@gmail.com')
recent_mails = account.inbox.filter(~query).order_by('-datetime_received')[:100] 

for item in recent_mails:
    print(item.subject, item.sender, item.datetime_received)

我也尝试过排除选项:

recent_mails = account.inbox.exclude(query)

0 个答案:

没有答案
相关问题