FastAPI service for sending emails through an SMTP server using a simple HTTP API.
This project receives SMTP settings and email content as JSON, then relays the message through the SMTP server you provide. It is useful when you want to let another service send mail without exposing SMTP credentials directly to that service.
- Accepts requests at
/sendand/send_async - Authenticates requests with an
Authorizationheader - Builds and sends plain text, HTML, and attachment-based emails
- Supports TLS and SSL SMTP connections
- Can run locally, in Docker, or as a Vercel Python function wrapper
Every request must include an Authorization header in the form:
Authorization: Bearer your-api-key
The token type must match ALLOWED_TOKEN and the key must be present in ALLOWED_API_KEYS.
| Method | Path | Description |
|---|---|---|
POST |
/send |
Sends all emails before responding |
POST |
/send_async |
Queues the work in the background and responds immediately |
The request body follows the EmailPayload schema:
smtp_config: SMTP host, port, username, password, and connection flagssender: The sender address used when sending mailmessages: A list of messages to send
Note: the current code uses the top-level sender field when sending mail. Keep it populated.
{
"smtp_config": {
"host": "smtp.example.com",
"port": 587,
"username": "user@example.com",
"password": "your-password",
"sender": { "email": "user@example.com", "name": "Example Sender" },
"use_tls": true,
"use_ssl": false,
"timeout": 10
},
"sender": { "email": "user@example.com", "name": "Example Sender" },
"messages": [
{
"to": [{ "email": "recipient@example.com", "name": "Recipient" }],
"subject": "Test message",
"text_body": "Hello from the SMTP proxy",
"html_body": "<p>Hello from the SMTP proxy</p>"
}
]
}/send
{ "message": "1 email's sent successfully, 0 failed.", "status": "success", "sent": 1 }/send_async
{ "message": "Email sending initiated in the background.", "status": "initiated", "queued": 1 }Create a .env file or set these in your deployment platform:
ALLOWED_API_KEYS=mykey1,mykey2
ALLOWED_TOKEN=Bearer
DEBUG=trueALLOWED_API_KEYS: Comma-separated list of allowed API keysALLOWED_TOKEN: The token prefix expected in theAuthorizationheaderDEBUG: Set totrueto enable/docs,/redoc, and/openapi.json
If neither ALLOWED_API_KEYS nor ALLOWED_TOKEN is set, the app will fail on startup.
- Install dependencies
uv sync- Start the API
DEBUG=true uv run uvicorn app.app:app --reload --host 0.0.0.0 --port 8000- Open the docs in your browser
http://127.0.0.1:8000/docs
If you do not want to use uv, install the dependencies from pyproject.toml and run:
DEBUG=true python -m uvicorn app.app:app --reload --host 0.0.0.0 --port 8000docker pull krsahil8825/http-smtp-proxy:latestUsing a .env file:
docker run -d \
--name http-smtp-proxy \
--restart unless-stopped \
-p 8000:8000 \
--env-file .env \
krsahil8825/http-smtp-proxy:latestOr pass the environment variables directly:
docker run -d \
--name http-smtp-proxy \
--restart unless-stopped \
-p 8000:8000 \
-e ALLOWED_API_KEYS=mykey1,mykey2 \
-e ALLOWED_TOKEN=Bearer \
-e DEBUG=false \
krsahil8825/http-smtp-proxy:latestCreate a docker-compose.yml file:
# using .env file
version: "3.9"
services:
http-smtp-proxy:
image: krsahil8825/http-smtp-proxy:latest
container_name: http-smtp-proxy
restart: unless-stopped
ports:
- "8000:8000"
env_file:
- .env# without .env file
version: "3.9"
services:
http-smtp-proxy:
image: krsahil8825/http-smtp-proxy:latest
container_name: http-smtp-proxy
restart: unless-stopped
ports:
- "8000:8000"
environment:
- ALLOWED_API_KEYS=mykey1,mykey2
- ALLOWED_TOKEN=Bearer
- DEBUG=falseStart the container:
docker compose up -dStop the container:
docker compose downView logs:
docker compose logs -fSet up a Vercel project and deploy this repository directly.
-
Open the Vercel dashboard and create a new project.
-
Connect your GitHub repository containing this code.
-
Select
FastAPIas the framework preset. -
Add build command
pip install uv && uv export --no-hashes > requirements.txt -
Dont change the output directory, leave it as
/or.. -
Install command
pip install -r requirements.txt -
Dont change the Deploy command (if available, leave it as default).
-
Add the environment variables
ALLOWED_API_KEYS,ALLOWED_TOKEN, andDEBUGin the Vercel project settings. -
Deploy the project.
app/app.py: FastAPI app and send logicapp/schema.py: Request and response modelsDockerfile: Production container buildpyproject.toml: Python dependencies and project metadatauv.lock: Locked dependency versions
- If you get
401 Unauthorized, check theAuthorizationheader and theALLOWED_API_KEYS/ALLOWED_TOKENvalues. - If the app exits on startup, confirm at least one allowed API key or token setting is present.
- If email sending fails, verify the SMTP host, port, authentication, TLS/SSL mode, and recipient addresses.
- If you want interactive API docs, start the app with
DEBUG=true. But keep it disabled in production for security reasons.
See LICENSE for the full text.