SMTP Mail Server

Overview

Recently, I had the opportunity to develop and integrate an SMTP email server into a larger full-stack application. In this tutorial, I’ll reference the code from my solution to guide you through the process.

We will primarily use Express and Node.js to create the server. Additionally, we’ll integrate Nodemailer to construct a mailable object and the Gmail SMTP server to send our emails for free.

Features

efore we start coding, we should quantify the expected functionality of our Express mail server. We can always refer back to this list later if confusion develops.
  • Establish API routes to a contact form on the frontend.
  • Receive user data from these routes upon form submission.
  • Format a mailable JSON object with the data received.
  • Construct a transport function with Nodemailer in preparation for delivery.
  • Obtain authorization from Gmail using credentials stored in the server.
  • Call the transport function, prompting Nodemailer to handoff the email object to the Gmail SMTP Server for delivery.
  • Request confirmation of successful delivery and return an error message if not received.

Upon completion, we will have the ideal backend for processing contact forms. Alternatively, this server will also serve as a robust foundation for building a full-stack MERN app with extensive functionality.

The production version of the server we are building today powers the backend of Ormica, a real estate investment firm in South Florida.

Prerequisites

  • A basic understanding of fundamental programming concepts and familiarity with at least one server-side programming language such as [Node, Python, Ruby, PHP].
  • A code editor such as Visual Studio
  • Node.js and npm locally installed. (Current LTS version preferred)

Development

Begin with these terminal commands to make an empty directory wherever you wish to store your server’s local version and create the first file using npm.
Now we need to add the required dependencies (npm packages) to our project. Run the following command and notice that npm automatically creates a new directory called node_modules. This is normal, so don’t change or configure anything in this folder.
  • Express is a minimal and flexible Node.js web application framework that provides a robust set of features.
  • Nodemailer is a module designed to allow Node.js applications to send emails securely and efficiently.
  • Morgan is named after Dexter (a show you should not watch until completion) and provides HTTP request logger middleware for Node.js.
  • dotenv is a zero-dependency module that loads environment variables from a .env file into process.env.
Remember the transport function we mentioned earlier? In this step, we will construct that same function. Let’s have a look!

Part 1: Dependencies

Begin with these terminal commands to make an empty directory wherever you wish to store your server’s local version and create the first file using npm.

Follow the terminal prompts from npm to create a package.json file. In most cases, the default values will suffice, but be sure to change the entry point to server.js instead of the proposed index.js.

Now we need to add the required dependencies (npm packages) to our project. Run the following command and notice that npm automatically creates a new directory called node_modules. This is normal, so don’t change or configure anything in this folder.

Part 2: Routes

Let’s begin by creating a routes folder containing routes.js.
Open routes.js in your code editor and establish a baseline by requiring the dependencies at the top of the file.

As you can see, Express has its own built-in router. I have opted to use this option instead of a separate npm package. This helps keep our application lightweight and efficient.

Our next order of work will involve setting up authentication using our Gmail account credentials. Don’t forget to add the .env file in the root directory rather than our current location.

Remember the transport function we mentioned earlier? In this step, we will construct that same function. Let’s have a look!

Now let’s build the API routes. We will simultaneously define the schema for our JSON object (email).

In simple terms, we are telling Nodemailer about the form data it will be receiving from the front-end and how it should translate said data into a structured email.

Finally, we call the transporter.sendMail function, Nodemailer works its magic, and the email goes on its merry way to the recipient’s inbox.

The last block of code in this file instructs Express to use the routes we have established; finally, we export the entire router to the Node server.

Part 3: Server

Let’s create a server.js file in the root directory and open it with the editor.

Here we are just ensuring everything runs smoothly. I could write an entire article about each of the parameters defined here, but the template is pretty standard.

Let’s start by defining some initial requirements.

We will then connect to the frontend, priority serve our static HTML files, define some middleware for AJAX parsing, and finally call the routes we are importing from the routes.js file, all in that order.

We wrap it up with some error logging for the development environment, and voila! Our Express mail server is humming along.

SMTP Email Server is complete! Congratulations!

NOTE: If you encounter difficulty with the authentication
process, read this Google Support documentation! It will save
you hours of debugging and Excedrin.

Conclusion

We’ve successfully created an SMTP Email Server using Express and Node. We also learned how to integrate Nodemailer and Gmail to streamline the mailing process. Feel free to clone the source code and explore your own methods of implementation. I hope you have found this tutorial useful, and thank you for taking the time to follow along!

Contact Form

Please enable JavaScript in your browser to complete this form.