I am using imaplib to get the number of emails received in a mailbox on specific dates and it works great but I would like to take it further and use variables to select the date range (with SINCE and BEFORE) and print the number of daily emails received since a chosen starting date until today in a a loop.
However I am already getting a error message when replacing the BEFORE date with today's date variable:
from datetime import date,timedelta
today = date.today()
tdate = today.strftime("%d-%b-%Y")
obj = imaplib.IMAP4_SSL('imap.gmail.com', 993)
obj.login('xxx@gmail.com', 'password')
obj.select('Inbox')
('OK', ['50'])
gmail_count = obj.search(None,'(SINCE "28-Aug-2020" BEFORE "tdate")')
values = gmail_count[1][0]
replaced = values.decode("utf-8").replace(" ", ",")
replaced.count(",") + 1
error: SEARCH command error: BAD [b'Could not parse command']
Is there a way to avoid this?
If I understand it correctly you are trying to use variable
values to select the date range of SINCE & BEFORE. Since the 2nd parameter of obj.search()
accepts a string value just format the string prior to passing in.
For example
You can set a formatted variable like so and pass the variable value in there
rangeVals = (SINCE "{}" BEFORE "{}").format("<Start-Date>,<End-Date>")
And then pass the variable as the parameter, like so
gmail_count = obj.search(None, rangeVals)
If the solution is unsuccessful, consider updating your error traces to the question.