# Email Campaigner

Self-hosted email marketing app (React frontend, Node API, Postgres/Supabase stack).

## Repository

**https://github.com/silicon-overdrive/email_campaigner.git**

```bash
git clone https://github.com/silicon-overdrive/email_campaigner.git
cd email_campaigner
```

## Quick start

- Copy `.env.example` to `.env` (if present) and configure database, Supabase, and API URLs.
- **Database:** New Docker volumes apply SQL under `supabase/migrations/` on first Postgres init (including `003_profiles.sql`). If your Postgres volume already existed before `003_profiles.sql` was added, apply that file once with `psql` (e.g. host port `54350`) or recreate the volume.
- **Postman:** Import `postman/Email_Campaigner_API.postman_collection.json` — set collection variable `accessToken` to your Supabase JWT after signing in at the app URL.
- **Docker:** `docker compose up -d` — frontend [http://localhost:8098](http://localhost:8098), API [http://localhost:3002](http://localhost:3002). Postgres is exposed on host port **54350** (for GUI clients); inside Compose, services use `ec-db:5432`.
- **Local frontend dev:** `cd frontend && npm install && npm run dev` (default Vite port in `vite.config.ts`).
- **Password reset:** Allow-list redirect URLs in Supabase Auth (GoTrue) for each origin you use, e.g. `http://localhost:8098/reset-password` (Docker) or `http://localhost:5173/reset-password` (Vite default). Recovery emails use the stack’s SMTP (MailHog in dev).
- **Captcha (optional):** Set `VITE_TURNSTILE_SITE_KEY` for Cloudflare Turnstile on login/sign-up. Configure matching GoTrue captcha settings for production.
- **DB migrations:** Run `003_rls_and_campaign_failed.sql` then `004_rls_enrichment_tables.sql` **after** `002` when using AI enrichment (RLS on enrichment tables).
- **API:** Rate-limited JSON routes; structured logs via `pino` (`LOG_LEVEL` optional).

## Structure

- `frontend/` — Vite + React UI  
- `api/` — Express API  
- `supabase/` — migrations & Kong config  
- `docker-compose.yml` — full stack

## AI enrichment & Microsoft 365 (optional)

1. Apply DB migration `002_enrichment_schema.sql` (e.g. run your usual Postgres migrate flow for this repo).
2. Set `OPENAI_API_KEY` on the API process (see `.env.example`).
3. For production-style sends from a shared mailbox, set `MS365_SMTP_*`. If unset, the API keeps using MailHog in `NODE_ENV=development`.
4. To process inbound replies (unsubscribe detection, forwarding), set `MS365_IMAP_*` and optionally `MAILBOX_POLL_SEC`. The poller starts with the API server.

**Flow:** Enable AI on a campaign (create dialog or campaign detail) → save template in the builder → **Run AI enrich** → **Review enrichment** → approve rows → **Send approved**.

## License

See repository settings on GitHub for the project license.
