468,468 Members | 2,727 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,468 developers. It's quick & easy.

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..

Expand|Select|Wrap|Line Numbers
  1. import win32com.client
  2. import datetime
  3. import os
  4. import email
  5.  
  6.  
  7. outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
  8. inbox = outlook.GetDefaultFolder(6).Folders.Item("LGW Performance")
  9. message = inbox.items
  10.  
  11. items = inbox.items
  12. for item in items:
  13.     for attachment in item.Attachments:
  14.         print(attachment.FileName)
4 Weeks Ago #1
2 3315
Banfa
9,052 Expert Mod 8TB
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.
3 Weeks Ago #2
SioSio
246 128KB
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.
Expand|Select|Wrap|Line Numbers
  1. import win32com.client
  2. import datetime
  3. import os
  4. import email
  5.  
  6. Today = datetime.date.today()
  7. today = Today.strftime('%Y%m%d')
  8.  
  9. outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
  10. inbox = outlook.GetDefaultFolder(6).Folders.Item("LGW Performance")
  11. message = inbox.items
  12.  
  13. items = inbox.items
  14. for item in items:
  15.     RT = item.ReceivedTime
  16.     Msgdate = datetime.datetime(RT.year ,RT.month, RT.day, RT.hour, RT.minute, RT.second)
  17.     msgdate = Msgdate.strftime('%Y%m%d')
  18.     if today == msgdate:
  19.         for attachment in item.Attachments:
  20.             print(attachment.FileName)
3 Weeks Ago #3

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

3 posts views Thread by LutherRevisited | last post: by
15 posts views Thread by Thomas W | last post: by
29 posts views Thread by 63q2o4i02 | last post: by
2 posts views Thread by David Veeneman | last post: by
3 posts views Thread by olafmol | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.