@overflo-srl/brevo-mailer
v1.2.1
Published
## Description
Downloads
73
Keywords
Readme
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: "[email protected]"
})],
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: "[email protected]"
}));
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, ["[email protected]"]);
await this.brevoMailerService.sendEmailWithCc(89, ["[email protected]"], ['[email protected]']);
// Sending mail with parameters
await this.brevoMailerService.sendEmail(89, ["[email protected]"], {something: "something"});
await this.brevoMailerService.sendEmailWithCc(89, ["[email protected]"],
["[email protected]"],
{something: "something"});
// Sending mail with attachment
await this.brevoMailerService.sendEmail(89,
["[email protected]"],
{something: "something"},
[{content: "base64", name: "name"}]
);
await this.brevoMailerService.sendEmailWithCc(89,
["[email protected]"],
["[email protected]"],
{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,
["[email protected]"],
["[email protected]"],
{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
- Author - Overflo srl
- Repo - Private gitlab
- Package - npm page
License
Nest is MIT licensed.