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
— Acceptsemail
via HTML form or JSON and stores it.GET /stats
— Returns a count of stored subscribers.GET /health
— Basic health check.
Quick start
- Create and activate a virtualenv (recommended):
python -m venv .venv
source .venv/bin/activate # Windows: .venv\Scripts\activate
- Install dependencies:
pip install -r requirements.txt
- Run the app:
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 isemails.db
in the project root.PORT
(optional): server port (default5000
).
Posting from your HTML form
Your existing form should point to /subscribe
and include an email
field:
<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:
curl -X POST -d "email=test@example.com" http://localhost:5000/subscribe
- JSON POST:
curl -X POST -H "Content-Type: application/json" \
-d '{"email":"test@example.com"}' \
http://localhost:5000/subscribe
- Check stats:
curl http://localhost:5000/stats
Notes
- Emails are normalized to lowercase and checked with a simple regex. For strict validation, consider the
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.