[英]Python exchangelib create alert if no email is found the past 24 hours
How can I make an alert/exception, if an email isn't found the past 24 hours?如果在过去 24 小时内未找到电子邮件,我如何发出警报/例外?
This is the printed output, if there is an email found.如果找到电子邮件,则这是打印输出。 It prints the email-address , subject and count .
它打印电子邮件地址、主题和计数。 In one line
在一行
defaultdict(<class 'int'>, {('info@something.com', 'Backup status report (Error): Tesla (D) (Monday, 14/09/2020)'): 1})
defaultdict(<class 'int'>, {('info@something.com', '备份状态报告 (错误): Tesla (D) (Monday, 14/09/2020)'): 1})
this is the printed output if nothing is found.如果未找到任何内容,则这是打印输出。
defaultdict(<class 'int'>, {})
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)
Edited above code to the below answer, but it still prints the empty dictionary将上面的代码编辑为下面的答案,但它仍然打印空字典
Filtering on sender is supposed to work, although I can't get it to work right now on my test account.过滤发件人应该可以工作,但我现在无法在我的测试帐户上使用它。 Here's how I would do an efficient query that just gives you the information you need:
以下是我如何进行高效查询,只为您提供所需信息:
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!')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.