【问题标题】:Python exchangelib create alert if no email is found the past 24 hours如果在过去 24 小时内未找到电子邮件,Python exchangelib 创建警报
【发布时间】:2020-09-15 15:55:05
【问题描述】:

如果在过去 24 小时内未找到电子邮件,我该如何发出警报/异常?

如果找到电子邮件,这是打印输出。它打印 email-address、subject 和 count。一行

defaultdict(, {('info@something.com', '备份状态报告(错误): Tesla (D) (Monday, 14/09/2020)'): 1})

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

defaultdict(, {})

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)

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

【问题讨论】:

    标签: python outlook exchangelib


    【解决方案1】:

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

    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!')
    

    【讨论】:

    • 我根据您的答案编辑了我的代码,但它仍然打印出没有print('no email found')的空字典
    • 你不需要在循环中运行它。我更改了答案以获得完整的代码。
    猜你喜欢
    • 2019-08-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-02
    • 1970-01-01
    • 1970-01-01
    • 2022-07-12
    • 2021-01-29
    相关资源
    最近更新 更多