Mailing Microservice
Solution for Moroccan PHPers's February 2022 Challenge by Rabyâ Raghib ([email protected]).
It mainly consists of:
-
a php app that provides 3 endpoints
POST /send
: request sending an email. It expects a payload like:{ "sender": "[email protected]", // the email address of the sender. "recipient": "[email protected]", // the email address of the receiver. "message": "Hello World!", // email body as HTML format. "priority": 4 // oprtional, default to 0 }
GET /status/{id}
: get the status of request with id{id}
, the response:{ "request_id": "24E69C73-D9D6-7B93-77F3-82D7968E8DED", "status": "processing", "priority": 3 }
POST /status-webhook
: to handle webhook calls it expect:{ "ID": "The id we sent in the first request", "STATUS": "DELIVERED" | "REJECTED" | "FAILED" }
-
a worker for submitting emails (that failed submitting in the
/send
request) for delivery taking in consideration prioritizationIts background job that keep checking & submitting queued emails every
$WORKER_PERIOD_SECONDS
or can also run only once and exit. See Project setup Section.
Architecture diagram:
Project setup:
Please ensure you set the env variables before running the project. either in .env
, .env.local
(not committed) or via system environment variables
# run both app & worker services
docker-compose up -d
or instead you can run just the app via:
docker-compose up -d nginx
you can try submitting queued emails via the worker with:
# To keep checking & submitting queued emails every $WORKER_PERIOD_SECONDS
docker-compose up -d worker-service
# Submit all current queued emails and exist
docker-compose run worker-service " " # clearing default CMD (--serve)
Assumptions:
List of assumptions if you had to take any.
- The webhook call is made via a
POST
request to/status-webhook
- An env variable will be set with the mail delivery service
API_BASE
- This is a private api that can be accessed only by trusted (authorized) services
Improvements:
What would you have added if you had more time.