How to filter Microsoft Outlook emails using a specific date range?

I’m fairly new to Python and have been using it to make things at work easier for me. I work in I.T. and have to read through a lot of emails to look at the status of different things. One of these is our Yodeck players. I get emails every day about which Yodeck players are offline. They don’t come in 1 single email, but an email for each individual player that is offline. I get a lot of emails daily and I wanted to create a spreadsheet from all of the players that are offline so I have one central place that I can look at them.

I was able to actually retrieve all of the email subject lines from my inbox and place them in a CSV file. This felt pretty good. However, I only want to see the emails that came in the current day and the day before. This is where my hair started to turn gray. I tried all different types of solutions I found online and nothing seems to be working. I either get an error saying that < or > doesn’t work with datetime, or it just returns a blank list and prints [] in cell A1 of the CSV file.

Whenever I remove the date filtering part of the code, the email_date list prints fine with all of the dates populated. So for some reason my filtering is returning an empty list and empty spreadsheet.

Here is what I have (please bare with me):

# Importing modules/libraries
import win32com.client
import os
from datetime import date
from datetime import datetime
from datetime import timedelta
import csv
import pandas as pd

# Variables
outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
inbox = outlook.GetDefaultFolder(6)
messages = inbox.Items

#Empty Lists
email_subject = []
email_date = []
email_content = []

#find emails

for message in messages:
    start_date = message.senton.date() - timedelta(days=1)
    start_date = start_date.strftime("%Y, %#m, %#d")
    end_date = message.senton.date()
    end_date = end_date.strftime("%Y, %#m, %#d")
    if message.SenderEmailAddress == '[email protected]' and message.Subject.startswith('[EXTERNAL] [Yodeck] Your') and message.SentOn.date() == start_date and message.SentOn.date() == end_date:
        email_subject.append(message.Subject)
        email_date.append(message.senton.date()) 
        email_content.append(message.body)       

with open("yodeck.csv", "w") as new_file:
    new_file.write(str(email_subject))

file = open('C:\Users\jotaylor\Desktop\KAG_Projects\yodeck.csv', 'w', newline="")


with file:
    write = csv.writer(file)
    write.writerow([email_subject])
    for x in email_subject:
        write.writerow([x])


file.close()

I was able to convert my datetime variable to the same datetime structure as the Outlook emails. I’m just really lost on why everything returns blank once I apply the date range filtering in the if statement. I know to some it may seem pointless to try and accomplish this, but this is just something I’m doing for me and I’m using it as a way to practice Python on a real world scenario. Any help on this would be appreciated. Thanks in advance.

2

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