I am trying to download an attachment from outlook with a specific Subject line. It shows finished, but no attachment is getting downloaded. Below attached is my code, kindly help if I am missing something.
# import libraries
import win32com.client
import re
import datetime
import pathlib2 as pathlib
# set up connection to outlook
outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
inbox = outlook.GetDefaultFolder(6)
messages = inbox.Items
message = messages.GetFirst()
today_date = str(datetime.date.today())
while True:
try:
current_sender = str(message.Sender).lower()
current_subject = str(message.Subject).lower()
# find the email from a specific sender with a specific subject
# condition
if re.search('AllSalons was executed at',current_subject) != None:
#if re.search('AllSalons was executed at',current_subject) != None and re.search(sender_name,current_sender) != None:
print(current_subject) # verify the subject
print(current_sender) # verify the sender
attachments = message.Attachments
attachment = attachments.Item(1)
attachment_name = str(attachment).lower()
attachment.SaveASFile(pathlib.path + 'C:\\Users\\UserTest\\Desktop\\Folder\\Subject Line\\Nov' + attachment_name)
else:
pass
message = messages.GetNext()
except:
message = messages.GetNext()
break
print("Finished")