Send email verification mail and use custom domain to send emails using Node.js

Sarthak Singhal
2 min readAug 26, 2020

I spent a lot of time figuring out the perfect way to send confirmation mail. Here is the method I used.

Libraries

Nodemailer- To send mails

JWT- To encode user data

 const jwt = require(‘jsonwebtoken’);
const nodemailer = require(“nodemailer”);

Step 1
Encode the user id in a jwt token with an expiration date

var date = new Date();
var mail = {
“id”: user.id,
“created”: date.toString()
}
const token_mail_verification = jwt.sign(mail, config.jwt_secret_mail, { expiresIn: ‘1d’ });

var url = config.baseUrl + “verify?id=” + token_mail_verification;

Step 2
Send the token to the user email address using nodemailer library

let transporter = nodemailer.createTransport({
name: “www.domain.com",
host: “smtp.domain.com”,
port: 323,
secure: false, // use SSL
auth: {
user: “user@domain.com”, // username for your mail server
pass: “Password”, // password
},
});// send mail with defined transport object
let info = await transporter.sendMail({
from: ‘“NAME” <user@domain.com>’, // sender address
to: user.email, // list of receivers seperated by comma
subject: “Account Verification”, // Subject line
html: "Hello,<br> Please Click on the link to verify your email.<br><a href="+url+">Click here to verify</a>"
}, (error, info) => {
if (error) {
console.log(error)
return;
}
console.log(‘Message sent successfully!’);
console.log(info);
transporter.close();
});

Step 3
Accept the verification link

app.get(‘/verify’, function(req, res) {
token = req.query.id;
if (token) {
try {
jwt.verify(token, config.jwt_secret_mail, (e, decoded) => {
if (e) {
console.log(e)
return res.sendStatus(403)
} else {
id = decoded.id;
//Update your database here with whatever the verification flag you are using}});
} catch (err) {
console.log(err)
return res.sendStatus(403)
}
} else {
return res.sendStatus(403)
}})

Step 4
Have a coffee and thank mefor saving your time XD

PS: This nodemailer SMTP method will even work with your hosting. So no need to go for third party. You can also find ways to use gmail with nodemailer.

--

--