Skip to content

mobizerg/nest-sendgrid-mail

Repository files navigation

Nest Logo

A Sendgrid mail integration module for Nest.js framework.

Installation

Yarn

yarn add @mobizerg/nest-sendgrid-mail @sendgrid/mail

NPM

npm install @mobizerg/nest-sendgrid-mail @sendgrid/mail --save

Description

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

Usage

Import the SendgridMailModule in app.module.ts

import { Module } from '@nestjs/common';
import { SendgridMailModule } from '@mobizerg/nest-sendgrid-mail';

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

With Async

import { Module } from '@nestjs/common';
import { SendgridMailModule } from '@mobizerg/nest-sendgrid-mail';

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

Example config file (async)

import { Injectable } from '@nestjs/common';
import { ConfigService } from './config.service';
import { SendgridMailModuleOptions, SendgridMailOptionsFactory } from '@mobizerg/nest-sendgrid-mail';

@Injectable()
export class SendgridMailConfigService implements SendgridMailOptionsFactory {

  constructor(private readonly config: ConfigService) {}

  createSendgridMailOptions(name?: string): SendgridMailModuleOptions {
      
    return {
      name,
      apiKey: 'your-api-key',
      substitutionWrappers: {
        left: '{{',
        right: '}}',
      },
    };
  }
}

Importing inside services

import { Injectable } from '@nestjs/common';
import { SendgridMailService } from '@mobizerg/nest-sendgrid-mail';
import { ClientResponse } from "@sendgrid/client/src/response";

@Injectable()
export class MailService {
    
      constructor(private readonly mailer: SendgridMailService) {}
                  
      async send(): Promise<[ClientResponse, {}]> {
          return await this.mailer.send({
            to: "",
            from: "",
            subject: "",
            text: "",
            html: ""
          });
      }           
}

License

MIT

About

Sendgrid mail integration module for nestjs framework

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published