Skip to content

mailexam/FastAPI

Repository files navigation

FastAPI + Mailexam

Minimal FastAPI example that sends test mail through Mailexam SMTP via the Python standard library smtplib.

Based on the Mailexam FastAPI guide.

What you need

  • A Mailexam account and a project with SMTP credentials.
  • Python 3.10+ and pip.

From your Mailexam welcome email or dashboard:

Variable Description
MAILEXAM_LOGIN SMTP login (for example, xxxxx)
MAILEXAM_PASSWORD SMTP password (paired with the login)
Host {MAILEXAM_LOGIN}.mailexam.ru (built automatically in code)

Quick start (host)

  1. Create a virtual environment and install dependencies:
python3 -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt
  1. Copy the example environment file and fill in your credentials:
cp .env.example .env
  1. Edit .env:
MAILEXAM_LOGIN=YOUR_LOGIN
MAILEXAM_PASSWORD=YOUR_PASSWORD
MAILEXAM_PORT=587
MAIL_FROM=noreply@example.test
  1. Run the server:
uvicorn main:app --host 127.0.0.1 --port 8000

The server listens on http://127.0.0.1:8000 by default. Interactive API docs: http://127.0.0.1:8000/docs

  1. Send a test message:
curl -X POST http://127.0.0.1:8000/mail/test \
  -H 'Content-Type: application/json' \
  -d '{"to":"user@example.test","subject":"Test","body":"Hello"}'

The message appears in the Mailexam dashboard → your project → inbox.

Environment variables

Variable Required Default Description
MAILEXAM_LOGIN yes SMTP login; also used to build the host name
MAILEXAM_PASSWORD yes SMTP password
MAILEXAM_PORT no 587 SMTP port (587, 2525, or 25)
MAIL_FROM no noreply@example.test Sender address (any test address is fine)
HTTP_HOST no 127.0.0.1 HTTP bind address (used in Docker)
HTTP_PORT no 8000 HTTP listen port (used in Docker)

For port 587 the code calls starttls() before login. For port 25 it connects without STARTTLS.

Project layout

.
├── requirements.txt
├── mail.py            # smtplib transport and send_test()
├── main.py            # FastAPI app and POST /mail/test
├── .env.example
├── Dockerfile         # for local debugging only
└── docker-compose.yml

Docker (debugging)

Docker is provided for local debugging. For day-to-day development, run the app on the host with uvicorn (see above).

cp .env.example .env
# edit .env with your credentials

docker compose up --build

Then call the same endpoint on the mapped port:

curl -X POST http://127.0.0.1:8000/mail/test \
  -H 'Content-Type: application/json' \
  -d '{"to":"user@example.test","subject":"Test","body":"Hello"}'

Inside the container the server binds to 0.0.0.0:8000 so the port mapping works.

CI

Set these secrets in your CI environment:

variables:
  MAILEXAM_LOGIN: $MAILEXAM_LOGIN
  MAILEXAM_PASSWORD: $MAILEXAM_PASSWORD
  MAILEXAM_PORT: "587"
  MAIL_FROM: "noreply@example.test"

After sending a message in a test, verify delivery via the Mailexam API.

Troubleshooting

TLS or connection error

  • Host must be {login}.mailexam.ru, where {login} matches MAILEXAM_LOGIN.
  • Login and password must come from the same Mailexam project.
  • For port 587 call starttls() before login().

Event loop blocking

  • send_test() runs inside asyncio.to_thread() so the async handler does not block.

Message not in the dashboard

  • Open the inbox of the same Mailexam project.
  • Check the /mail/test response and uvicorn logs.

Port already in use

  • Change the port in the uvicorn command or set HTTP_PORT in Docker.

See also

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors