How do I download email attachments for the current day only using Python?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Shane88
    New Member
    • May 2021
    • 2

    How do I download email attachments for the current day only using Python?

    Good afternoon,

    I've written a python code to download my email attachments. However, it currently downloads all the emails in the folder. I receive new email attachments every day, thus I just want to download the email attachments for the current day.
    Current code below does work, but as mentioned, downloads all the emails in the folder.
    Thanks for your help guys..

    Code:
    import win32com.client
    import datetime
    import os
    import email
    
    
    outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
    inbox = outlook.GetDefaultFolder(6).Folders.Item("LGW Performance")
    message = inbox.items
    
    items = inbox.items
    for item in items:
        for attachment in item.Attachments:
            print(attachment.FileName)
    Last edited by Banfa; May 18 '21, 08:23 AM. Reason: Added code tags
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    I don't really know the answer but here are some thoughts around the subject
    1. Do you really want to just download all attachments from your inbox; given the prevalence of spam some of those attachments must be malicious files that you do not want on your computer.
    2. The attachment is not a separate thing but something embedded in the email so to get the attachment you more or less have to get the email
    3. In order to know if an email does or doesn't have an attachment at the very least you need the email headers. The basic form of an email is a data block consisting of [Headers][Message]([Attachments]).
    4. It seems likely that this line items = inbox.items is the issue (and line 9 that is the same). You get a collection of all the items in the inbox; I assume that it just gets the entirety of every email. The only way you could improve this is either by changing this line to something that just gets the headers or by finding some function that allows you to interrogate the makeup of an email without downloading it. Sorry I don't have time to become familiar with this library (documentation here) so you may have to do some research yourself.

    Comment

    • SioSio
      Contributor
      • Dec 2019
      • 272

      #3
      If you want to target only the emails received today, you will compare today's date with the date and time the email was received.
      Code:
      import win32com.client
      import datetime
      import os
      import email
      
      Today = datetime.date.today()
      today = Today.strftime('%Y%m%d')
      
      outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
      inbox = outlook.GetDefaultFolder(6).Folders.Item("LGW Performance")
      message = inbox.items
      
      items = inbox.items
      for item in items:
      	RT = item.ReceivedTime
      	Msgdate = datetime.datetime(RT.year ,RT.month, RT.day, RT.hour, RT.minute, RT.second)
      	msgdate = Msgdate.strftime('%Y%m%d')
      	if today == msgdate:
      		for attachment in item.Attachments:
      			print(attachment.FileName)

      Comment

      Working...