Skip to main content

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:

FieldTypeRequiredNotes
fromstring❌ NoOverrides defaultFromAddress.
tostring | string[]✅ YesRecipient email(s).
subjectstring✅ YesEmail subject.
contentstring✅ YesEmail body (provider-specific; commonly HTML).
ccstring[]❌ NoCC recipients.
bccstring[]❌ NoBCC recipients.
attachmentsAttachment[]❌ NoAttachments to include in the email.

Attachment shape:

FieldTypeRequiredNotes
contentstring✅ YesAttachment content (typically Base64 encoded, provider-specific).
filenamestring✅ YesFile name.
typestring✅ YesMIME type (e.g. "application/pdf").
encodingstring✅ YesContent encoding (provider-specific).
dispositionstring❌ Noe.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.