# Sunbelt Mail Matrix

Email automation and campaign management app for Baytown Networking Professionals (BNP), B2B, and B2C outreach.

## Stack

- **Frontend**: React + TypeScript + Vite + Tailwind CSS
- **Backend**: Express + Node.js (TypeScript via tsx)
- **Database**: Supabase (PostgreSQL)
- **Email**: Nodemailer → Namecheap Private Email (SMTP)
- **Tracking**: Open pixel + click wrapping via Express backend

## Running on Replit

Two workflows run simultaneously:

| Workflow | Command | Port | Purpose |
|---|---|---|---|
| Start application | `npm run dev` | 5000 | React frontend (Vite, proxies /api to 8080) |
| Backend Server | `node --import tsx/esm server.ts` | 8080 | Express API, SMTP engine, scheduler |

## Required Environment Variables / Secrets

| Key | Where | Notes |
|---|---|---|
| `VITE_SUPABASE_URL` | .env | Supabase project URL |
| `VITE_SUPABASE_ANON_KEY` | .env | Supabase anon/publishable key |
| `SUPABASE_SERVICE_ROLE_KEY` | Secret | Supabase service role key — bypasses RLS for server writes |
| `SMTP_HOST` | .env | `mail.privateemail.com` |
| `SMTP_PORT` | .env | `465` |
| `SMTP_USER` | .env | SMTP email address |
| `SMTP_PASS` | .env | SMTP password |
| `SMTP_SECURE` | .env | `true` (SSL on port 465) |
| `TRACKING_BASE_URL` | Secret | **On Replit**: auto-detected from REPLIT_DEV_DOMAIN. **On VPS**: set to `https://server1.sunbeltaiservices.com` |
| `PORT` | .env | `8080` |

## VPS Deployment (Namecheap)

**Target server**: `server1.sunbeltaiservices.com` (184.94.212.117)

### Steps when ready to migrate

1. **Install Node.js 18+** on the VPS
2. **Clone or upload** the project files
3. **Copy environment variables** — replicate all `.env` values and secrets
4. **Update `TRACKING_BASE_URL`** — set to `https://server1.sunbeltaiservices.com` (or the subdomain you assign)
5. **Install dependencies**: `npm install`
6. **Build the frontend**: `npm run build` → produces `dist/` folder
7. **Run the backend**: `node --import tsx/esm server.ts` (or use PM2 for process management)
8. **Configure Nginx**:
   - Serve `dist/` as the static frontend on port 80/443
   - Proxy `/api` to `localhost:8080` for the backend
9. **SSL**: Use Let's Encrypt (Certbot) for HTTPS
10. **SMTP**: Port 465 works natively on Namecheap VPS — no extra config needed

### Nginx config sketch

```nginx
server {
    listen 443 ssl;
    server_name server1.sunbeltaiservices.com;

    root /var/www/sunbelt/dist;
    index index.html;

    location /api/ {
        proxy_pass http://localhost:8080;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
    }

    location / {
        try_files $uri $uri/ /index.html;
    }
}
```

## Email Tracking Architecture

Tracking URLs in sent emails point to the Express backend (`/api/track/open/:id` and `/api/track/click/:id`).

- **Opens** → recorded in `email_opens` table, linked via `email_send_id`
- **Clicks** → recorded in `email_clicks` table, linked via `email_send_id`
- **List separation** → `email_sends.list_type` column ('BNP' / 'B2B' / 'B2C') — Analytics tab filters by this

## Supabase Tables

- `members_list` — BNP members
- `business_partners` — B2B contacts
- `customer_list` — B2C contacts
- `templates` — email templates
- `email_sends` — outbound email log (with `list_type`)
- `email_opens` — open tracking events
- `email_clicks` — click tracking events
- `scheduled_campaigns` — future-dated sends
