Python exchangelib 在过去 24 小时内未找到电子邮件时创建警报

Python exchangelib create alert if no email is found the past 24 hours

如果在过去 24 小时内未找到电子邮件,我该如何创建 alert/exception?

如果找到电子邮件,这是打印输出。它打印 email-addresssubjectcount。一行

defaultdict(<class 'int'>, {('info@something.com', 'Backup status report (Error): Tesla (D) (Monday, 14/09/2020)'): 1})

如果没有找到,这是打印输出。

defaultdict(<class 'int'>, {})

from exchangelib import Credentials, Account, UTC_NOW
from collections import defaultdict
from datetime import timedelta



credentials = Credentials('something@something.dk', 'private')
a = Account('something@something.dk', credentials=credentials, autodiscover=True)
counts = defaultdict(int)
testfolder = a.inbox.parent / 'Test'
since = UTC_NOW() - timedelta(hours=24)

for item in testfolder.all()\
        .only('sender', 'subject')\
        .filter(datetime_received__gt=since)\
        .order_by('-datetime_received'):
    if item.sender.email_address == 'info@something.com':
        counts[item.sender.email_address, item.subject] += 1
    if not testfolder.filter(datetime_received__gt=since,
            sender='info@something.com').exists():
                print('no email found')
print(counts)

将上面的代码编辑为下面的答案,但它仍然打印空字典

过滤发件人应该可以,但我现在无法在我的测试帐户上使用它。以下是我将如何进行高效查询,只为您提供所需信息:

from datetime import timedelta
from exchangelib import Credentials, Account, UTC_NOW

credentials = Credentials('something@something.dk', 'private')
a = Account('something@something.dk', credentials=credentials, autodiscover=True)
since = UTC_NOW() - timedelta(hours=24)
testfolder = a.inbox.parent / 'Test'
if not testfolder.filter(
    datetime_received__gt=since, 
    sender='info@something.com',
).exists():
    print('No email found!')