npm package: @overflo-srl/brevo-mailer
Description: ## Description
Latest version: 1.2.1


# Brevo mailer

## Description

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

```bash
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:

```typescript
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
```typescript
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
```typescript
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

```typescript
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

```typescript
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

```typescript
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](https://www.overflo.srl/)
- Repo - [Private gitlab](https://gitlab.com/overflo1/brevo-mailer)
- Package - [npm page](https://www.npmjs.com/package/@overflo-srl/brevo-mailer)


## License

Nest is [MIT licensed](LICENSE).