Below is the code to download attachments from a sender with a specific subject. I need to deploy it locally on my computer so that this code keeps monitoring outlook and as soon as the email arrives, it should download the attachment. I have earlier deployed ml models with flask. I need help to deploy this locally. Currently running the .py script in batch mode
import imaplib
import base64
import os
import email
import imap_tools
from imap_tools import MailBox
import win32com.client
import os
from datetime import datetime, timedelta
outlook = win32com.client.Dispatch('outlook.application')
mapi = outlook.GetNamespace("MAPI")
messages = inbox.Items
received_dt = datetime.now() - timedelta(days=7)
received_dt = received_dt.strftime('%m/%d/%Y %H:%M %p')
messages = messages.Restrict("[ReceivedTime] >= '" + received_dt + "'")
messages = messages.Restrict("[SenderEmailAddress] = 'XXXX@gmail.com'")
messages = messages.Restrict("[Subject] = '[EXTERNAL] Fwd: Expense Report'")
outputDir = r"\\BLRESGANALXXXX\XXXXX\attachment_download"
try:
for message in list(messages):
try:
s = message.sender
for attachment in message.Attachments:
attachment.SaveASFile(os.path.join(outputDir,attachment.FileName))
print(f"attachment {attachment.FileName} from {s} saved")
except Exception as e:
print("error when saving the attachment:" + str(e))
except Exception as e:
print("error when processing emails messages:" + str(e))