etm-email-service
v1.0.0
Published
NestJS Email Service
Downloads
3
Readme
NestJS Email Service
This project provides an email service using NestJS, designed to handle both individual and bulk email requests. It integrates with external email services and allows configuration through environment variables.
Table of Contents
Project Overview
This NestJS application provides endpoints to send emails using a configurable email service. It supports sending both single and bulk emails. The email service is designed to be flexible and can be adapted to various email providers such as Nodemailer, Mailgun, and Postmark.
Features
- Single Email Sending: Send individual emails with customizable options.
- Bulk Email Sending: Send emails to multiple recipients with a single request.
- Configurable: Use environment variables to configure the email service.
- Logging: Includes logging for debugging and error reporting.
Prerequisites
Before you begin, ensure you have met the following requirements:
Node.js: Version 14 or higher.
NestJS CLI: Install the NestJS CLI globally using npm: npm install -g @nestjs/cli
Email Service: Access to an email service provider and necessary API keys.
Installation
Clone the Repository:
git clone https://github.com/wondwosen-bewketu/Email-service.git
Navigate to the Project Directory**:
cd Email-service
Install Dependencies**:
npm install
Set Up Environment Variables**: Create a
.env
file in the root directory and add your email service configuration:EMAIL_PROVIDER=nodemailer SMTP_HOST=mail.example.com SMTP_PORT=587 [email protected] SMTP_PASS=yourpassword [email protected] MAILGUN_API_KEY=your-mailgun-api-key MAILGUN_DOMAIN=your-mailgun-domain POSTMARK_API_KEY=your-postmark-api-key
Configuration
Configuration is handled through the email.config.ts
file and environment variables. The EmailService
is initialized with the settings from these files.
Example email.config.ts
import { ConfigService } from '@nestjs/config';
export class EmailConfig {
static getEmailConfig(configService: ConfigService) {
return {
provider: configService.get<string>('EMAIL_PROVIDER'),
smtpHost: configService.get<string>('SMTP_HOST'),
smtpPort: configService.get<number>('SMTP_PORT'),
smtpUser: configService.get<string>('SMTP_USER'),
smtpPass: configService.get<string>('SMTP_PASS'),
fromEmail: configService.get<string>('FROM_EMAIL'),
mailgunApiKey: configService.get<string>('MAILGUN_API_KEY'),
mailgunDomain: configService.get<string>('MAILGUN_DOMAIN'),
postmarkApiKey: configService.get<string>('POSTMARK_API_KEY'),
};
}
}
Usage
Endpoints
Send Email
- URL:
/emails/send-email
- Method: POST
- Description: Sends a single email.
- Request Body:
{ "to": "[email protected]", "subject": "Your Subject Here", "context": { "key1": "value1" }, "text": "Plain text content", "customTemplatePath": "path/to/template.hbs" }
Send Bulk Email
- URL:
/emails/send-bulk-email
- Method: POST
- Description: Sends emails to multiple recipients.
- Request Body:
{ "to": ["[email protected]", "[email protected]"], "subject": "Your Subject Here", "context": { "key1": "value1" }, "text": "Plain text content", "customTemplatePath": "path/to/template.hbs" }
Starting the Server
To start the server, use the following command:
npm run start