Mailer module for NestJS
- Nodemailer - nodemailer (v6.6.2)
npm install --save @crowdlinker/nestjs-mailer nodemailer
npm install --save-dev @types/nodemailer
// or
// yarn add @crowdlinker/nestjs-mailer nodemailer
// yarn add -D @types/nodemailer
Note: For NestJS < v7, please download version 0.5.0 for this package.
import { NodemailerModule } from '@crowdlinker/nestjs-mailer';
import { NodemailerDrivers } from '@crowdlinker/nestjs-mailer';
import { NodemailerOptions } from '@crowdlinker/nestjs-mailer';
@Module({
imports: [
NodemailerModule.forRoot({
transport: {
host: 'smtp.mailtrap.io',
port: 2525,
auth: {
user: 'ccdff3b99c83ec',
pass: 'a700a7eafe1e28',
},
},
defaults: {
from: 'Hello @Crowdlinker <hello@crowdlinker.com>',
},
} as NodemailerOptions<NodemailerDrivers.SMTP>),
],
})
import { NodemailerModule } from '@crowdlinker/nestjs-mailer';
import { NodemailerOptions } from '@crowdlinker/nestjs-mailer';
@Module({
imports: [
NodemailerModule.forRootAsync({
imports: [ConfigModule],
useFactory: async (configService: ConfigService) =>
({
transport: {
host: configService.host,
port: configService.port,
secure: configService.secure,
auth: {
user: configService.username,
pass: configService.password,
},
},
defaults: {
from: 'Hello @Crowdlinker <hello@crowdlinker.com>',
},
} as NodemailerOptions<NodemailerDrivers.SMTP>),
inject: [ConfigService],
}),
],
})
import { Nodemailer } from '@crowdlinker/nestjs-mailer';
class MailService {
constructor(
private readonly nodemailer: Nodemailer<NodemailerDrivers.SMTP>
) {}
async mail(to, subject, text, html) {
this.nodemailer.sendMail({ to, subject, text, html });
}
}
You can use the following drivers
NodemailerDrivers.SES
- https://nodemailer.com/transports/ses/NodemailerDrivers.JSON
- https://nodemailer.com/transports/stream/NodemailerDrivers.SMTP
- https://nodemailer.com/smtp/NodemailerDrivers.SENDMAIL
- https://nodemailer.com/transports/sendmail/
- Code is written in Typescript (v3.9.10)