Nest Mail Service

An example of email sending service using NestJS
Alternatives To Nest Mail Service
Project NameStarsDownloadsRepos Using ThisPackages Using ThisMost Recent CommitTotal ReleasesLatest ReleaseOpen IssuesLicenseLanguage
Mailer76078 days ago27June 20, 202284mitTypeScript
📨 A mailer module for Nest framework (node.js)
Nestjs Email Authentication375
8 months ago10mitTypeScript
Nestjs Starter using Mongodb and Passportjs
Nestjs Mailer63
2 years ago2mitHandlebars
🌈 A simple implementation example with and without email-templates using mailer module for nest js built on top of nodemailer.
Nestjs Boilerplate34
5 months ago12mitTypeScript
🐈 GraphQL NestJS starter
Nest Sendgrid28133 days ago207June 04, 20234mitTypeScript
Nest Mail Service27
2 years ago3TypeScript
An example of email sending service using NestJS
Nestjs Mailgun25
a month ago14mitTypeScript
This is a simple wrapper of mailgun-js. It only comtemplates the send email and verify email functionalities, but later more it will be added. Just ping me or open pull request and contribute :)
Homingbot7
6 years agomitPython
Fake SMTP Server & Low-Maintenance Email Generator
Zeebe Nestjs Transactional Email Service6
4 years ago1TypeScript
A NestJS-based node microservice to send transactional email with Zeebe
Music Songs Genre Dataset5
3 years ago1
Classification & Implementation of Machine Learning ALgorithms on Music Dataset
Alternatives To Nest Mail Service
Select To Compare


Alternative Project Comparisons
Readme

nest-mail-service

Build Status Coverage Status semantic-release

System Diagram

Tech stack

NestJS/Typescript, MongoDB, Redis, Queue

Diagram

How system works:

  • Client will POST /api/email to send email.
  • The endpoint will:
    • Perform validation to check the request payload.
    • New email document will be created in mongodb database
    • Dispatch a message to main queue.
    • Immediately response to client HTTP 201 Created with email id
    • If can not create new document, immediately response HTTP 500
    • If can create new document but can not dispatch message to main queue, api still response HTTP 201
  • Email Sender Worker will:
    • Long polling from the main queue. I use rsmq to implement main queue, but it can easily replaced with SQS.
    • Set the message visibility to prevent other worker instance pull this message again.
    • Attempt to send email
    • If success, delete message from the queue
    • If fail, update message status together with the visibility to let it available again in the queue for next time retry
    • In case if can not retry at all after a configured number of retries, it will dispatch a message to DeadLetter queue for analysis and mark the email document as Failed
  • Worker will:
    • Run every 30 mins
    • Try to scan any email documents that need to be enqueue again. This will be backup solution in case API did not dispatch message successfully.
    • Dispatch message to main queue

How Worker Fetch Enqueue Emails

  • We also have internal components to support exponential backoff and circuit breaker mechanism when selecting email provider

App scaling

  • All the endpoints are very light load. Can scale easily.
  • To clear the queue faster, we can increase the number of Email Sender Worker instances.

App components

Components

  • Currently App Module and Worker Module are all loaded in app.ts but we can easily separate it into worker and service to run separately

Interesting features

  • Smart Mail Transfer Agent Stategy
  • Circuit Breaker for Selecting Mail Transfer Agent

Things that can be improved

  • For the Worker, acquire mutex lock before executed to prevent other Worker instance to run while it running
  • Data sharding to improve write performance.
  • Switch to use Lambda
  • Apply mutex lock to guarantee that only one message that will be processed at a time without affect performance

Versioning

We use semantic-release to generate release notes. This make use of conventional commit structure for both the notes and release numbers.

Setup your working environment:

Quick start

  • Update mails config section in default.json config to use your own keys
  • docker-compose up -d

Available tasks

npm install
docker-compose up -d  # to start the app by docker, including mongodb container. This will build and use `production` config
npm run start:local # to start app in your local machine, that will connect to docker mongodb container
npm run start:local:watch # to start app and watch for any changes
npm run test # to run unit test
npm run test:e2e  # to run integration test
npm run test:watch # to test app and keep watching files change
npm run cover # to get code coverage
  • To start develop new feature, you can create your own config mylocal.json file as below. Remember to run docker-compose up -d first to init mongo and redis container
{
  "mongodb": {
    "host": "localhost:27020",
    "user": "",
    "password": "",
    "database": "mails",
    "replicaSet": ""
  },
  "redis": {
    "host": "localhost",
    "port": "6380",
    "db": 2
  }
}

Using docker

  • Run
docker-compose up -d

API Documentation

There is swagger integrated to help you easier navigate through all exposed restful api. Please follow:

npm run start
open http://localhost:9001/docs/

Release Production:

  • Create Github and NPM tokens (only need to do one time)
  • Export these tokens into your ~/.bash_profile and source it:
export GH_TOKEN={your github token}
export NPM_TOKEN={your npm token} # should be optional
  • Run npm run release
Popular Nest Projects
Popular Email Projects
Popular Frameworks Categories

Get A Weekly Email With Trending Projects For These Categories
No Spam. Unsubscribe easily at any time.
Typescript
Email
Mail
Nestjs