0

I am a bit new to coding and I am trying to understand how to get Python to save MS Outlook attachments from a specific sender. I currently receive the same email from the same person each day regarding data that I need to save to a specific folder. Below are the requirements I am trying to meet:

  1. I want to open MS Outlook and search for specific sender
  2. I want to make sure that the email that I am opening from the specific sender is the most current date
  3. I want to save all attached files from this sender to a specific folder on my desktop

I have seen some posts on using win32com.client but have not had much luck getting it to work with MS Outlook. I will attach some code I have tried below. I appreciate any feedback!

import win32com.client
outlook=win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
inbox=outlook.GetDefaultFolder(6)
messages=inbox.Items
for message in messages:
    attachments = message.attachments
    for attachment in attachments:
        pass
0m3r
  • 12,286
  • 15
  • 35
  • 71
MHP
  • 21
  • 2
  • 1
    Stack Overflow is geared towards specific technical questions. As such, this seems overly broad. _have not had much luck getting it to work_ sounds like it might be more relevant. – AMC Dec 18 '19 at 15:07
  • Thanks for the feedback. The code that I have seen posted on other form has not seemed to work or do anything when I use it in Python. Below is the code I have tried. outlook=win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI") inbox=outlook.GetDefaultFolder(6) messages=inbox.Items for message in messages: attachments = message.attachments for attachment in attachments: pass – MHP Dec 18 '19 at 15:12
  • 1
    Can you include that in your post (full code)? – AMC Dec 18 '19 at 15:15
  • Of course this code doesn't work... it has a `pass` at the end, or are you deliberate conceiling something unnecessary to the question? – tst Dec 19 '19 at 07:45

2 Answers2

3

You almost got it, add filter to the sender email address

import win32com.client

Outlook = win32com.client.Dispatch("Outlook.Application")
olNs = Outlook.GetNamespace("MAPI")
Inbox = olNs.GetDefaultFolder(6)

Filter = "[SenderEmailAddress] = '0m3r@email.com'"

Items = Inbox.Items.Restrict(Filter)
Item = Items.GetFirst()

for attachment in Item.Attachments:
    print(attachment.FileName)
    attachment.SaveAsFile(r"C:\path\to\my\folder\Attachment.xlsx")

Python 3.8 on windows

0m3r
  • 12,286
  • 15
  • 35
  • 71
0
def saveAttachments(email:object):
        for attachedFile in email.Attachments: #iterate over the attachments
                try:
                        filename = attachedFile.FileName
                        attachedFile.SaveAsFile("C:\\EmailAttachmentDump\\"+filename) #Filepath must exist already
                except Exception as e:
                        print(e)

for mailItem in inbox.Items:
        #Here you just need to bould your own conditions
        if mailItem.Sender == "x" or mailItem.SenderName == "y":
               saveAttachments(mailItem)

The actual conditions you can change to your liking. I would recommend referring to the Object model for Outlook MailItem objects: https://learn.microsoft.com/en-gb/office/vba/api/outlook.mailitem Specifically its Properties

tst
  • 371
  • 1
  • 11