Skip to content

mobizerg/nest-nodemailer

Repository files navigation

Nest Logo

A Nodemailer integration module for Nest.js framework.

Installation

Yarn

yarn add @mobizerg/nest-nodemailer nodemailer
yarn add @types/nodemailer --dev

NPM

npm install @mobizerg/nest-nodemailer nodemailer --save
npm install @types/nodemailer --save-dev

Description

Mail integration module for Nest.js based on the Nodemailer package.

Usage

Import the NodemailerModule in app.module.ts

import { Module } from '@nestjs/common';
import { NodemailerModule } from '@mobizerg/nest-nodemailer';

@Module({
    imports: [
        NodemailerModule.register(options);
    ],
})
export class AppModule {}

With Async

import { Module } from '@nestjs/common';
import { NodemailerModule } from '@mobizerg/nest-nodemailer';

@Module({
    imports: [
        NodemailerModule.registerAsync({
            imports: [ConfigModule],
            useExisting: NodemailerConfigService,
        }),
    ],
})
export class AppModule {}

Example config file (async)

import { Injectable } from '@nestjs/common';
import { ConfigService } from './config.service';
import { NodemailerModuleOptions, NodemailerOptionsFactory } from '@mobizerg/nest-nodemailer';

@Injectable()
export class NodemailerConfigService implements NodemailerOptionsFactory {

  constructor(private readonly config: ConfigService) {}

  createNodemailerOptions(name?: string): NodemailerModuleOptions {
      
    return {
      name,
      transport: {
        host: this.config.mailHost,
        port: this.config.mailPort,
        secure: this.config.mailIsSecure,
        auth: {
          user: this.config.mailUsername,
          pass: this.config.mailPassword,
        },
        pool: true,
      },
      defaults: {
        pool: true,
        maxConnections: 2,
        from: `${this.config.mailFromName} <${this.config.mailFromAddress}>`,
      },
    };
  }
}

Importing inside services

import { Injectable } from '@nestjs/common';
import { InjectTransport } from '@mobizerg/nest-nodemailer';
import { SentMessageInfo } from 'nodemailer';
import * as Mail from 'nodemailer/lib/mailer';

@Injectable()
export class MailService {
    
      constructor(@InjectTransport()
                  private readonly mailTransport: Mail) {}
                  
      async send(): Promise<SentMessageInfo> {
          return await this.mailTransport.sendMail(options);
      }           
}

License

MIT

About

Nodemailer integration module for nestjs framework

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published