Wednesday, September 22, 2021

Find emails between two dates in outlook using python

 import sys, win32com.client as client, datetime
import datetime,time
# Connect with MS Outlook - must be open.
outlook=client.Dispatch("Outlook.Application")

d = (datetime.date.today() - datetime.timedelta (days=7)).strftime("%d-%m-%y")
print(d)
olFolder = outlook.GetNamespace("MAPI").Folders("allsourav@hotmail.com").Folders("Sent Items")
itmemail = outlook.Createitem(0)
messages = olFolder.Items

#to find mail in my sent items folder having a date which is 7 days ago
##
##for msg in messages:
##    if(msg.SentOn.strftime("%d-%m-%y")==d):
##        print (msg.Subject,msg.SentOn.strftime("%d-%m-%y"))

    
#to find a mail in between a time period
#print(datetime.datetime.now().strftime("%d/%m/%Y"))
#get current date in day/month/year format
xDate=datetime.datetime.now().strftime("%d/%m/%Y")
print(xDate)
#get current date in day/month/year format
xDate=datetime.date.today()
print(xDate)
#get two days back from today
days = datetime.timedelta(2)


xDate = xDate - days
print(xDate)

print(datetime.date.today()-datetime.timedelta(6))
print(datetime.date.today()-datetime.timedelta(10))

for msg in messages:
    if (msg.SentOn.date()<(datetime.date.today()-datetime.timedelta(6))) and (msg.SentOn.date()>(datetime.date.today()-datetime.timedelta(10))) :
        print (msg.Subject,msg.SentOn.strftime("%d-%m-%y"))



No comments:

Post a Comment