Create an SMTP server with NodeJS
By: Simon Carr
Tags: Smtp, Programming, Nodejs, JavaScript, Startup

In this article, I will show you how simple it is to create an SMTP server using NodeJS and be up and running in a few minutes. You can then send emails to your node application, either as a method of remote control or for 1001 other reasons.

An SMTP receives emails from other email servers or email clients. If you want an application to process emails in realtime, then your own SMTP server is the way to go. Writing your own server provides some added benefits, like being able to use your own domain name with unlimited email addresses at no extra cost.

There is a bit more work to get it up and running, but I promise, it’s not that much.

What you're going to need.

Your own domain name with the ability to edit DNS records.

NodeJS

A fixed IP Address

If you're on a home network you will need to be able to set up port forward rules on your Internet Router.

DNS setup

In order for a domain to receive emails, it needs to know about the mail server that will handle incoming email.

I already have a domain hackmail.net which I will use as an example throughout this tutorial.

Here is how you need to configure your domain records. I use 123-reg.co.uk for domain registration. They provide their own guide on mail server setup. Your registrar may have a similar guide.

Step 1

Delete any existing MX records on your domain.

Step 2

Add a new A record that points to your internet router IP Address.

Hostname: mail

Type: A

Desination IP Address: <Your routers Public IP Address>

Step 3

Create a new MX record that points to the fully qualified hostname

Hostname: @

Type: MX

Priority: 10

Destination MX: mail.hackmail.net.

Notice the full stop (period) after the Destination. MX records must be terminated with a period or they will not work.

That’s it, Your DNS is configured.

Port Forwarding

You only need to do this if the server, computer, laptop that you will be running the server on, is behind a network router. How you configure this will be different for every router brand. At a high level, you need to tell the router to forward all traffic arriving on port 25 to the internal IP address of the computer running the SMTP server.

Forward all traffic arriving on port: 25 TO IP Address: <Your internal IP Address>

port: 25

Firewall configuration

If you have a firewall set up on your network or computer, you will need to make sure that traffic on port 25 is allowed through. Consult the docs for the firewall your using.

Writing the code

Create a new folder called node_smtp_server

In the new folder, run the following command

npm init -y

Create a new file called app.js

touch app.js

Before I add any code, I am going to install two npm modules. Run the following command to install them.

npm install smtp-server mailparser --save

Now open app.js in your editor of choice and add the following code.

On line 1, I import smtp-server this is the core of the application.

On line 2, I have imported mailparser , this will convert the received emails that are a simple string into a JSON object. This makes it easy to get the data I need or save them to a database like MongoDB.

Line 4 creates a new SMTP Server. It takes an object containing configuration as the constructor. In this example, I am passing in two options. I will cover the second option first.

disabledCommands is an array of commands you want to disable. In this case I am disabling AUTH . If you don’t do, this, any emails you send to this server will be bounced with an error. 530 Error: authentication Required

onData is a method that can take 3 parameters stream , session , and callback . In this case, I only require the stream and callback . The session is used for email clients, when a user is logging into the server with a client.

stream is a readable stream for the incoming email.

callback is run when the stream is ended. If you return an error object, it will reject the email.

Line 18 puts the server online. It only requires one parameter the port number In this case, I am also passing the optional IP address to listen on.

You can read more about the smtp-server module and it’s many options here https://nodemailer.com/extras/smtp-server/

Testing it out

I am going to send an email from my Gmail Account to simon@hackmail.net . When the message is received a few seconds later, the following is outputted to the terminal.

As you can see it’s an easy to use JSON object, thanks to the mailparser module.

Conclusion

Writing a mail server in NodeJS is really simple and can provide some really powerful features for an application.

Let me know in the comments, your ideas for using an SMTP server in an application.

As long as you can edit your domain’s DNS records, you can be up and running a few minutes.

If you liked this article, please show your appreciation and leave me a message. If you didn’t like it, I still appreciate the feedback, it keeps me on my toes 🙂