473,473 Members | 1,873 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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

2 New Member
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)
May 15 '21 #1
2 6657
Banfa
9,065 Recognized Expert Moderator Expert
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.
May 18 '21 #2
SioSio
272 Contributor
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)
May 19 '21 #3

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

Similar topics

3
by: LutherRevisited | last post by:
Is there a way I can put a message together without having to download any attachments there may be at the same time. I'm not having any problems dealing with attachments, but the way I'm doing...
15
by: Thomas W | last post by:
How can I use python to find images that looks quite similar? Thought I'd scale the images down to 32x32 and convert it to use a standard palette of 256 colors then compare the result pixel for...
29
by: 63q2o4i02 | last post by:
Hi, I'm interested in using python to start writing a CAD program for electrical design. I just got done reading Steven Rubin's book, I've used "real" EDA tools, and I have an MSEE, so I know what...
2
by: David Veeneman | last post by:
My web app creates several temporary files, then attaches them to an email that it sends out. When the app attches the documents to the email, .NET locks the files. My problem is that they aren't...
3
by: olafmol | last post by:
Hello, i want to write a PHP4 script that reads POP3 or IMAP mail and can decode the possible attachments in the email. I've looked around for some classes to do this, and found a few, but most...
1
by: shil | last post by:
Is there any possible way to create an email attachments from an URL in windows vb.net application? I'm using frame work 2.0, System.Net.Mail component. If I attach a file from my file system,...
3
by: Blubaugh, David A. | last post by:
To All, I was wondering if it was possible to utilize python to share a memory resource between a linux and windows system?? It should be stated that both the Linux (CENTOS 5) and windows...
0
by: Blubaugh, David A. | last post by:
Diez, What you have said is extremely concerning. I am now using VMware. With Linux as the Master and windows as the guest operating system. I was wondering if you have ever had to develop...
3
by: dakshinamurti | last post by:
So I try to download email attachments or programs from the internet and the files are impossible to open, for they are automatically deleted. I have tried in different browsers, different computer...
1
by: varunmaggo | last post by:
I have outlook 2007 and i am building a plugin, that will edit the footer for all the email attachments,(for example - abc confidential). How can i write an outlook addin, that will on the fly, will...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.