A Python utility to automatically manage Gmail and Outlook emails with intelligent filtering, archiving, and calendar integration.
β¨ Automated Email Management:
- ποΈ Delete promotional emails older than 30 days
- π§ Delete job alerts older than 7 days
- π Archive job rejection emails
- β Highlight important emails addressed directly to you
- π Create calendar reminders for important emails
π Safe by Default:
- Runs in dry-run mode by default (no changes made)
- Moves emails to trash/deleted items instead of permanent deletion
- Detailed logging of all actions
pip install -r requirements.txt- Go to Google Cloud Console
- Create a new project (or select existing)
- Enable APIs:
- Gmail API
- Google Calendar API
- Configure OAuth consent screen:
- User Type: External
- Add your email as test user
- Create credentials:
- Create Credentials β OAuth 2.0 Client ID
- Application type: Desktop app
- Name: "Email Manager" (or any name)
- Download credentials JSON file
- Save as
gmail_credentials.jsonin the project directory
The app requests these permissions:
https://www.googleapis.com/auth/gmail.modify- Read and modify emailshttps://www.googleapis.com/auth/calendar- Create calendar events
- Go to Azure Portal
- Navigate to Azure Active Directory β App registrations
- Click "New registration":
- Name: "Email Manager" (or any name)
- Supported account types: "Accounts in any organizational directory and personal Microsoft accounts"
- Redirect URI: Leave blank for now
- After creation, note the Application (client) ID
- Configure platform:
- Go to Authentication
- Add platform β Mobile and desktop applications
- Add redirect URI:
http://localhost
- Configure API permissions:
- Add permission β Microsoft Graph β Delegated permissions
- Add:
Mail.ReadWrite,Calendars.ReadWrite - Grant admin consent (if required)
- Create
outlook_credentials.json:
{
"client_id": "your-application-client-id-here",
"authority": "https://login.microsoftonline.com/common"
}# Setup Gmail only
python email_manager.py --provider gmail --setup-only
# Setup Outlook only
python email_manager.py --provider outlook --setup-only
# Setup both
python email_manager.py --setup-onlyThis will open your browser for OAuth authentication. After granting permissions, credentials are saved locally for future use.
Recommended for first use - Shows what would be done without making changes:
# Process both Gmail and Outlook (dry run)
python email_manager.py
# Process only Gmail
python email_manager.py --provider gmail
# Process only Outlook
python email_manager.py --provider outlook# Explicit authentication step (recommended before live runs)
python email_manager.py --setup-only
# Process both providers (make actual changes)
python email_manager.py --live
# Process only Gmail
python email_manager.py --provider gmail --live
# Process only Outlook
python email_manager.py --provider outlook --live- First run opens browser for Google login
- Grants app permission to access Gmail and Calendar
- Token saved to
gmail_token.picklefor future use - Token auto-refreshes when expired
- First run opens browser for Microsoft login
- Grants app permission to access Mail and Calendar
- Token cached by MSAL library
- Token auto-refreshes when expired
- Gmail: Uses built-in
category:promotionslabel - Outlook: Keyword matching on sender addresses:
- Contains: noreply, newsletter, marketing, promotions, offers, unsubscribe
- Gmail: Searches for:
- Subject/body: "job alert", "career opportunity", "new jobs", "job notification"
- From: linkedin.com, indeed.com, glassdoor.com
- Outlook: Searches:
- Sender domains: linkedin.com, indeed.com, glassdoor.com, monster.com, careerbuilder.com
- Subject: "job alert", "career opportunity", "new jobs"
Archives emails containing phrases like:
- "regret to inform"
- "not moving forward"
- "chosen to pursue other candidates"
- "not been successful"
- "position has been filled"
Identifies emails that are:
- Received in last 7 days
- Directly addressed to you (in To: field, max 2 recipients)
- Not from no-reply addresses
- Marked as important OR from real people
For each important email, creates a calendar event:
- Title: "π§ Read: [Email Subject]"
- When: Tomorrow at 9:00 AM
- Duration: 30 minutes
- Reminder: 10 minutes before
- Description: Sender information and reminder text
email-manager/
βββ email_manager.py # Main entry point
βββ gmail_handler.py # Gmail-specific logic
βββ outlook_handler.py # Outlook-specific logic
βββ requirements.txt # Python dependencies
βββ gmail_credentials.json # Google OAuth credentials (you create)
βββ outlook_credentials.json # Azure AD credentials (you create)
βββ gmail_token.pickle # Saved Gmail token (auto-created)
βββ email_manager.log # Application logs
All actions are logged to:
- Console (stdout)
email_manager.logfile
Log levels:
- INFO: Normal operations, statistics
- ERROR: Problems encountered
- WARNING: Important notices
Edit the handlers to change retention periods:
# In gmail_handler.py or outlook_handler.py
# Change promotion deletion period (default: 30 days)
cutoff_date = datetime.now() - timedelta(days=30) # Change to 60, 90, etc.
# Change job alert period (default: 7 days)
cutoff_date = datetime.now() - timedelta(days=7) # Change to 14, 30, etc.Add to the keyword lists in handlers:
# More job alert keywords
job_keywords = [
'job alert',
'career opportunity',
'new jobs',
'your custom keyword here' # Add your own
]
# More rejection phrases
rejection_phrases = [
'regret to inform',
'not moving forward',
'your custom phrase here' # Add your own
]Modify _find_important_emails() and _is_directly_addressed() methods to change what's considered important.
Run daily at 8 AM:
# Edit crontab
crontab -e
# Add this line (adjust path to your script)
0 8 * * * cd /path/to/email-manager && /usr/bin/python3 email_manager.py --live >> /path/to/logs/cron.log 2>&1- Open Task Scheduler
- Create Basic Task
- Trigger: Daily at 8:00 AM
- Action: Start a program
- Program:
python - Arguments:
email_manager.py --live - Start in:
C:\path\to\email-manager
π Credential Storage:
- OAuth tokens stored locally in
gmail_token.pickleand MSAL cache - Never share these files or commit to version control
- Add to
.gitignore:gmail_token.pickle outlook_credentials.json gmail_credentials.json *.log
π Permissions:
- App only requests necessary permissions
- You can revoke access anytime:
- Gmail: Google Account Permissions
- Outlook: Microsoft Account Apps
- Download OAuth credentials from Google Cloud Console
- Save in the same directory as the scripts
- Create this file with your Azure AD client_id
- See Outlook setup section above
- Check that APIs are enabled in Google Cloud Console
- Verify redirect URIs are configured correctly
- Make sure you granted all requested permissions
- Run in dry-run mode first to see what would be processed
- Check log file for specific errors
- Verify email filters match your email patterns
- Both APIs have rate limits
- The app processes in batches to avoid hitting limits
- Add delays between requests if needed
Feel free to customize this utility for your needs! Some ideas:
- Add more email categories
- Integrate with other email providers
- Create a GUI interface
- Add email analytics/reporting
- Integrate with task management tools
MIT License - Feel free to use and modify as needed!
- Always test in dry-run mode first
- This app permanently deletes/archives emails in live mode
- Author not responsible for any data loss
- Use at your own risk
- Keep backups of important emails