A secure Node.js REST API for sending templated emails via Resend, designed specifically for contact form submissions and automated email notifications.
- Template-based emails - Pre-designed HTML/text email templates
- Multi-language support - Templates available in multiple languages (ES/EU)
- Resend integration - Send emails via Resend's reliable API
- Input validation - Robust validation with Joi
- Smart rate limiting - Environment-aware rate limiting (stricter in production)
- Security first - CORS, Helmet, and security headers
- Health monitoring - Built-in health check endpoints
- Environment-based config - Easy deployment across environments
-
Clone the repository
git clone <repository-url> cd email-sender
-
Install dependencies
npm install
-
Configure environment
cp .env.example .env # Edit .env with your Resend API key and sender email -
Start the server
# Development npm run dev # Production npm start
Create a .env file with the following variables:
# Server Configuration
PORT=3000
NODE_ENV=development
# Resend Configuration
RESEND_API_KEY=re_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
FROM_EMAIL=[email protected]
# Optional
ALLOWED_ORIGINS=http://localhost:3000,https://yourdomain.com- Sign up at resend.com
- Generate an API key from your dashboard
- Add your domain and verify it (or use
[email protected]for testing) - Set your
FROM_EMAILto a verified domain email address
- High deliverability - Built for modern email delivery
- No SMTP complexity - Simple REST API
- Built-in analytics - Track email performance
- Developer-friendly - Clean API and excellent documentation
GET /health
Returns server status and uptime.
{
"status": "ok",
"timestamp": "2025-09-13T10:30:00.000Z",
"uptime": 3600.123
}GET /api/test-connection
Verifies Resend API configuration.
{
"success": true,
"message": "Resend API connection is working",
"connected": true
}POST /api/contact-form
Sends a templated email based on contact form data.
Request:
{
"templateName": "laguntza-fisioterapia",
"to": "[email protected]",
"lang": "es",
"data": {
"name": "John Doe",
"email": "[email protected]",
"phone": "123-456-7890",
"message": "I need more information about your services",
"source": "Website"
},
"from": "[email protected]"
}Response:
{
"success": true,
"message": "Template email sent successfully",
"messageId": "re_abc123def456",
"template": "laguntza-fisioterapia"
}GET /api/templates
Returns list of available email templates.
{
"success": true,
"message": "Available templates retrieved successfully",
"templates": ["laguntza-fisioterapia"]
}Templates are organized by name and language:
templates/
βββ laguntza-fisioterapia/
β βββ es.html # Spanish HTML template
β βββ es.txt # Spanish text template
β βββ eu.html # Basque HTML template
β βββ eu.txt # Basque text template
Templates support variable interpolation using {{variable}} syntax:
{{name}}- Customer name{{email}}- Customer email{{phone}}- Customer phone{{message}}- Customer message{{source}}- How they found you
- Create a new folder in
templates/ - Add HTML and text files for each language
- Update
emailTemplateService.jsto include the new template - Add template logic in
getAvailableTemplates()
- Rate limit: 100 requests per 15 minutes per IP
- Primary limit: 2 requests per minute per IP
- Secondary limit: 5 requests per hour per IP
- Both limits are enforced simultaneously
- Helmet.js for security headers
- CORS configuration
- Input validation and sanitization
- Environment-based error messaging
Use the included requests.http file with your HTTP client:
POST http://localhost:3000/api/contact-form
Content-Type: application/json
{
"templateName": "laguntza-fisioterapia",
"to": "[email protected]",
"lang": "es",
"data": {
"name": "Test User",
"email": "[email protected]",
"phone": "123456789",
"message": "Test message",
"source": "Website"
}
}Run the test script:
node test-api.js- Set
NODE_ENV=production - Configure production Resend API key and verified domain
- Set appropriate
ALLOWED_ORIGINS - Ensure secure Resend API credentials
FROM node:18-alpine
WORKDIR /app
COPY package*.json ./
RUN npm ci --only=production
COPY . .
EXPOSE 3000
CMD ["npm", "start"]email-sender/
βββ routes/
β βββ email.js # API routes
βββ services/
β βββ emailService.js # Resend email service
β βββ emailTemplateService.js # Template management
βββ middleware/
β βββ validation.js # Input validation
βββ templates/
β βββ laguntza-fisioterapia/ # Email templates
βββ examples/
β βββ usage.js # Usage examples
βββ server.js # Main application
βββ requests.http # HTTP test requests
βββ test-api.js # API test script
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests if applicable
- Submit a pull request
MIT License - see LICENSE file for details.
- Check the
API_TESTING.mdfor detailed testing examples - Review
examples/usage.jsfor implementation patterns - Use the health check endpoint to verify configuration