8min

Level Up Your NestJS Apps: Email Functionality with Mailgun & EJS

Level Up Your NestJS Apps: Email Functionality with Mailgun & EJS (placeholder)Level Up Your NestJS Apps: Email Functionality with Mailgun & EJS

Effective email communication is a fundamental requirement for virtually any application. In this course, we’ll delve into the art of implementing essential email sending functionality within NestJS, a robust Node.js framework. We’ll harness the power of Mailgun, a reliable third-party email service, while taking advantage of the versatile EJS templating language for crafting visually appealing emails enriched with compelling content.

The complete working code is available in the mailgun-nestjs Github repository.

Prerequisites

Before we dive into the implementation, you need to set up a NestJS project and configure the Mailgun API credentials. Make sure you have the following dependencies installed:

1. NestJS project setup

let’s start by installing the nest cli then creating our project

installing @nestjs/cli

create a new project using @nestjs/cli

2. Installing our required dependencies

we need the config module which will help us store our mailgun api crendentials in a secure place (the process environnement variables) , nest config will basically load the envs into our app

this is the mailgun sdk aka a bunch of modules to help us to communicate with the mailgun api.

Let’s start coding

1. Email Module components

We’ll begin by wrapping our email interaction logic in a separate module to better separate concerns. Run the following commands to generate an email module and an email service:

Make sure to provide and export the EmailService within the module to allow injection into other parts of the application:

Next, import the email module into your app module to enable injection of its exported services in the app module context:

2. Setup mailgun account and and load our credentials safly

To get started with Mailgun website, sign up for a free account on the Mailgun website. After that, create a .env file and store your Mailgun credentials securely: then create a .env file and put your mailgun credentials there:

To read the .env file and inject its content as environment variables in NestJS, use the config module. Set the isGlobal attribute to true to make the module available globally for injection:

3. Injecting our mailgun client

Now, let’s write the main logic for sending emails. In the following code, we initialize the Mailgun package with our credentials. To access these credentials, we inject the configService from the ConfigModule:

initializing our mailgun sdk , formData is needed by the mailgun client to wrap the payload which it will be sending under the ground as its just a wrapper around an http clients doing api calls to the mailgun rest api

4. creating our email template

Now, let’s create our HTML email template . If you’re not familiar with the EJS templating engine, <%=var1%> means to render the content of the JavaScript variable named var as a string in that specific place.

save the file in your project route directory at ./templates/email-template.ejs Let’s start implement our email service

5. Email sending logic

Let’s start implementing our email sending method

  1. First we are reading our template file as a bare utf8 encoded string
  2. Next we are using ejs to render our html from our ejs template file and it’s

ejs will automatically substitute all the referenced variables passed which are passed to the render function in the template file to get the final html

nest we will use the mailgunClient to send our email after filling all of these options:

  • from : sender
  • to : receiver
  • subject : email subject
  • html : email html body , we will use the ejs rendered html which we got in the previous step
  • inline : inline attachments like images …
  • attachment : some documents which we want to attach with the email

6. Using our email service

Now let’s try to make use our email service in app.controller.ts

Conclusion

In this tutorial, we’ve walked through the process of sending emails in a NestJS application using the Mailgun API. Here are the key steps we covered:

  1. Setting up a NestJS project and installing the necessary dependencies.
  2. Creating an EmailModule and EmailService to encapsulate email-related logic.
  3. Storing Mailgun credentials securely in a .env file and using the @nestjs/config module to load them.
  4. Initializing the Mailgun client and sending emails with HTML templates using the EJS templating engine.
  5. With this setup, you can easily integrate email functionality into your NestJS application, allowing you to send dynamic and visually appealing emails to your users. This is just the beginning, and you can further enhance this functionality by handling email responses, error handling, and more, depending on your application’s requirements.
See all postsSee all posts