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 🙏

© 2024 – Pkg Stats / Ryan Hefner

@nestpack/mail

v1.0.0-alpha.3

Published

Mail Module for NestJS projects

Downloads

1

Readme

NestPack Mail

A Mail Module for NestJs that allows for swappable Email Services.

Installation

$ npm install @nestpack/mail
# OR
$ yarn add @nestpack/mail

Usage

Import the MailModule and register on your root AppModule.

// app.module.ts
import { Module } from '@nestjs/common';
import { MailModule } from '@nestpack/mail';
import { AppController } from './app.controller';

@Module({
  imports: [MailModule.forRoot()],
  controllers: [AppController]
})
export class AppModule {}

Drivers are used to switch between email providers. By default, MemoryMailDriver is used. This driver does not send emails to a 3rd party provider, and, instead, holds your emails in memory.

To use a different driver, register it by passing it into the driver option.

forRoot() will import the MailModule globally, meaning you don't need to import everywhere, and everything shares the same configuration. forFeature() can also be used to use different configurations in different modules.

In this example, MemoryMailDriver is used, but a different 3rd party driver would be passed in. (If nothing is passed in MemoryMailDriver is used anyway.)

// app.module.ts
import { Module } from '@nestjs/common';
import { MailModule, MemoryMailDriver } from '@nestpack/mail';
import { AppController } from './app.controller';

@Module({
  imports: [MailModule.forRoot({
    driver: MemoryMailDriver
  })],
  controllers: [AppController]
})
export class AppModule {}

Next, create something that can be emailed by creating a new Mailable. This can be a class instance, or an object so long as it implements the IMailable interface.

// confirmation.mailable.ts
import { IMailable } from '@nestpack/mail';
import { User } from 'user/user.entity.ts';

export class ConfirmationMailable implements IMailable {
  constructor(public user: User){
    this.to = [user.email];
    this.from = '[email protected]'
    this.text = 'Hello world';
  }
}

Now that the mailable is created, inject the MailService somewhere in the app, and send the email.

import { Injectable } from '@nestjs/common';
import { MailService } from '@nestpack/mail';
import { User } from '../user/user.entity.ts';
import { ConfirmationMailable } from '../confirmation.mailable.ts'


@Injectable()
export class YourService {
  constructor(private readonly mailService: MailService){}

  async sendConfirmationEmail(user: User){
    await this.mailService.sendMail(new ConfirmationMailable(user));

  }
}

Usage with testing

By default, when NODE_ENV is test the MemoryMailDriver will be used. This means that within tests, emails aren't sent to the 3rd party services. In order to get test emails, the MemoryMailDriver needs to be accessed directly from the module system as show below.

    const module = await Test.createTestingModule({
      imports: [
        MailModule.forRoot(),
      ],
    }).compile();

    const mailService = module.get(MailService);
    const mailDriver = module.get(MemoryMailDriver);


    // Email is not sent, and is stored in-memory instead.
    await service.sendMail({ to: ['[email protected]'] });

    expect(mailDriver.getTestEmails()).toMatchObject([{ to: ['[email protected]'] }]);

    mailDriver.resetTestEmails();

    expect(mailDriver.getTestEmails()).toMatchObject([]);

Writing a Driver

A custom Mail Driver must be a class that implements the IMailDriver interface. The driver is dependency injected, so the class has access to the Nest module system, including the options passed into MailModule.forRoot().

import { Inject, Injectable } from '@nestjs/common';
import { IMailable, IMailDriver, IMailModuleOptions } from '@nestpack/mail';

@Injectable()
export class CustomMailDriver implements IMailDriver {
  constructor(@Inject('MAIL_OPTIONS') private options: IMailModuleOptions) {}

  async sendMail(mailable: IMailable) {
    // Global 3rd party mailer options.
    this.options.driverOptions;

    // Mailable specific 3rd party mailer options.
    mailable.options;

    // Use the options above to set up your custom driver and send an email here.
  }
}