I am using exchangelib to connect to my exchange mailbox. Currently the code is scanning through all folders since the target email message can be in any of the folders for my task.
Is there a more efficient way for looping/scanning the folders here? (Looking for emails greater than a target start date)
all_folders = account.root.glob('**/*')
for folder in all_folders:
# Messages can only exist in 'IPF.Note' folder classes
if folder.folder_class == 'IPF.Note':
# For all emails that are newer than the target date
for email in folder.filter(datetime_received__gt=ews_bfr):
# Do something
Edit: Thanks for the help, Erik. This worked and made the run time decrease dramatically:
target_folders = (f for f in account.root.walk() if f.name in ['xxxxxx'])
folder_coll = FolderCollection(account=account, folders=target_folders)
for email in folder_coll.filter(datetime_received__gt=ews_bfr):
# Do something