0

Right now I have some code that searches an inbox for emails with attachments between two dates specified by the user, and saves attachments to a folder. There is also a dictionary to only look at certain senders. The idea is to run the script and grab expected attachments without having to search through the inbox for them. The problem is that even if I specify a range of one day, it will search the entire inbox. It doesn't get attachments outside of the range, so the program does what I need but searching the whole inbox takes longer than it needs to.

I have a feeling the issue is that I start with 'for msg in reversed(itcontents):' - itcontents is the name i've given the inbox, which I think is telling python to look at the whole inbox, and nothing telling it to stop searching after passing the date determined by 'searchRange'.

####Code to determine search range by date
check = 0
todayDate = datetime.date.today()
startDate = input("Enter a date dd-mm-yyyy: ")
if startDate == "":
        startDate = input("Enter a date dd-mm-yyyy: ")
startDate = datetime.datetime.strptime(startDate, "%d-%m-%Y") #class and module are called datetime hence datetime.datetime.strptime
searchRange = int(input("Enter how many before that date to search: "))
searchDate = startDate.date() - datetime.timedelta(days = searchRange)
###use msg.SentOn.date() >= searchDate to search emails on or after the date determined by searchRange 


print("Your search starts from", startDate, "and ends on", searchDate)

#####Code to check emails.
for msg in reversed(itcontents): #reversed() will go from most recent to oldest email

        if msg.Class == 43: #avoid attribute error for non mail-item objects
                try:
                        if (str(msg.SenderEmailAddress) or str(msg.SentOnBehalfOfName)) in senderDict and (msg.SentOn.date() >= searchDate and msg.SentOn.date() <= startDate.date()):
                                check += 1
                                print(check, "messages from", msg.SenderEmailAddress) #keep count of messages checked
                                for x in msg.Attachments:
                                        if str(".pdf").casefold() in str(x): #casfold() for upper or lower case combinations
                                                #x refers to the attachment
                                                x.SaveAsFile(r"C:\Users\...\Desktop\Invoices from Outlook\\" + str(msg.SenderEmailAddress) + x.FileName)
                                                print("Saved attachment", x, "from", str(msg.Sender()), "on", str(msg.SentOn.date()))


                except UnicodeEncodeError: #unsupported characters
                        print("Subject line could not be parsed.")
                        #continue
                except AttributeError:
                        print("Attribute error for item on", msg.SentOn.date())
                        #continue

input("Press any key to exit.")

Output looks like

Enter a date dd-mm-yyyy: 10-09-2019
Enter how many before that date to search: 7
Your search starts from 2019-09-10 00:00:00 and ends on 2019-09-03
1 messages from senderaddress.com
Saved attachment Invoice INV-001142.pdf from senderaddress.com on 2019-09-09
2 messages from senderaddress.com
3 messages from senderaddress.com
Saved attachment invoice287163401.pdf from senderaddress.com on 2019-09-04
4 messages from senderaddress.com
Saved attachment Invoice 450013144.pdf from senderaddress.com on 2019-09-03
5 messages from senderaddress.com
Press any key to exit.

'Press any key to exit.' will come up after some time, once it has gone through the whole inbox.

0 Answers0