How to break the loop only if email subject does not contain today or yesterday date

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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>res, msg = imap.fetch(str(i), "(RFC822)")
</code>
<code>res, msg = imap.fetch(str(i), "(RFC822)") </code>
res, msg = imap.fetch(str(i), "(RFC822)")

My emails have static subject format like this:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>StaticText1 - SUBJECT - SentDateYYYY-MM-DD Hour:Minute
StaticText2 - 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

Let’s suppose a list like this for simplicity:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>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']
</code>
<code>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'] </code>
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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>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
</code>
<code>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 </code>
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):

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>for subject in emails:
if today in subject or yesterday in subject:
print(subject)
</code>
<code>for subject in emails: if today in subject or yesterday in subject: print(subject) </code>
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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>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
</code>
<code>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 </code>
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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>StaticText1 - SUBJECT - SentDateYYYY-MM-DD Hour:Minute
StaticText2 - 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.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>subject = "Static - Able - 2024-01-01 12:34"
datestr = subject.split()[-2]
print(datestr) # 2024-01-01
</code>
<code>subject = "Static - Able - 2024-01-01 12:34" datestr = subject.split()[-2] print(datestr) # 2024-01-01 </code>
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

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>import datetime
dt = datetime.datetime.strptime(datestr, "%Y-%m-%d")
</code>
<code>import datetime dt = datetime.datetime.strptime(datestr, "%Y-%m-%d") </code>
import datetime
dt = datetime.datetime.strptime(datestr, "%Y-%m-%d")

and compute delta with today

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>delta = datetime.date.today() - dt.date()
</code>
<code>delta = datetime.date.today() - dt.date() </code>
delta = datetime.date.today() - dt.date()

and access .days attribute

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>print(delta.days) # 197
</code>
<code>print(delta.days) # 197 </code>
print(delta.days)  # 197

value of which is integer. This allows you to easily adjust number of days.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật