Create an SMTP server with NodeJS
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.
- 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.
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.
Delete any existing
MX records on your domain.
Add a new
A record that points to your internet router IP Address.
Desination IP Address: <Your routers Public IP Address>
Create a new
MX record that points to the fully qualified hostname
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.
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: 25TOIP Address: <Your internal IP Address>
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
In the new folder, run the following command
npm init -y
Create a new file called
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
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
callback. In this case, I only require 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
email@example.com. 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
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 🙂