An Express.js-based email service that allows users to send emails with optional attachments via API calls. It includes request validation with an API key and utilizes TypeScript for enhanced type safety.
Check the project at https://email-service-coral.vercel.app
- Send emails with plain text or HTML content.
- Support for multiple file attachments.
- API key validation for secure access.
- Written in TypeScript for improved development experience.
- Middleware-powered architecture using Multer for file uploads.
Before running the project, ensure you have:
git clone https://github.com/abhinay9601/email-service.git
cd email-service
Install the required dependencies using the following command:
npm install
Create a .env file in the root directory and add the following variables:
PORT=3000
EMAIL_HOST=smtp.example.com
EMAIL_PORT=587
[email protected]
EMAIL_PASS=your-email-password
API_KEY=your-secure-api-key
NOTE:- For API key contact [email protected]
Replace placeholders with actual values:
EMAIL_HOST: SMTP host of your email provider. EMAIL_PORT: SMTP port (e.g., 587 for TLS). EMAIL_USER: Email account username. EMAIL_PASS: Email account password (or app password if using Gmail). API_KEY: A secure API key to authenticate requests
Start the development server with the following command:
npm run dev
Build the project and run it in production:
npm run build
npm start
Endpoint: POST /api/send-email
x-api-key: Your secure API key.
to (string): Recipient email address (required). subject (string): Email subject (required). text (string): Plain text content (required). html (string): HTML content (required). attachments: Array<{ filename: string, content: any }> (optional)
curl -X POST https://email-service-coral.vercel.app/api/send-email \
-H "x-api-key: your-secure-api-key" \
-F "[email protected]" \
-F "subject=Test Email with Attachments" \
-F "text=Hello from Email Service!" \
-F "attachments=@path/to/file1.txt" \
-F "attachments=@path/to/file2.jpg"
- 200 OK: Email sent successfully.
{
"message": "Email sent successfully",
"info": { ... }
}
- 400 Bad Request: Missing required fields
{
"error": "Missing required fields: to, subject, text or html"
}
- 401 Unauthorized: Missing API key.
{
"error": "API key is missing"
}
- 403 Forbidden: Invalid API key.
{
"error": "Invalid API key"
}
- 500 Internal Server Error: Failed to send email.
{
"error": "Failed to send email",
"details": "Error details here"
}
email-service/
├── src/
│ ├── config/
│ │ └── emailConfig.ts # Nodemailer configuration
│ ├── controllers/
│ │ └── emailController.ts # Email logic
│ ├── middlewares/
│ │ └── validateApiKey.ts # API key validation middleware
│ └── app.ts # Express app setup
├── uploads/ # Directory for temporary file storage
├── .env # Environment variables
├── package.json
├── tsconfig.json # TypeScript configuration
├── README.md # Project documentation
- Express
- TypeScript
- Nodemailer
- Multer
- ts-node
- dotenv
##Troubleshooting
-
Invalid login credentials: Ensure correct email/password in the .env file. If using Gmail, enable "less secure apps" or use an app password.
-
File deletion errors: Verify the file paths and permissions for the uploads directory.