Skip to main content

Getting Started

@nestjs-modules/mailer is a mailer module for the NestJS framework powered by Nodemailer.

Installation

Install the core package and nodemailer:

pnpm add @nestjs-modules/mailer nodemailer

Install the TypeScript types for nodemailer:

pnpm add -D @types/nodemailer

Template Engines (optional)

Install the template engine(s) you plan to use:

# Handlebars
pnpm add handlebars

# Pug
pnpm add pug

# EJS
pnpm add ejs

# Liquid
pnpm add liquidjs

# MJML (responsive emails)
pnpm add mjml

Basic Usage

Import MailerModule into your root AppModule:

import { Module } from '@nestjs/common';
import { MailerModule } from '@nestjs-modules/mailer';
import { HandlebarsAdapter } from '@nestjs-modules/mailer/adapters/handlebars.adapter';

@Module({
imports: [
MailerModule.forRoot({
transport: {
host: 'smtp.example.com',
port: 587,
secure: false,
auth: {
user: 'username',
pass: 'password',
},
},
defaults: {
from: '"No Reply" <noreply@example.com>',
},
template: {
dir: __dirname + '/templates',
adapter: new HandlebarsAdapter(),
options: {
strict: true,
},
},
}),
],
})
export class AppModule {}

Then inject and use MailerService:

import { Injectable } from '@nestjs/common';
import { MailerService } from '@nestjs-modules/mailer';

@Injectable()
export class NotificationService {
constructor(private readonly mailerService: MailerService) {}

async sendWelcomeEmail(email: string, name: string) {
await this.mailerService.sendMail({
to: email,
subject: 'Welcome!',
template: 'welcome',
context: { name },
});
}
}