1.2.1 • Published 8 months ago

@overflo-srl/brevo-mailer v1.2.1

Weekly downloads
-
License
-
Repository
-
Last release
8 months ago

Brevo mailer

Description

Simple mailer used for sending emails with Brevo (former SendInBlue)

Installation

npm install @overflo-srl/brevo-mailer

Setup into the project

In order to use the service, we need to import the module and inject the desired configuration:

import {BrevoMailerModule} from "@overflo-srl/brevo-mailer";

@Module({
    imports: [
        BrevoMailerModule.register({
            isProd: false,
            devEmail: 'mail of the developer',
            key: "key of the brevo account",
            senderName: "Overflo",
            senderEmail: "prova.srl@overflo.srl"
    })],
   providers: [AppService],
})
export class AppModule {
}

Another example using @nestjs/config:

app.module.ts

import {BrevoMailerModule} from "@overflo-srl/brevo-mailer";

@Module({
    imports: [
        BrevoMailerModule.registerAsync({
            imports: [ConfigModule],
            inject: [ConfigService],
            useFactory: async (configService: ConfigService) => (
                configService.get('email')
            ),
        }),    
    ],
   providers: [AppService],
})
export class AppModule {
}

email.config.ts

import { registerAs } from '@nestjs/config';

export default registerAs('email', () => ({
    isProd: false,
    devEmail: 'mail of the developer',
    key: "key of the brevo account",
    senderName: "Overflo",
    senderEmail: "prova.srl@overflo.srl"
}));

Now we can import the service where we need

import { Injectable } from '@nestjs/common';
import {BrevoMailerService} from "@overflo-srl/brevo-mailer";
import {ProjectEmitter} from "../../../app.event";

export class AppService {

    constructor(
        private readonly brevoMailerService: BrevoMailerService,
        @InjectEventEmitter() private readonly emitter: ProjectEmitter, // Mandatory only for the 'sendEmailWithEvent' method
    ) {
    }
    
    ...
}

Examples of usage

Sending mails

import { Injectable } from '@nestjs/common';
import {BrevoMailerService} from "@overflo-srl/brevo-mailer";
import {ProjectEmitter} from "../../../app.event";


@Injectable()
export class AppService {

  constructor(
      private readonly brevoMailerService: BrevoMailerService,
      @InjectEventEmitter() private readonly emitter: ProjectEmitter, // Mandatory only for the 'sendEmailWithEvent' method
  ){}
  
  async getHello(): Promise<boolean> {
    // Sending normal mail
    await this.brevoMailerService.sendEmail(89, ["mail1@overflo.srl"]);
    await this.brevoMailerService.sendEmailWithCc(89, ["mail1@overflo.srl"], ['mail2@overflo.srl']);

    // Sending mail with parameters
    await this.brevoMailerService.sendEmail(89, ["mail1@overflo.srl"], {something: "something"});
    await this.brevoMailerService.sendEmailWithCc(89, ["mail1@overflo.srl"], 
        ["mail2@overflo.srl"],
        {something: "something"});

    // Sending mail with attachment
    await this.brevoMailerService.sendEmail(89, 
        ["mail1@overflo.srl"],
        {something: "something"},
        [{content: "base64", name: "name"}]
    );
    await this.brevoMailerService.sendEmailWithCc(89, 
        ["mail1@overflo.srl"], 
        ["mail2@overflo.srl"], 
        {something: "something"},
        [{content: "base64", name: "name"}]
    );
    
    // Sending mail with internal event to call
    const eventModel = {emitter: this.emitter, eventName: "mailXYZSent", payload: {something: "something"}};
    await this.brevoMailerService.sendEmailWithEvent(
        89,
        ["mail1@overflo.srl"],
        ["mail2@overflo.srl"],
        {something: "something"},
        [{content: "base64", name: "name"}], 
        eventModel
    );
  }
}

Get mail template

If you need to download the template from Brevo and then send the mail with another service you can use this API

import { Injectable } from '@nestjs/common';
import {BrevoMailerService} from "@overflo-srl/brevo-mailer";


@Injectable()
export class AppService {

  constructor(
      private readonly brevoMailerService: BrevoMailerService,
){}
  
  async getHello(): Promise<boolean> {
      // Getting Brevo mail template
      await this.brevoMailerService.getTemplateById(89);
  }
  
}

Stay in touch

License

Nest is MIT licensed.