Email usage
Register EmailModule
app.module.ts
import { Module } from '@nestjs/common';
import { EmailModule, EmailProvider } from 'nestjs-notification-module';
@Module({
imports: [
EmailModule.register({
isGlobal: true,
provider: EmailProvider.SMTP,
defaultFromAddress: 'noreply@example.com',
host: 'smtp.example.com',
auth: { user: 'user', password: 'pass' },
}),
],
})
export class AppModule {}
Send an email
sendAsync parameters
EmailService.sendAsync(args) accepts the following fields:
| Field | Type | Required | Notes |
|---|---|---|---|
from | string | ❌ No | Overrides defaultFromAddress. |
to | string | string[] | ✅ Yes | Recipient email(s). |
subject | string | ✅ Yes | Email subject. |
content | string | ✅ Yes | Email body (provider-specific; commonly HTML). |
cc | string[] | ❌ No | CC recipients. |
bcc | string[] | ❌ No | BCC recipients. |
attachments | Attachment[] | ❌ No | Attachments to include in the email. |
Attachment shape:
| Field | Type | Required | Notes |
|---|---|---|---|
content | string | ✅ Yes | Attachment content (typically Base64 encoded, provider-specific). |
filename | string | ✅ Yes | File name. |
type | string | ✅ Yes | MIME type (e.g. "application/pdf"). |
encoding | string | ✅ Yes | Content encoding (provider-specific). |
disposition | string | ❌ No | e.g. "attachment" or "inline". |
mail.service.ts
import { Injectable } from '@nestjs/common';
import { EmailService } from 'nestjs-notification-module';
@Injectable()
export class MailService {
constructor(private readonly email: EmailService) {}
async sendWelcome(to: string) {
return this.email.sendAsync({
to,
subject: 'Welcome',
content: '<b>Hello</b>',
});
}
}
note
If you don't provide from in the args, the module uses defaultFromAddress.