0

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'))
ZygD
  • 22,092
  • 39
  • 79
  • 102
Chaa MH
  • 73
  • 7

2 Answers2

0

The Outlook object model provides the Find/FindNext and Restrict methods of the Items class to get items that correspond to your conditions.

You may also find the How To: Retrieve Outlook calendar items using Find and FindNext methods article helpful where a similar search criteria is used.

Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45
0

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')"):
Dmitry Streblechenko
  • 62,942
  • 4
  • 53
  • 78