nestjs-mailer
TypeScript icon, indicating that this package has built-in type declarations

1.0.1 • Public • Published

NestJS Mailer

NPM Version Package License

Table of Contents

Description

Integrates Nodemailer with Nest

Installation

npm install nestjs-mailer nodemailer handlebars

Hint: handlebars is an optional dependency, if you want to use the template helper, you must install it.

npm install -D @types/nodemailer

You can also use the interactive CLI

npx nestjs-modules

Examples

MailerModule.forRoot(options, connection?)

import { Module } from '@nestjs/common';
import { MailerModule } from 'nestjs-mailer';
import { AppController } from './app.controller';
 
@Module({
  imports: [
    MailerModule.forRoot({
      config: {
        transport: 'smtp://login:password@smtp.mailtrap.io',
        // transport: {
        //   host: 'smtp.mailtrap.io',
        //   port: 587,
        //   secure: false,
        //   auth: {
        //     user: 'login',
        //     pass: 'password',
        //   }
        // },
        // defaults: {
        //   from: '"Sviatoslav" <sviatoslav@example.com>',
        // },
      },
    }),
  ],
  controllers: [AppController],
})
export class AppModule {}

MailerModule.forRootAsync(options, connection?)

import { Module } from '@nestjs/common';
import { MailerModule } from 'nestjs-mailer';
import { AppController } from './app.controller';
 
@Module({
  imports: [
    MailerModule.forRootAsync({
      useFactory: () => ({
        config: {
          transport: 'smtp://login:password@smtp.mailtrap.io',
          // transport: {
          //   host: 'smtp.mailtrap.io',
          //   port: 587,
          //   secure: false,
          //   auth: {
          //     user: 'login',
          //     pass: 'password',
          //   }
          // },
          // defaults: {
          //   from: '"Sviatoslav" <sviatoslav@example.com>',
          // },
        },
      }),
    }),
  ],
  controllers: [AppController],
})
export class AppModule {}

InjectMailer(connection?)

import { Controller, Get, } from '@nestjs/common';
import { InjectMailer, Mailer, template } from 'nestjs-mailer';
 
@Controller()
export class AppController {
  constructor(
    @InjectMailer() private readonly mailer: Mailer,
  ) {}
 
  @Get()
  async getHello() {
    this.mailer.sendMail({
      from: '"Sviatoslav" <sviatoslav@example.com>',
      to: 'john@example.com',
      subject: 'Hello ✔',
      text: 'Hello John',
      html: template('src/mailer/hello.hbs', { name: 'John' })
    }).catch(e => console.log(e));
  }
}

License

MIT

Package Sidebar

Install

npm i nestjs-mailer

Weekly Downloads

149

Version

1.0.1

License

MIT

Unpacked Size

16.2 kB

Total Files

20

Last publish

Collaborators

  • svtslv