This project automates the process of fetching, categorizing, and organizing Outlook emails using the Microsoft Graph API. It supports handling attachments, managing mail folders, and applying categories — ideal for teams dealing with alert-based or operational email workflows.
| File | Purpose |
|---|---|
ms_graph.py |
Handles authentication and connection to Microsoft Graph API. |
outlook.py |
Contains reusable functions to interact with Outlook API. |
pre_alert.py |
The main automation script that processes and sorts emails. |
| Other files | Practice or test files (can be ignored). |
The pre_alert.py script is the core automation engine that performs the entire workflow — from token generation to downloading attachments and moving emails into specific folders.
-
Token Initialization
- Uses
ms_graph.pyto authenticate and retrieve an access token from Microsoft Graph API.
- Uses
-
Check Last Processed Time
- Checks for a
last_outlook_check_time.txtfile:- If found: reads the time and adds 1 second to avoid overlap.
- If not found: defaults to 2 days ago from the current time.
- This ensures only new emails are fetched.
- Checks for a
-
Fetch and Filter Emails
- Retrieves emails from the inbox after the defined time.
- Filters only those matching a predefined Pre-Alert criteria.
-
Prepare Mail Folders
- Ensures the following folders exist in Outlook:
PreAlertNo AttachmentQuery
- If missing, the script creates them automatically.
- Ensures the following folders exist in Outlook:
-
Process Emails
For each filtered email:
- 📩 From specific sender (
[email protected]):- Add Yellow category.
- Move to Query folder.
- 📎 With attachment:
- Download attachments to:
./downloaded/<subject>_<received_time>/ - Add Orange category.
- Move to PreAlert folder.
- Download attachments to:
- ❗ Without attachment (but marked as Pre-Alert):
- Add Orange and Yellow categories.
- Move to No Attachment folder.
- 📩 From specific sender (
-
Update Last Checked Time
- Writes the latest processed email's timestamp to
last_outlook_check_time.txt.
- Writes the latest processed email's timestamp to
| Folder Name | When Used | Assigned Categories |
|---|---|---|
PreAlert |
Email has valid attachments | 🟠 Orange |
No Attachment |
Email is Pre-Alert but missing attachments | 🟠 Orange, 🟡 Yellow |
Query |
Needs human intervention or specific user | 🟡 Yellow |
- ✅ Automatically fetches new emails using time-based filtering
- ✅ Detects and downloads attachments to organized local folders
- ✅ Assigns Outlook categories for easy filtering
- ✅ Moves emails into relevant Outlook folders
- ✅ Auto-creates folders if they don’t exist
- ✅ Fully integrated with Microsoft Graph API
- Python 3.7+
- Microsoft 365 account with API access
- Microsoft Graph API permissions (Mail.ReadWrite, Mail.Send, etc.)
- Clone the repository
git clone https://github.com/ShivPatel031/Outlook_Automation.git cd Outlook_Automation
🧠 Future Improvements
✅ Add multithreading to speed up processing (planned)
📁 Add support for other folder types (optional)
📊 Dashboard/summary of processed emails