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

 const jwt = require(‘jsonwebtoken’);
const nodemailer = require(“nodemailer”);
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;
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();
});
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)
}})

--

--

--

Founder at elysionsoftwares.com

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Cheat Sheet for JS methods to work with DOM, part 1

Creating Media with Anime.js — 001

The Beginner’s Guide to React Redux

https://youtu.be/-sNzyj1hqcs

Custom Events in JavaScript

Basic HTTP Server Using NodeJS From Scratch

Creating a Landing Page Using TUK Components

Dynamic Data and Maps — ReactJS Tutorial — Leaflet and Cluster Groups #3

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Sarthak Singhal

Sarthak Singhal

Founder at elysionsoftwares.com

More from Medium

Nextjs — introduction to deployment🚀: Nextjs video tutorial series 📺

Nuxt3 complex solutions: database integration

How to use next/image with cloud providers

How to use multiple node js versions