A robust, high-performance email delivery microservice built with Node.js, Express, and Nodemailer. Designed for seamless integration, security, and scalability.
- Template System: Rich HTML templates for:
- Password Recovery
- Email Verification (with 6-digit codes)
- Workspace and Space Invitations
- Task Assignments
- Mentions and Notifications
- PR Merged Announcements
- Deadline Reminders
- Database Integration: Automatic PostgreSQL migrations for audit logs.
- Security:
- API Key based authentication (
X-Api-Key). - Request validation via
express-validator. - Secure environment configuration.
- API Key based authentication (
- Developer Experience:
- Colorized terminal logs with
picocolors. - Comprehensive integration test suite with Jest and Supertest.
- ESLint configuration for code quality.
- ESM (EcmaScript Modules) support.
- Colorized terminal logs with
- Reliability:
- Graceful connection handling for DB and SMTP.
- Health checks (
/health) with detailed component status. - Error middleware for centralized stability.
If you have make installed, you can set up the entire project in one command:
make setupThis will:
- Install dependencies (
npm install). - Create your
.envfile from the example. - Automatically generate a secure
INTERNAL_API_KEY. - Start the infrastructure (PostgreSQL) using Docker Compose.
Alternatively, you can run the bash script: ./setup.sh.
- Node.js (v18+)
- PostgreSQL (optional, but recommended for logging)
git clone https://github.com/Keiver-Dev/letterbox-mail-service.git
cd letterbox-mail-service
npm installThis service uses PostgreSQL to store audit logs of sent emails.
- Windows: Download and run the installer from postgresql.org.
- macOS (Homebrew):
brew install postgresql@15 - Linux (Ubuntu/Debian):
sudo apt install postgresql
Using psql or a tool like pgAdmin:
CREATE DATABASE letterbox;The service handles migrations automatically on startup. Ensure your .env database credentials are correct, and the tables will be created when you run npm run dev.
Create a .env file based on .env.example:
# Server
PORT=3001
NODE_ENV=development
# Security
# Generate a secure 32-character key with:
# node -e "console.log(require('crypto').randomBytes(16).toString('hex'))"
INTERNAL_API_KEY=your_secure_32_char_key
# Database
DB_HOST=localhost
DB_PORT=5432
DB_NAME=letterbox
DB_USER=postgres
DB_PASSWORD=your_password
# ─── Email ───────────────────────────────────────
# true = uses Ethereal (test emails, no real account needed)
# false = uses Gmail with the credentials below
EMAIL_TEST_MODE=true
EMAIL_USER=your_email@gmail.com
EMAIL_PASS=your_gmail_app_password
EMAIL_FROM_NAME=Letterbox
# ─── Environment ──────────────────────────────────
# Values: development, production, test
NODE_ENV=development
# ─── Frontend ────────────────────────────────────
# Base URL to build links inside emails
FRONTEND_URL=http://localhost:3000To use Gmail as your provider:
- Enable 2FA on your Google Account.
- Go to App Passwords.
- Create a new app password (e.g., "Letterbox").
- Copy the 16-character code and use it as
EMAIL_PASS.
This service uses nodemailer. You can swap Gmail for other SMTP providers (SendGrid, Mailgun, Amazon SES) by modifying src/config/email.config.js.
The easiest way to run the service is using Docker Compose, which spins up both the Node.js app and a PostgreSQL instance.
# 1. Start everything
docker-compose up -d
# 2. Check logs
docker-compose logs -f letterboxThe service will be accessible at http://localhost:3001.
The docker-compose.yml file includes:
- mail-service: The Node.js microservice.
- db: A PostgreSQL 15 instance.
- Volumes: Persistent storage for the database in
postgres_data.
Tip
You can customize the ports and credentials directly in the docker-compose.yml or via environment variables.
Runs with nodemon for automatic restarts and detailed colorized logs.
npm run devExecutes integration tests for all endpoints.
npm testnpm start| Method | Endpoint | Description |
|---|---|---|
GET |
/health |
Status of DB and SMTP connections |
POST |
/api/email/password-recovery |
Send recovery links |
POST |
/api/email/verify-email |
Send 6-digit verification codes |
POST |
/api/email/workspace-invitation |
Invite users to workspaces |
POST |
/api/email/task-assignment |
Notify task assignments |
POST |
/api/email/mention |
Notify user mentions |
curl -X POST http://localhost:3001/api/email/verify-email \
-H "Content-Type: application/json" \
-H "X-Api-Key: your_secure_32_char_key" \
-d '{
"to": "user@example.com",
"code": "123456",
"userName": "Keiver"
}'For full details on payloads, see the Endpoint Documentation.
├── src/
│ ├── config/ # Configurations (DB, Email, Migrations)
│ ├── controllers/ # Request handlers
│ ├── middlewares/ # Auth, Validation, Error Handling
│ ├── routes/ # API Route definitions
│ ├── services/ # Business logic (Mailer Service)
│ ├── templates/ # HTML/Text Email templates
│ └── utils/ # Loggers and helpers
├── tests/ # Integration and Unit tests
└── docs/ # Detailed technical documentation
1. Database Connection Failed
- Ensure PostgreSQL is running.
- Verify
DB_HOST,DB_PORT, and credentials in.env. - If using Docker, ensure the
dbservice is healthy.
2. Gmail "Invalid Login"
- Ensure
EMAIL_PASSis an App Password, not your main account password. - Verify that
EMAIL_TEST_MODEis set tofalsefor Gmail.
3. API Key Unauthorized
- All requests (except
/health) require the headerX-Api-Key. - Ensure this matches the
INTERNAL_API_KEYdefined in your.env.
4. Migrations Not Running
- Check the console logs for
[ERROR] Migration runner failed. - Ensure the database user has permissions to create tables.
Released under the MIT License. Built with care by Keiver-Dev.