Skip to content

Older version

You're reading the docs for v2 (2.3.x). The latest version is v4.

Read the v4 guide · Migrate from v2 to v3 · Migrate from v3 to v4 · Versions & upgrading

Event System ​

React to email lifecycle events using @nestjs/event-emitter.

Setup ​

Install the event emitter package:

bash
pnpm add @nestjs/event-emitter

Register it in your app module:

typescript
import { EventEmitterModule } from '@nestjs/event-emitter';

@Module({
  imports: [
    EventEmitterModule.forRoot(),
    MailerModule.forRoot({ ... }),
  ],
})
export class AppModule {}

Available Events ​

EventDescription
mailer.before_sendEmitted before an email is sent
mailer.after_sendEmitted after successful send
mailer.send_errorEmitted when send fails
mailer.queuedEmitted when email is added to queue
mailer.queue.completedEmitted when queued email is sent
mailer.queue.failedEmitted when queued email fails

Listening to Events ​

Use the @OnEvent() decorator:

typescript
import { OnEvent } from '@nestjs/event-emitter';
import { MailerEvent, MailerEventPayload } from '@nestjs-modules/mailer';

@Injectable()
export class EmailEventListener {
  @OnEvent(MailerEvent.AFTER_SEND)
  handleEmailSent(payload: MailerEventPayload) {
    console.log('Email sent to:', payload.mailOptions.to);
    console.log('Message ID:', payload.result?.messageId);
  }

  @OnEvent(MailerEvent.SEND_ERROR)
  handleEmailError(payload: MailerEventPayload) {
    console.error('Email failed:', payload.error?.message);
    // Log to monitoring, retry, etc.
  }
}

Use Cases ​

Logging ​

typescript
@OnEvent(MailerEvent.AFTER_SEND)
async logEmail(payload: MailerEventPayload) {
  await this.emailLogService.create({
    to: payload.mailOptions.to,
    subject: payload.mailOptions.subject,
    template: payload.mailOptions.template,
    sentAt: payload.timestamp,
    messageId: payload.result?.messageId,
  });
}

Metrics ​

typescript
@OnEvent(MailerEvent.AFTER_SEND)
trackSuccess() {
  this.metricsService.increment('emails.sent');
}

@OnEvent(MailerEvent.SEND_ERROR)
trackFailure() {
  this.metricsService.increment('emails.failed');
}

INFO

The event system is optional. If @nestjs/event-emitter is not installed, events are silently skipped with zero overhead.

Released under the MIT License.