I’m reading this document regarding connecting to my IMAP server and read the emails, but I have some issues.
Currently I have around 10K emails and it’s growing (I delete old emails weekly, but they’re still a lot), and in this part of the code, it fetches all emails:
res, msg = imap.fetch(str(i), "(RFC822)")
My emails have static subject format like this:
StaticText1 - SUBJECT - SentDateYYYY-MM-DD Hour:Minute
StaticText2 - SUBJECT - SentDateYYYY-MM-DD Hour:Minute
Let’s suppose a list like this for simplicity:
dates = ['2024-07-16', '2024-07-16', '2024-07-16', '2024-07-16', '2024-07-16', '2024-07-16', '2024-07-16', '2024-07-15', '2024-07-15', '2024-07-15', '2024-07-15', '2024-07-15', '2024-07-15', '2024-07-15', '2024-07-15', '2024-07-14', '2024-07-14', '2024-07-14', '2024-07-14', '2024-07-14', '2024-07-14', '2024-07-13', '2024-07-13', '2024-07-13', '2024-07-13', '2024-07-13', '2024-07-13', '2024-07-13']
So, let’s say reading each emails take at least 0.1 second, and that will be a huge time for 10K emails.
I read emails in my code from the latest to the oldest.
I’m going to reach this:
if datetime.datetime.now().strftime('%Y-%m-%d') in subject or (datetime.datetime.now() - datetime.timedelta(days=1)).strftime('%Y-%m-%d') in subject:
# then read email
print(email)
else:
# as soon as it reaches an email which has 2024-07-14, it breaks the loop and shouldn't try anything further
break
The main question is, is that even possible?
Update1
To clarify, I know this but it reads each email and if it doesn’t contain the date, breaks and goes to another email (which is not excepted):
for subject in emails:
if today in subject or yesterday in subject:
print(subject)
Yes, it’s possible. The if
statement is wrong, though, since the ... in subject
should be on both sides of the or
; otherwise it’ll always evaluate to True, since datetime.datetime.now().strftime('%Y-%m-%d')
is always true.
To make it simpler to read:
now = datetime.datetime.now()
today = now.strftime("%Y-%m-%d")
yesterday = (now - datetime.timedelta(days=1)).strftime("%Y-%m-%d")
for mail in mails:
subject = mail.subject
if not (today in subject or yesterday in subject):
break # all done!
# ... process mail
5
I would approach this task differently as
My emails have static subject format like this:
<code>StaticText1 - SUBJECT - SentDateYYYY-MM-DD Hour:MinuteStaticText2 - SUBJECT - SentDateYYYY-MM-DD Hour:Minute</code><code>StaticText1 - SUBJECT - SentDateYYYY-MM-DD Hour:Minute StaticText2 - SUBJECT - SentDateYYYY-MM-DD Hour:Minute </code>StaticText1 - SUBJECT - SentDateYYYY-MM-DD Hour:Minute StaticText2 - SUBJECT - SentDateYYYY-MM-DD Hour:Minute
You might easily extract that YYYY-MM-DD
by splitting and taking 2nd element from end e.g.
subject = "Static - Able - 2024-01-01 12:34"
datestr = subject.split()[-2]
print(datestr) # 2024-01-01
which you then can convert into datetime.datetime
object
import datetime
dt = datetime.datetime.strptime(datestr, "%Y-%m-%d")
and compute delta with today
delta = datetime.date.today() - dt.date()
and access .days attribute
print(delta.days) # 197
value of which is integer. This allows you to easily adjust number of days.