Hello everyone!!!
Sending mails to your users is like a must do for every developer; it can be a verification mail, a password reset mail, or maybe a simple reminder mail for a service.
The good news is that with NodeJS it is very easy to setup.
So, in this post, I will show you how to send emails using NodeJS in less than 5 minutes.
**PS: follow the instructions below to complete the setup. If you are familiar with nodejs, skip to step 4.
Step 1 (Setup a new NodeJS project)
To setup your NodeJS , do the following:
- Create an empty folder, such as send-mail-using-nodejs
- Open the folder in your code editor, such as vscode.
- Open the terminal. [you can use (
ctrl + shift + `
) to open terminal if you are on windows. ] - Run
npm init
. [input the necessary stuff] or just runnpm init -y
to skip. You should see that package.json was created. Inside the package.json, you change the value of main to "server.js". - Replace the test with
"start": "node server.js"
- Create a server.js file. Your package.json should now contain something that looks like this:
Step 2 (Install necessary dependencies)
To Install the necessary dependencies, do the following.
Open the terminal and Run
npm install express nodemailer
to install express and nodemailer. note: expressJS is nodejs framework that helps us create a server really fast. nodemailer is a client that helps us send emails.Run
npm install -D nodemon dotenv
to install nodemon and dotenv as a dev dependency. note: nodemon helps us restart our server on every file change. Add"dev": "nodemon server.js"
to the package.json. Also, dotenv helps us access our .env variables
Your package.json should now look like this:
Step 3 (Setup a server with ExpressJS)
To setup an express server do the following
Open the server.js file.
Import and setup ExpressJs server:
const express = require("express")
const app = require("express")
app.listen(5000, () => console.log("Server running on port 5000"))
3. Open the terminal and run npm run dev
to start your server.
note: you should see a message saying "Server running on port 5000"
Step 4 (Set up nodemailer)
To set up nodemailer and send mails, do the following:
Import nodemailer:
const nodemailer = require("nodemailer")
Create a transporter:
//The service can be gmail or whatever you choose. I am using outlook or better hotmail. const transporter = nodemailer.createTransport({ service: "hotmail", auth: { user: process.env.EMAIL_USERNAME, pass: process.env.EMAIL_PASSWORD, }, tls: { rejectUnauthorized: false }, })
Import dotenv:
require("dotenv/config")
Create a .env file and put in two key value pair like this:
EMAIL_USERNAME="your_email@outlook.com" EMAIL_PASSWORD="your_password"
Create the body of the email:
const body = { from: process.env.EMAIL_USERNAME, to: "email_address_to_be_sent_to_", subject: "Testing node mailer", html: ` <h1>This is an email to test run nodemailer</h1> <p>So, This is it guys!!! We have done it</p> `, }
Send the email:
transporter.sendMail(body, function (error, info) { if (error) { console.log(error) res.send("An error occured. Try again.") } else { console.log(info) res.json({ info }) } })
Save your work! You should see that the email has been sent.
if you run into this error below, do the following: set this key/value
NODE_EXTRA_CA_CERTS=A_FILE_IN_OUR_PROJECT
in your .env file
Final code should be:
const express = require("express")
const nodemailer = require("nodemailer")
require("dotenv/config")
const app = express()
const transporter = nodemailer.createTransport({
service: "hotmail",
auth: {
user: process.env.EMAIL_USERNAME,
pass: process.env.EMAIL_PASSWORD,
},
tls: { rejectUnauthorized: false },
})
const body = {
from: process.env.EMAIL_USERNAME,
to: "bonarhyme@gmail.com",
subject: "Testing node mailer",
html: `
<h1>This is an email to test run nodemailer</h1>
<p>So, This is it guys!!! We have done it</p>
`,
}
transporter.sendMail(body, function (error, info) {
if (error) {
console.log(error)
} else {
console.log(info)
}
})
app.listen(5000, () => console.log("Server running on port 5000"))
We did it guys!! Next post will be improving this code to send the email using an html form.
Hit the like button and comment to boost my morale