# Email Capture Flask App

A minimal Flask service that accepts an email from a form POST and stores it in a SQLite database.

## Endpoints

- `POST /subscribe` — Accepts `email` via HTML form or JSON and stores it.
- `GET /stats` — Returns a count of stored subscribers.
- `GET /health` — Basic health check.

## Quick start

1. Create and activate a virtualenv (recommended):

```bash
python -m venv .venv
source .venv/bin/activate  # Windows: .venv\Scripts\activate
```

2. Install dependencies:

```bash
pip install -r requirements.txt
```

3. Run the app:

```bash
python app.py
# or
FLASK_DEBUG=1 python app.py
```

The server listens on `http://localhost:5000`.

## Configure

- `DATABASE_PATH` (optional): path to the SQLite database file. Default is `emails.db` in the project root.
- `PORT` (optional): server port (default `5000`).

## Posting from your HTML form

Your existing form should point to `/subscribe` and include an `email` field:

```html
<form action="http://localhost:5000/subscribe" method="post">
  <input type="email" name="email" placeholder="you@example.com" required />
  <button type="submit">Subscribe</button>
</form>
```

If your web page is served from a different origin (domain/port), you may need to enable CORS on this service (e.g., via a proxy or using `flask-cors`).

## cURL examples

- Form POST:

```bash
curl -X POST -d "email=test@example.com" http://localhost:5000/subscribe
```

- JSON POST:

```bash
curl -X POST -H "Content-Type: application/json" \
     -d '{"email":"test@example.com"}' \
     http://localhost:5000/subscribe
```

- Check stats:

```bash
curl http://localhost:5000/stats
```

## Notes

- Emails are normalized to lowercase and checked with a simple regex. For strict validation, consider the [`email-validator`](https://pypi.org/project/email-validator/) package.
- Duplicate submissions return HTTP 409 with status `duplicate`.
- SQLite is suitable for lightweight usage; for higher traffic, consider a server database (PostgreSQL, MySQL) with a proper connection pool.