@nestdevx/mailer
v1.0.1
Published
Mailer module for multi-tenant NestJS applications.
Downloads
6
Maintainers
Readme
@nestdevx/mailer
Mailer module for multi-tenant NestJS applications.
Features
- Multi-tenant email sending support
- Pluggable mail providers (Mailersend & Nodemailer)
- Email templates and variables
Installation
npm install @nestdevx/mailer
# or
yarn add @nestdevx/mailer
# or
pnpm add @nestdevx/mailerUsage
1. Import the Module
import { MailerModule } from '@nestdevx/mailer';
@Module({
imports: [
// Use simple SMTP email
MailerModule.forRootAsync({
useFactory: async (configService: ConfigService) => ({
type: 'smtp',
host: configService.getOrThrow('SMTP_HOST'),
port: configService.getOrThrow('SMTP_PORT'),
secure: configService.getOrThrow('SMTP_SECURE') === 'true',
auth: {
user: configService.getOrThrow('SMTP_USER'),
pass: configService.getOrThrow('SMTP_PASSWORD'),
},
}),
inject: [ConfigService],
}),
// Or use mailersend
MailerModule.forRootAsync({
useFactory: async (configService: ConfigService) => ({
type: 'mailersend',
apiKey: configService.getOrThrow('MAILER_SEND_API_KEY')
}),
inject: [ConfigService],
}),
],
})
export class AppModule {}2. Inject and Use the MailerService
import { MailerService } from '@nestdevx/mailer';
@Injectable()
export class UserService {
constructor(private readonly mailer: MailerService) {}
async sendWelcomeEmail(user: User) {
await this.mailer.sendMail({
to: user.email,
subject: 'Welcome!',
template: 'welcome', // template name
context: { name: user.name }, // template variables
});
}
}3. Email Templates
Place your templates in a directory (e.g., templates/). The module will load and render them using the provided context.
4. Multi-Tenancy
- The module supports per-tenant configuration. You can provide tenant-specific mailer settings via a service or database.
- Use the
tenantIdproperty insendMailto route emails through the correct provider/config.
5. Queue Integration
- If
queue: trueis set, emails are sent via BullMQ jobs for reliability and scalability. - Requires
@nestjs/bullmqand a running Redis instance.
API Reference
MailerModule
forRoot(options: MailerModuleOptions)– Synchronous global registrationforRootAsync(options: MailerModuleAsyncOptions)– Async registration (e.g., with ConfigService)
MailerService
sendMail(options: SendMailOptions): Promise<any>– Send an email (supports templates, variables, attachments, etc.)
Example: Async Registration with ConfigService
import { MailerModule } from '@nestdevx/mailer';
import { ConfigModule, ConfigService } from '@nestjs/config';
@Module({
imports: [
ConfigModule.forRoot(),
MailerModule.forRootAsync({
imports: [ConfigModule],
useFactory: async (config: ConfigService) => ({
defaultProvider: 'nodemailer',
nodemailer: {
host: config.get('SMTP_HOST'),
port: config.get('SMTP_PORT'),
auth: {
user: config.get('SMTP_USER'),
pass: config.get('SMTP_PASS'),
},
},
queue: true,
}),
inject: [ConfigService],
}),
],
})
export class AppModule {}License
MIT
