Sending Emails with NestJS and Facteur.dev

In this blog post, we will be exploring how to send emails in the NestJS framework using the Facteur Node.js SDK. NestJS, as you may already know, is a server-side framework for building efficient, reliable, and scalable Node.js server-side applications.

Step 1: Prerequisites

Before you can send emails using the Facteur Node.js SDK, there are a few things you need:

  • A Facteur account

  • An API key from Facteur

  • A verified sender domain

Step 2: Setting up Environment Variables using @nestjs/config

One of the best practices when working with credentials is to never hard-code them directly into your application. Instead, we should use environment variables. Environment variables allow us to hide sensitive information and keep our configuration separate from our code.

To set up environment variables in NestJS, we can use the @nestjs/config package.

First, install the package:

npm install @nestjs/config

Next, create a .env file in your root directory and set your API key:

FACTEUR_API_KEY=your-api-key

Then, import the ConfigModule in your app.module.ts file:

import { Module } from "@nestjs/common";
import { ConfigModule } from "@nestjs/config";
import { FacteurService } from "./facteur.service";

@Module({
  imports: [ConfigModule.forRoot()],
  providers: [FacteurService],
})
export class AppModule {}

Step 3: Installing Facteur Node.js SDK

Install the Facteur Node.js SDK to our NestJS project with the following command:

npm install facteur-node

Step 4: Setting Up Facteur In NestJS

Next, we'll create a FacteurService to encapsulate our email sending functionality:

nest generate service facteur

This generates a service file named facteur.service.ts. Open this file and make it look like this:

import { Injectable } from "@nestjs/common";
import { ConfigService } from "@nestjs/config";
import Facteur, { FacteurSendEmailPayload } from "facteur-node";

@Injectable()
export class FacteurService {
  private facteur: Facteur;

  constructor(private configService: ConfigService) {
    this.facteur = new Facteur(this.configService.get("FACTEUR_API_KEY"));
  }

  async sendEmail(emailData: FacteurSendEmailPayload) {
    await this.facteur.sendEmail(emailData);
  }
}

Here, we inject the ConfigService to read the API key from our environment variable, then we use this key to initialize our Facteur instance.

Step 5: Using the FacteurService

Now, let's create a more practical endpoint to showcase our FacteurService in action. We'll create a POST /signup route that accepts user information, and after signup, sends a confirmation email:

import { Controller, Post, Body } from "@nestjs/common";
import { FacteurService } from "./facteur.service";

@Controller()
export class AppController {
  constructor(private readonly facteurService: FacteurService) {}

  @Post("signup")
  async signUp(@Body() userDto) {
    // Sign up the user
    // ...

    // Send a confirmation email
    await this.facteurService.sendEmail({
      from: "no-reply@example.com",
      to: userDto.email,
      subject: "Welcome to Our Service!",
      text: `Hello ${userDto.name}, Welcome to our service...`,
      html: `<p>Hello <b>${userDto.name}</b>, Welcome to our service...</p>`,
    });

    return "User created and email sent!";
  }
}

In the above controller, we accept user data in the request body. After signing up the user (business logic not included for brevity), we send a confirmation email using our FacteurService.

And there you have it! You've successfully set up email sending in your NestJS application using the Facteur Node.js SDK. The usage of environment variables increases the security and scalability of your application. Happy emailing!