I am trying to archive emails in INBOX using the win32com.client module. The loop stops after a certain number of times and I have tried it with different loops with the same results. I have even added exception handler to see if it fails anywhere but it exits gracefully.
The code is as follows:-
There could be 15 files in the Inbox folder, it will move 9 first when first executed. I have tried the Delete() method and it does the same thing. It will only delete 9 emails but does not go through the entire list. It is like there is a hidden counter somewhere and it will only do so many.
import datetime as date
from datetime import datetime
from datetime import timedelta
import pywintypes, re,sys
outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
inbox = outlook.GetDefaultFolder(6)
folder = outlook.Folders(1)
proc_folder = folder.Folders("Archive")
messages = inbox.Items
ESA_PRFX = 'IronPort Report: Content Filters'
WSA_PRFX_1 = 'IronPort Report: Monthly Anti-Malware'
WSA_PRFX_2 = 'IronPort Report: WAS Monthly Overview'
file_count = 0
for msg in messages:
if ((re.search(ESA_PRFX, msg.Subject) and msg.SenderEmailAddress == "firstname.lastname@example.org") or \
(re.search(WSA_PRFX_1, msg.Subject) and msg.SenderEmailAddress == "email@example.com") or \
(re.search(WSA_PRFX_2, msg.Subject) and msg.SenderEmailAddress == "firstname.lastname@example.org")):
for att in msg.Attachments:
att.SaveAsFile(DATA_DIR + att.FileName)
file_count = file_count + 1