Search code examples
pythondateemailoutlookemail-attachments

Save Outlook attachment from a specific date using Python


I would like to know how I could add the date to this code to save the attachment files n emails from Outlook: for example, I would like to save the files found between 20/04/2020 and 01/01/2020. Do you have any idea, please?

outputDir = r"C:\Users\CMhalla\Desktop\attachment"
i=0
for m in messages:
    if m.SenderEmailAddress == 'info@outlook.com':
       body_content=m.Body
       for attachment in m.Attachments:
           i=i+1
           attachment.SaveAsFile(os.path.join(outputDir,attachment.FileName + str(i)+'.xlsx'))

Solution

  • Change the line

    for m in messages:
    

    to (off the top of my head):

    for m in messages.Restrict("([ReceivedTime] <= '04/20/2020') AND ([ReceivedTime] >= '01/01/2020') AND (SenderEmailAddress = 'info@outlook.com')"):