Appending Outlook multiple attachment file names into a list in Python
I'm currently working on a script that reads unread emails in a folder and appends the file names of the attachments into a list for further data manipulation. The script works if there is only one attachment per email, but I am having trouble getting it to recognize when there are multiple attachments in an email (2+).
Here is the code I am working with so far:
outlook = win32com.client.Dispatch("Outlook.Application")
mapi = outlook.GetNamespace("MAPI")
inbox = mapi.Folders['user@outlook.com'].Folders['Inbox'].Folders['Folder1']
message = inbox.Items
contents = [] attachment_name = []
for mail in message:
if mail.UnRead == True and mail.Attachments.Count > 0: # Grab all emails that are unread AND have an attachment
attachments = mail.Attachments
m_attach = len([i for i in attachments])
contents.append(mail.body)
contents.append(mail.Subject)
for i in range(1, m_attach):
attachment = attachments.Item(i)
attachment_name.append(attachment.FileName)
print(attachment_name)
Comments
Post a Comment