Streamlining Email Communication in Custom CRM with Brevo Integration using NestJS, PostgreSQL, and MikroORM

Streamlining Email Communication in Custom CRM with Brevo Integration using NestJS, PostgreSQL, and MikroORM

· 4 min read

In today's fast-paced business environment, effective communication is crucial. Integrating email functionalities into your custom CRM can significantly enhance communication with your users and prospects. In this guide, we'll walk through the process of integrating Brevo's email services into a NestJS application using PostgreSQL and MikroORM, enabling you to send emails and track their statuses effectively.

Why Choose Brevo?

Brevo (formerly Sendinblue) offers a robust platform for email marketing and transactional email services. Their API is well-documented and provides powerful features such as real-time tracking, webhooks, and detailed analytics, making it an excellent choice for integrating email functionalities into your CRM. They also provide 300 emails/day for free and that’s enough to get started.

Prerequisites

  • Basic understanding of NestJS, PostgreSQL, and MikroORM.
  • Brevo account with API key (available in your Brevo account settings).

Step 1: Setting Up Brevo API Credentials

First, create an account on Brevo and obtain your API key.

  1. Sign Up and Login: If you don’t already have an account, sign up at Brevo.
  2. Generate API Key:
    • Go to your account settings.
    • Navigate to the "SMTP & API" section.
    • Click on "Create a New API Key".

Step 2: Setting Up Your NestJS Project

Ensure you have a NestJS project set up with MikroORM and PostgreSQL configured. If not, you can create a new NestJS project and add MikroORM as follows:

nest new my-crm
cd my-crm
npm install @mikro-orm/core @mikro-orm/nestjs @mikro-orm/postgresql

Step 3: Creating Entities

EmailBatch and EmailInstance Entities

To keep track of emails sent and their statuses, we'll create two entities: EmailBatch and EmailInstance.

  • EmailBatch represents a batch of emails sent with the same subject and content.
  • EmailInstance represents each individual email sent, which belongs to a particular EmailBatch.
import { Entity, PrimaryKey, Property, OneToMany, ManyToOne } from '@mikro-orm/core';
@Entity()
export class EmailBatch {
  @PrimaryKey()
  id!: number;
  @Property()
  subject!: string;
  @Property()
  htmlContent!: string;
  @OneToMany(() => EmailInstance, emailInstance => emailInstance.emailBatch)
  emailInstances = new Collection<EmailInstance>(this);
}
@Entity()
export class EmailInstance {
  @PrimaryKey()
  id!: number;
  @ManyToOne(() => EmailBatch)
  emailBatch!: EmailBatch;
  @Property()
  messageId!: string;
}

Prospect Entity

The Prospect entity represents a potential customer or lead to whom emails are sent.

import { Entity, PrimaryKey, Property } from '@mikro-orm/core';
@Entity()
export class Prospect {
  @PrimaryKey()
  id!: string;
  @Property()
  emailAddress!: string;
  @Property()
  firstName!: string;
  @Property()
  lastName!: string;
}

Step 4: Sending Emails

Using Brevo's API, you can send emails from your NestJS service. Here’s how to send an email to one recipient:

Send Email to One

import { Injectable } from '@nestjs/common';
import axios from 'axios';
@Injectable()
export class EmailService {
  async sendEmailToOne(prospectEmail: string, subject: string, htmlContent: string) {
    const data = {
      sender: { name: 'Your Name', email: 'your-email@example.com' },
      to: [{ email: prospectEmail }],
      subject,
      htmlContent,
    };

    const response = await axios.post('https://api.sendinblue.com/v3/smtp/email', data, {
      headers: {
        'api-key': process.env.BREVO_API_KEY,
        accept: 'application/json',
        'Content-Type': 'application/json',
      },
    });
    return response.data;
  }
}

Step 5: Setting Up Webhooks

To track email events like delivery, opens, and clicks, set up webhooks in your Brevo account:


1. Create a Webhook:

  • Navigate to "SMTP & API" > "Webhooks".
  • Enter the URL of your webhook endpoint (e.g., https://your-crm.com/webhook/brevo).

2. Webhook Endpoint in NestJS: Create a controller to handle webhook events:

import { Controller, Post, Body } from '@nestjs/common';
@Controller('webhook')
export class WebhookController {
  @Post('brevo')
  handleBrevoEvent(@Body() body: any) {
    // Process the webhook data
    console.log('Webhook event received:', body);
  }
}

Step 6: Storing Email Logs and Updating Activity Table

To keep track of email events, store them in your database. Here’s a simplified approach:

Activity Entity

Create an Activity entity to store different types of activities, including email events.

import { Entity, PrimaryKey, Property, ManyToOne } from '@mikro-orm/core';
@Entity()
export class Activity {
  @PrimaryKey()
  id!: number;
  @ManyToOne(() => Prospect)
  prospect!: Prospect;
  @Property()
  type!: string;
  @Property()
  timestamp!: Date;
  @Property()
  details!: string;
}

Logging Email Events

Implement a method to log email events in your EmailService:

import { EntityManager } from '@mikro-orm/core';
@Injectable()
export class EmailService {
  constructor(private em: EntityManager) {}
  async logEmailEvent(eventData: any) {
    const emailInstance = await this.em.findOne(EmailInstance, { messageId: eventData.messageId });
    if (!emailInstance) return;

    const activity = new Activity({
      prospect: emailInstance.prospect,
      type: eventData.event,
      timestamp: new Date(eventData.timestamp),
      details: JSON.stringify(eventData),
    });
    this.em.persist(activity);
    await this.em.flush();
  }
}

Example Workflow

1. Send Email: Use the sendEmailToMany method to send emails to multiple prospects.

2. Receive Webhook Event: Brevo sends a webhook event to your webhook endpoint.

3. Log Event: The webhook controller processes the event and calls the logEmailEvent method to log the event in the database.

Conclusion

Integrating Brevo's email services into your custom CRM with NestJS, PostgreSQL, and MikroORM can significantly enhance your communication capabilities. By following the steps outlined above, you can set up a robust email integration that not only sends emails but also tracks their journey from sent to clicked, providing a comprehensive view of your email interactions. Happy emailing!

For further details, you can refer to the Brevo API Documentation.

https://developers.brevo.com/docs/getting-started

Sudhanshu Kumar

About Sudhanshu Kumar

Meet Sudhanshu Kumar, a final-year student at IIT Delhi and a Software Development Intern at CyberMind Works. With a passion for technology, Sudhanshu has cultivated expertise in Data Science, Web Development, and more. His journey includes leading projects in machine learning and cybersecurity, reflecting his commitment to innovation and impactful solutions. Sudhanshu is dedicated to pushing the boundaries of technology while bringing creativity and expertise to every endeavor.

Link copied
Copyright © 2024 CyberMind Works. All rights reserved.