npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

nest-mailer-module

v1.0.11

Published

NestJS Discovery Module

Downloads

31

Readme

Features

  • Provide a way to send emails
  • Provide easy to use options by default

Installation

$ npm i --save nest-mailer

Example Usage with sendgrid

Create mailer module in you application:

import { Global, Module } from '@nestjs/common';
import { MailerModule, SendgridMailer } from 'nest-mailer-module';
import { MailerEventEmitter } from './mailer.emitter';

@Global()
@Module({
  imports: [MailerModule.forRoot({ mailer: new SendgridMailer(/* sendgrid api key*/) })],
  providers: [MailerEventEmitter],
  exports: [MailerEventEmitter],
})
export class AppMailerModule {}

Then create our MailerEventEmitter

import { Injectable } from '@nestjs/common';
import { EventEmitter2 } from '@nestjs/event-emitter';
import { SendEmailEvent, SEND_EMAIL_EVENT } from 'nest-mailer-module';

@Injectable()
export class MailerEventEmitter {
  constructor(private eventEmitter: EventEmitter2) {}

  emit(event: SendEmailEvent) {
    this.eventEmitter.emit(SEND_EMAIL_EVENT, new SendEmailEvent({
      from: '[email protected]',
      ...event.message,
    }));
  }
}

After that we should import our newly created module in app.module and we can inject it in any service since it was configured as global module with private mailerEventEmitter: MailerEventEmitter.

Now we can send plain emails with:

this.mailerEventEmitter.emit({
  subject: 'Reset Password',
  to: [{ email: '[email protected]', type: 'to' }],
  text: `Reset password`,
});

Adding own mailer client

We can write our custom mailer by implementing interface Mailer and using it in mailer.module as mailer property for configuration. Here we have an example of mandrill client:

import { Logger } from '@nestjs/common';
import { Mandrill } from 'mandrill-api';

import { Mailer, Message } from 'nest-mailer-module';

export class MandrillMailer implements Mailer {
  private logger = new Logger(MandrillMailer.name);
  mandrillClient: any;

  constructor(mandrillApiKey: string) {
    this.mandrillClient = new Mandrill(mandrillApiKey);
    this.mandrillClient.users.ping(
      {},
      result => this.logger.log('ping "mandrill" success'),
      e => this.logger.error('ping "mandrill" failed'),
    );
  }

  send(message: Message) {
    const loggerContext = { subject: message.subject, to: message.to };
    this.logger.log(loggerContext, 'Sending email');
    return this.mandrillClient.messages.send(
      {
        message: {
          auto_text: true,
          from_email: message.from?.email,
          from_name: message.from?.name,
          html: message.html,
          important: true,
          subject: message.subject,
          to: message.to,
          text: message.text,
        },
        async: true,
      },
      () => {
        this.logger.log(loggerContext, `Mail sent`);
      },
      error => {
        this.logger.error(error, error.message);
      },
    );
  }
}

Example usage with react templated emails

Let's start with creating mailer module in you application like in first example

Now we can create ReactMailerEventEmitter

import { Injectable } from '@nestjs/common';
import { EventEmitter2 } from '@nestjs/event-emitter';
import { SendTemplatedEmailEvent, SEND_TEMPLATED_EMAIL_EVENT } from 'nest-mailer-module';

@Injectable()
export class ReactMailerEventEmitter {
  constructor(private eventEmitter: EventEmitter2) {}

  emit(event: SendTemplatedEmailEvent) {
    this.eventEmitter.emit(SEND_TEMPLATED_EMAIL_EVENT,
      new SendTemplatedEmailEvent(event.template, {
        from: '[email protected]',
        ...event.message,
      }, event.mergeVars),
    );
  }
}