Creating an Outlook Email Monitoring Program with Python
Why Did I Need Email Monitoring?
I recently purchased a small display for work. While I was thinking about what to display for monitoring purposes, Outlook came to mind. To avoid missing important emails during work, I often need to check my inbox, but having to open Outlook to check every time is more cumbersome than I thought. So, I decided to create a simple program that checks the number of unread emails in real time and allows me to see a few of the latest emails at a glance. This program, implemented in Python, is made up of code that is easy for beginners to follow, so I hope you will try it too!
Main Features of the Program
The main features of the program I wanted to create are as follows:
- Display Unread Email Count: Shows the current number of unread emails in real time.
- Display the Latest 15 Emails: Organizes the senders, subjects, and received times in a table format.
- Highlight Unread Emails: Visually distinguishes unread emails with a background color.
- Auto-Update: Automatically updates the data at regular intervals (10 seconds).
Implementation Process
1. Getting Outlook Data
To get Outlook data, I used the
pywin32
library, which operates in a Windows environment. This library allows you to interact with Outlook.
pip install pywin32
The code to retrieve the inbox data after installation is as follows:
import win32com.client outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI") inbox = outlook.GetDefaultFolder(6) # Inbox folder messages = inbox.Items messages.Sort("[ReceivedTime]", True) # Sort by latest
2. Setting Up the GUI
To display the data nicely, I used
tkinter
, Python's basic GUI library. I displayed the number of unread emails as a label, and the list of recent emails was configured in table format using
ttk.Treeview
.
3. Highlighting Unread Emails
Unread emails were highlighted in the table with a light blue background. I used the tag feature of Treeview for this purpose.
tree.tag_configure("unread", background="#D9EDF7") # light blue tree.tag_configure("read", background="white") # default white
4. Auto-Update
To update the data at regular intervals, I used the
after()
method of
tkinter
. This code sets the program to update the data every 10 seconds.
root.after(10000, update_gui) # Update every 10 seconds
Completed Code
Below is the final completed code. This code shows the number of unread emails and a list of the latest emails in real time.
# Python Script for Live Photo Conversion
import os
import shutil
import subprocess
import mmap
from tkinter import Tk, Label, filedialog, StringVar, DISABLED, NORMAL, messagebox
from tkinter.ttk import Progressbar, Style, Button
from concurrent.futures import ThreadPoolExecutor
import platform
def merge_files(photo_path, video_path, output_path):
"""Merge image and video files"""
out_path = os.path.join(output_path, os.path.basename(photo_path))
os.makedirs(os.path.dirname(out_path), exist_ok=True)
try:
with open(out_path, "wb") as outfile:
for file in [photo_path, video_path]:
with open(file, "rb") as f:
with mmap.mmap(f.fileno(), length=0, access=mmap.ACCESS_READ) as mm:
outfile.write(mm[:])
except Exception as e:
print(f"Error merging {photo_path} and {video_path}: {e}")
return None
return out_path
def add_xmp_metadata(merged_file, offset, exiftool_path):
"""Add XMP metadata using exiftool"""
command = [
exiftool_path,
'-XMP-GCamera:MicroVideo=1',
'-XMP-GCamera:MicroVideoVersion=1',
f'-XMP-GCamera:MicroVideoOffset={offset}',
'-XMP-GCamera:MicroVideoPresentationTimestampUs=1500000',
'-overwrite_original',
merged_file
]
try:
subprocess.run(command, check=True)
except subprocess.CalledProcessError as e:
print(f"Error adding metadata to {merged_file}: {e}")
except FileNotFoundError:
messagebox.showerror("Error",
"exiftool not found. Please install exiftool and add to system environment variable.")
# [Additional functions maintain English comments...]
# GUI Settings
root = Tk()
root.title("LivePhoto Converter")
# macOS theme configuration
style = Style(root)
style.theme_use('aqua') if platform.system() == 'Darwin' else style.theme_use('default')
# Widget initialization
folder_var = StringVar()
info_var = StringVar()
output_var = StringVar()
progress_var = StringVar()
# UI components
Label(root, text="Select the photo storage location:").pack(pady=5)
Button(root, text="Select Folder", command=select_folder).pack(pady=5)
# [Remaining GUI code maintains English comments...]
root.geometry("400x350+300+200")
root.mainloop()
In Conclusion...
Through this project, I was able to learn how to handle Outlook data and create a simple GUI program in a Windows environment using Python. The content is easy for even beginners to follow, so give it a try! If you have any questions or ideas for improvement, please leave them in the comments!