2.2.1 • Published 2 years ago

@enricoemiro/mailer v2.2.1

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

Mailer

This is a simple NestJS mailer module based on NodeMailer.

Table of Contents

Why should you install this package?

This package is meant to be a wrapper of NodeMailer (nothing less, nothing more). If your goal is to be able to use NodeMailer within NestJS then this package is for you.

IMPORTANT: This package will never add native support for template engines (such as pug, handlebars, etc...).

Installation

Installation is as simple as running:

npm install @enricoemiro/mailer nodemailer
npm install --save-dev @types/nodemailer
# OR
yarn add @enricoemiro/mailer nodemailer
yarn add -D @types/nodemailer

Usage

A basic usage example:

  1. Register the module as a dependency (app.module.ts)

Using forRoot()

import { MailerModule } from '@enricoemiro/mailer';

MailerModule.forRoot({
  transports: [
    {
      name: 'mailtrap',
      transport: {
        host: 'smtp.mailtrap.io',
        // ... transport settings
      },
    },
  ],
  isGlobal: true,
});

Using forRootAsync()

  • Using useFactory
import { MailerModule } from '@enricoemiro/mailer';

MailerModule.forRootAsync({
  imports: [ConfigModule],
  useFactory: async function (configService: ConfigService) {
    return {
      transports: [
        {
          name: 'mailtrap',
          transport: {
            host: configService.get<string>('MAIL_HOST'),
            // ... transport settings
          },
        },
      ];
    };
  },
  inject: [ConfigService],
  isGlobal: true,
});
  • Using useClass or useExisting
@Injectable()
export class MailerConfigService implements MailerTransportFactory {
  createMailerModuleOptions(): Promise<MailerModuleOptions> | MailerModuleOptions {
    return {
      transports: [
        {
          name: 'smtp',
          transport: {
            // ... transport settings
          },
        },
      ];
    };
  }
}
import { MailerModule } from '@enricoemiro/mailer';

import { MailerConfigService } from './mailerConfigServicePath.ts';

MailerModule.forRootAsync({
  useClass: MailerConfigService,
  isGlobal: true,
});
  1. Inject the MailerService as a dependency:
import { MailerService } from '@enricoemiro/mailer';

@Injectable()
export class YourService {
  constructor(private mailerService: MailerService) {}

  async sendHelloWorldEmail() {
    this.mailerService.sendAsyncMail('mailtrap', {
      from: '"Ghost Foo 👻" <foo@example.com>',
      to: 'bar@example.com, baz@example.com',
      subject: 'Hello ✔',
      text: 'Hello world?',
      html: '<b>Hello world?</b>',
    });
  }
}

API Methods

The MailerModule can be instantied synchronously or asynchronously using respectively:

  • forRoot(options: MailerModuleOptions)
  • forRootAsync(options: MailerModuleAsyncOptions)

The MailerService exposes the following two methods:

  • sendAsyncEmail(name: string, mailOptions: Mail.Options): Promise<SentMessageInfo>
  • getTransporter(name: string): Transporter

Changelog

All changelog are available here.

License

MIT

2.2.1

2 years ago

2.2.0

2 years ago

1.3.4

2 years ago

1.3.3

2 years ago

1.3.2

2 years ago

2.0.0

2 years ago

1.3.1

2 years ago

1.3.0

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago