# Email Alerts System - Flask Deployment Guide

## Overview
This Flask web application provides a user-friendly interface for the Email Alerts System with configurable settings for time frames, email monitoring, and alert levels.

## Features

### ✅ Configurable Settings
- **Email Address**: Set which email to monitor for responses
- **Time Frames**: Add unlimited custom time frames for alerts (e.g., 1-24 hours, 24-48 hours, 48+ hours)
- **Email Range**: Configure how many days back to check emails (1-365 days)
- **Agency Domains**: Set which email domains indicate agency responses

### ✅ Web Interface
- **Dashboard**: View system status and process emails
- **Settings**: Configure all system parameters
- **Real-time Updates**: See processing results and thread status

## Installation

### 1. Install Dependencies
```bash
pip install -r requirements.txt
```

### 2. Environment Setup
Copy the example environment file and configure your settings:
```bash
cp env.example .env
```

Edit `.env` with your credentials:
```env
ZOHO_EMAIL=your-email@domain.com
ZOHO_APP_PASSWORD=your-app-password
GROQ_API_KEY=your-groq-api-key
TWILIO_ACCOUNT_SID=your-twilio-sid
TWILIO_AUTH_TOKEN=your-twilio-token
TWILIO_PHONE_NUMBER=your-twilio-phone
SECRET_KEY=your-secret-key-here
```

### 3. Run the Application
```bash
python run.py
```

The web interface will be available at: http://localhost:5000

## Usage

### Dashboard
- **Test Connection**: Verify email connectivity
- **Process Emails**: Run the email processing pipeline
- **Refresh Threads**: Update the list of threads needing alerts

### Settings
1. **Email Configuration**:
   - Set the email address to monitor
   - Configure how many days back to check emails
   - Add agency domains (comma-separated)

2. **Time Frames**:
   - Add unlimited custom time frames
   - Set hours and alert levels for each frame
   - Remove unwanted time frames

3. **Save Configuration**: All settings are automatically saved

## Configuration Examples

### Time Frames
```
1-24 hours: 24 hours, Level 1 (Normal)
24-48 hours: 48 hours, Level 2 (Urgent)  
48+ hours: 72 hours, Level 3 (Critical)
Custom: 12 hours, Level 1 (Early warning)
```

### Email Range
- **7 days**: Standard monitoring
- **30 days**: Monthly monitoring
- **90 days**: Quarterly monitoring

### Agency Domains
```
projects@manaknightdigital.com
support@company.com
help@agency.com
```

## API Endpoints

- `GET /` - Dashboard
- `GET /settings` - Settings page
- `POST /update_settings` - Update configuration
- `POST /process_emails` - Process emails and send alerts
- `GET /get_threads` - Get threads needing alerts
- `GET /test_connection` - Test email connection

## Production Deployment

### Using Gunicorn
```bash
pip install gunicorn
gunicorn -w 4 -b 0.0.0.0:5000 app:app
```

### Using Docker
```dockerfile
FROM python:3.11-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY . .
EXPOSE 5000
CMD ["gunicorn", "-w", "4", "-b", "0.0.0.0:5000", "app:app"]
```

## Troubleshooting

### Common Issues
1. **Connection Failed**: Check Zoho IMAP settings and app password
2. **No Alerts Sent**: Verify Twilio credentials and phone numbers
3. **AI Analysis Errors**: Check Groq API key and quota

### Logs
Check the console output for detailed error messages and processing results.

## Security Notes
- Use strong SECRET_KEY for production
- Enable HTTPS in production
- Restrict access to authorized users
- Regularly rotate API keys and passwords 