FastAPI-based REST API for sending emails
The regtech-mail-api has been incorporated into the SBL Project docker-compose. See the SBL Project LOCAL_DEV_COMPOSE for instructions on running the services.
pip install poetry
poetry install
poetry shell
pytest (from within a poetry shell)
or
poetry run pytest
The API endpoints require authentication to access. The service uses the regtech-api-commons library to utilize OAuth2 authentication, currently using Keycloak. To use either endpoint, you must first get an access token from Keycloak. The Contact Name and Email, derived from the Access Token, will be put into the body of the email.
To get an access token, run the following curl command, using the Keycloak user you wish to test with (see LOCAL_DEV_COMPOSE for launching Keycloak):
export RT_ACCESS_TOKEN=$(curl 'localhost:8880/realms/regtech/protocol/openid-connect/token' \
-X POST \
-H 'Content-Type: application/x-www-form-urlencoded' \
--data-urlencode 'username=<user>' \
--data-urlencode 'password=<pass>' \
--data-urlencode 'grant_type=password' \
--data-urlencode 'client_id=regtech-client' | jq -r '.access_token')
GET /
curl -H "Authorization: Bearer ${RT_ACCESS_TOKEN}" http://localhost:8765
{"message":"Welcome to the Email API"}
POST /send
curl -vs -X POST \
-H "Authorization: Bearer ${RT_ACCESS_TOKEN}" \
-H "X-Mail-Subject: Institution Profile Change" \
-F lei=1234567890ABCDEFGHIJ \
-F "institution_name_1=Fintech 1" \
-F tin_1=12-3456789 \
-F rssd_1=1234567 \
http://localhost:8765/send | jq '.'
{
"email": {
"subject": "Institution Profile Change",
"body": "lei: 1234567890ABCDEFGHIJ\ninstitution_name_1: Fintech 1\ntin_1: 12-3456789\nrssd_1: 1234567",
"from_addr": "noreply@localhost.localdomain",
"to": [
"cases@localhost.localdomain"
],
"cc": null,
"bcc": null
}
}
The developer setup uses Mailpit as a mock SMTP server. The Mail API is pre-configured to point at Mailpit's SMTP port. Mailpit also includes a web interface for viewing email messages.
Mailpit has been included in the SBL Project docker-compose. See the SBL Project LOCAL_DEV_COMPOSE.
You can browse your emails at: