How can I make an alert/exception, if an email isn't found the past 24 hours?
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})
this is the printed output if nothing is found.
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