- mail_client.py β Hybrid IMAP + AppleScript engine
- mail_action_engine.py β Mail-specific pipeline & actions
- app.py β Updated Flask endpoints for Mail IMAP
- requirements.txt β Updated dependencies with IMAP support
- MAIL_AI_SETUP.md β Comprehensive setup documentation
-
mail_client.pyβ IMAP client with AppleScript fallback - Hybrid connection strategy with failover
- Support for mark_read, flag, move, archive, trash operations
- Provider-agnostic IMAP implementation
-
mail_action_engine.pyβ Mail-specific pipeline - Email classification (same LLM logic)
- Approval queue system
- Draft reply generation
- Auto-actions (archive spam, unsubscribe, etc.)
- Full feature parity with Gmail manager
-
app.pyβ Updated Flask routes - Removed Gmail OAuth endpoints
- Added
/api/mail/test-connectionendpoint - Added
/api/mail/statusendpoint - Updated
/api/setupfor IMAP credentials - Updated
/api/pipeline/runfor mail_action_engine - Updated action execution endpoints
- Removed: Gmail OAuth flows (/api/auth/start, /oauth2callback, etc.)
- Removed: Calendar endpoints (optional, can be added later)
- Added: /api/mail/test-connection β Test IMAP credentials
- Added: /api/mail/status β Check connection status
- Updated: /api/status β Show mail_connected instead of authenticated
- Updated: /api/setup β IMAP fields instead of Gmail OAuth
- Updated: Pipeline imports to use mail_action_engine
- Updated: Action execution imports to use mail_action_engine
cd /Volumes/EXTERNAL/MYLLM
# Create mail-ai-manager folder if it doesn't exist
mkdir -p mail-ai-manager
# Copy the new/updated files:
cp mail_client.py mail-ai-manager/
cp mail_action_engine.py mail-ai-manager/
cp MAIL_AI_SETUP.md .# Backup original gmail-ai-manager
cp -r gmail-ai-manager gmail-ai-manager.backupcd mail-ai-manager
cp ../requirements.txt .
pip3 install -r requirements.txtpython3 -c "
from mail_client import create_mail_client
config = {
'imap_host': 'imap.gmail.com',
'imap_port': 993,
'username': 'your-email@gmail.com',
'password': 'your-app-password',
'account_name': 'Test'
}
client = create_mail_client(config)
if client.connect():
print('β
IMAP connection successful!')
mailboxes = client.imap_client.list_mailboxes()
print(f'Mailboxes: {mailboxes[:5]}')
client.disconnect()
else:
print('β Connection failed')
"- Update
index.htmlsetup wizard for IMAP- Remove Gmail OAuth section
- Add IMAP host/port/username/password fields
- Add test connection button
- Add mailbox selector
- Update
database.pyto add Mail config fields:mail_imap_hostmail_imap_portmail_imap_usernamemail_imap_passwordmail_account_name
- Test IMAP connection with Gmail (app-password)
- Test IMAP connection with iCloud
- Test IMAP connection with Outlook
- Test email fetching
- Test email classification
- Test archive operation
- Test trash/spam operation
- Test flag/read operation
- Test approval queue
- Test draft generation
- Test daily/weekly summaries
# Test connection
curl -X POST http://localhost:5051/api/mail/test-connection \
-H "Content-Type: application/json" \
-d '{"mail_imap_host":"imap.gmail.com","mail_imap_port":"993","mail_imap_username":"user@gmail.com","mail_imap_password":"app-password","mail_account_name":"Gmail"}'
# Check status
curl http://localhost:5051/api/mail/status
# Run pipeline
curl -X POST http://localhost:5051/api/pipeline/run \
-H "Content-Type: application/json" \
-d '{"max_emails": 10}'Mail AI Manager (Phase 3 Complete)
βββ mail_client.py
β βββ IMAPMailClient (primary)
β βββ AppleScriptMailClient (fallback)
β βββ HybridMailClient (orchestrator)
β
βββ mail_action_engine.py
β βββ run_pipeline() β Main orchestration
β βββ fetch_unread_mail() β IMAP fetch
β βββ execute_action() β Mail operations
β βββ Approval queue management
β
βββ app.py (updated)
β βββ /api/mail/test-connection β Test IMAP
β βββ /api/mail/status β Check connection
β βββ /api/setup β Save IMAP config
β βββ /api/pipeline/run β Start mail pipeline
β βββ [All other routes unchanged]
β
βββ database.py (ready for Mail config schema)
βββ llm_engine.py (unchanged - same classification)
βββ summarizer.py (unchanged)
βββ index.html (ready for UI update)
β
Hybrid IMAP + AppleScript β Reliable with native Mac fallback
β
Any IMAP Provider β Gmail, iCloud, Outlook, ProtonMail, etc.
β
Full Feature Parity β Classification, drafts, summaries, actions
β
No OAuth Complexity β Just IMAP credentials
β
100% Local β Zero cloud dependencies
β
Approval Queue β All actions require confirmation
β
Automatic Spam Detection β High-confidence filtering
β
Draft Generation β AI-powered reply suggestions
- Old
gmail-ai-managerdatabase remains unchanged - New system uses fresh
mail-ai.db - Export data from old system if needed before deleting
- IMAP passwords stored in SQLite (recommend full-disk encryption)
- Consider using app-specific passwords instead of main passwords
- Never commit credentials to version control
- Start with test IMAP connection endpoint
- Verify mailbox enumeration works
- Test email fetching on small batch (5 emails)
- Verify classification with known spam/work emails
- Test action execution in approval queue mode first
- Enable auto-actions after verification
# Test manually
python3 << 'EOF'
import imaplib
try:
imap = imaplib.IMAP4_SSL('imap.gmail.com', 993)
imap.login('user@gmail.com', 'app-password')
print("β
Success!")
except Exception as e:
print(f"β Error: {e}")
EOF# Verify Ollama running
curl http://localhost:11434/api/tags
# If not running:
ollama serve &- Gmail uses:
[Gmail]/Sent Mail,[Gmail]/All Mail, etc. - iCloud uses:
Sent,Drafts,Trash, etc. - The system auto-detects common folder names
Ready for Phase 4: Testing & Polish
This involves:
- Testing all endpoints with real IMAP accounts
- Updating UI/database schema if needed
- Documentation & deployment
- Performance optimization
Current state: All backend logic complete and ready for testing!
Built with: Flask Β· IMAP Β· AppleScript Β· Ollama Β· SQLite Β· 100% Local Β· Zero Cloud