A comprehensive NestJS mail package with modern design patterns for seamless, scalable email handling in enterprise applications.
npm install nestjs-mailable
import { Module } from '@nestjs/common';
import { MailModule } from 'nestjs-mailable';
@Module({
imports: [
MailModule.forRoot({
config: {
default: 'smtp',
mailers: {
smtp: {
transport: 'smtp',
host: 'smtp.gmail.com',
port: 587,
secure: false,
auth: {
user: 'your-email@gmail.com',
pass: 'your-app-password'
}
}
},
from: {
address: 'noreply@yourapp.com',
name: 'Your App Name'
}
}
})
],
})
export class AppModule {}
import { Injectable } from '@nestjs/common';
import { MailService } from 'nestjs-mailable';
@Injectable()
export class UserService {
constructor(private mailService: MailService) {}
async sendWelcomeEmail(user: { email: string; name: string }) {
await this.mailService
.to({ address: user.email, name: user.name })
.subject(`Welcome ${user.name}!`)
.html(`<h1>Welcome to our platform!</h1>
<p>Hi ${user.name}, thanks for joining us.</p>`)
.tag('welcome')
.tag('onboarding')
.send();
}
}
import { Mailable } from 'nestjs-mailable';
export class WelcomeMail extends Mailable {
constructor(private user: { email: string; name: string }) {
super();
}
protected build() {
return this
.to({ address: this.user.email, name: this.user.name })
.subject(`Welcome ${this.user.name}!`)
.view('emails.welcome', {
userName: this.user.name,
appName: 'Your App',
loginUrl: 'https://yourapp.com/login'
})
.tag('welcome');
}
}
// Usage in service
@Injectable()
export class UserService {
constructor(private mailService: MailService) {}
async sendWelcomeEmail(user: { email: string; name: string }) {
const welcomeMail = new WelcomeMail(user);
await this.mailService.send(welcomeMail);
}
}
Create a template file templates/emails/welcome.hbs
:
Built with proven design patterns for elegant and maintainable email handling, following NestJS best practices.
Comprehensive error handling, retry mechanisms, and monitoring capabilities for enterprise applications.
Type-safe APIs, extensive documentation, and debugging tools make development a breeze.
Modular design allows you to use only what you need, with easy extensibility for custom requirements.