@kasifraza/nestjs-mailer
v1.0.0
Published
Email service for NestJS with template support (Handlebars), multiple transports
Downloads
71
Maintainers
Readme
@kasifraza/nestjs-mailer
Email service for NestJS with Handlebars template support and multiple transports.
Installation
npm install @kasifraza/nestjs-mailer nodemailer
npm install -D @types/nodemailerUsage
Register the module
import { MailerModule } from '@kasifraza/nestjs-mailer';
@Module({
imports: [
MailerModule.register({
transport: {
host: 'smtp.example.com',
port: 587,
secure: false,
auth: { user: '[email protected]', pass: 'password' },
},
defaults: { from: '"No Reply" <[email protected]>' },
templateDir: path.join(__dirname, 'templates'),
}),
],
})
export class AppModule {}Async registration
MailerModule.registerAsync({
useFactory: (config: ConfigService) => ({
transport: {
host: config.get('SMTP_HOST'),
port: config.get('SMTP_PORT'),
auth: { user: config.get('SMTP_USER'), pass: config.get('SMTP_PASS') },
},
}),
inject: [ConfigService],
});Send emails
import { MailerService } from '@kasifraza/nestjs-mailer';
@Injectable()
export class NotificationService {
constructor(private mailer: MailerService) {}
async sendWelcome(email: string, name: string) {
await this.mailer.sendMail({
to: email,
subject: 'Welcome!',
template: 'welcome',
context: { name },
});
}
}Template example
Create templates/welcome.hbs:
<h1>Welcome, {{name}}!</h1>
<p>Thanks for signing up.</p>API
MailerService
sendMail(options: SendMailOptions)— Send an email (plain text, HTML, or template)verifyConnection()— Verify SMTP connection
License
MIT
