# AI Mail Summarizer

Lightweight IMAP email summarizer that cleans noisy newsletters/marketing footers, asks an Ollama model for concise digests (JSON or bullet text), and serves them via Streamlit. Ships with SQLite persistence, user login, per-user IMAP settings, and an hourly cron job.

## Features
- IMAP fetch with MIME decoding and HTML/boilerplate stripping.
- Per-user settings (IMAP host/user/pass, days back, Ollama host/model, output mode, custom prompts) stored in SQLite.
- User login page; defaults to `ADMIN_USER`/`ADMIN_PASSWORD` (env, defaults `admin`/`admin`). UI creds are separate from IMAP creds.
- Summaries stored per user in SQLite; already-summarized messages are skipped using Message-ID/UID.
- Streamlit UI: manual refresh (per user), settings page with **Custom Prompts**, structured rendering for JSON summaries, **From** address display, log tail viewer.
- Flexible Prompting: Customize both the System Prompt and User Prompt (template) regardless of provider.
- Hourly cron run in the container; optional run on container start (`RUN_ON_START`).

## Quick start (Docker)
1) Optionally seed defaults from config template (used to prefill admin settings on first run):
   ```bash
   cp config.json.default config.json
   # edit mail_host/username/password/ollama_host/ollama_model/output_mode
   ```
2) Build the image (already built as `email-summarizer:1.0.1` in this workspace):
   ```bash
   sudo docker build -t email-summarizer:1.0.1 .
   ```
3) Run:
   ```bash
   sudo docker run -d --name email-summarizer \
     --restart unless-stopped \
     -p 8501:8501 \
     -e RUN_ON_START=1 \
     -e ADMIN_USER=admin \
     -e ADMIN_PASSWORD=admin \
     -e OUTPUT_MODE=text \
     -v "$(pwd)/config.json:/app/config.json:ro" \
     -v "$(pwd)/data:/app/data" \
     email-summarizer:1.0.1
   ```
4) Open the UI at http://localhost:8501. Sign in (default admin creds above), set your IMAP/Ollama settings on the Settings tab, then use “Refresh now” to summarize. View tail logs under “View latest log”.

## Configuration
`config.json` (optional) is only used to prefill the first admin’s settings if none exist yet:
- `mail_host`, `username`, `password`: IMAP credentials.
- `days`: how many days back to fetch.
- `ollama_host`: URL to the Ollama server (e.g., `http://localhost:11434`).
- `ollama_model`: model name (e.g., `gemma3:27b-it-qat`).
- `output_mode`: `json` or `text`.
- `system_prompt`: (Optional) Override the default system instructions.
- `user_prompt`: (Optional) Template for the user message (Text mode). Supports `{subject}` and `{body}` placeholders.

Environment variables:
- `ADMIN_USER`, `ADMIN_PASSWORD`: UI login for the default admin (created if missing).
- `RUN_ON_START` (default 1): run summarizer once at container start.
- `OUTPUT_MODE`: fallback output mode if not set in user settings.
- `DB_PATH`: override SQLite location (default `data/app.db`).

## App scripts
- `summarize_emails.py`: fetches emails per user from SQLite settings, cleans bodies, summarizes via Ollama, and stores summaries per user in SQLite.
- `streamlit_app.py`: login + settings UI, per-user manual refresh, structured summary rendering, log viewer.

## Cron
- `docker-cron`: runs `summarize_emails.py` hourly.
- `docker-entrypoint.sh`: installs cron, optional initial run, then starts Streamlit.

## Development
Prereqs: Python 3.11+, pip.
```bash
pip install -r requirements.txt
python summarize_emails.py         # processes all users with configured IMAP settings
streamlit run streamlit_app.py
```

## Notes
- Data, SQLite DB, and logs live in `data/`; mount this directory when running Docker to persist outputs.
- The UI attempts to parse JSON summaries; otherwise it shows raw text bullets.
