How to Set Up Nodemailer, Send Emails & Verify User Email in Node.js with MongoDB

Published: 06 March 2025
on channel: CodeCaty
110
2

In this tutorial, you'll learn how to set up Nodemailer in a Node.js application, send verification emails, and use MongoDB for storing and verifying email tokens. We'll walk you through the process of configuring your SMTP server (such as Gmail), sending email verification links, and handling JWT tokens for secure user verification.

What you'll learn in this video:
Setting up Nodemailer in Node.js to send emails
Creating email verification tokens with JWT
Storing tokens in MongoDB for user verification
Sending verification emails securely using SMTP
MongoDB setup and integration for user verification
Handling JWT tokens securely for email verification
Important Notes:
Replace sensitive data such as SMTP credentials, MongoDB connection URI, and JWT tokens with your own values in a secure .env file.
In the .env file, you should add values like these:
.env
JWT Tokens (replace with your actual tokens in production)
JWT_TOKEN=your_jwt_token_here
JWT_REFRESH_TOKEN=your_jwt_refresh_token_here
JWT_EXPIRES_IN='1d'

MongoDB URI (replace with your actual MongoDB connection string)
MONGO_URI=mongodb://localhost:27017/your_database_name

SMTP Configuration (replace with your actual SMTP details)
SMTP_PASSWORD=your_smtp_password_here
SMTP_EMAIL=your_smtp_email_here
SMTP_HOST='smtp.gmail.com'
SMTP_PORT=587

Email Verification URL (replace with your actual URL)
EMAIL_VERIFICATION_URL='http://localhost:3000/auth/verify-email/'

Email Token (replace with your actual token for verification)
EMAIL_TOKEN=your_email_token_here
This tutorial assumes you have Node.js and MongoDB set up locally. If not, please install them first.
GitHub Repository:
Check out the full project code here:
https://github.com/ashwani8090/code-c...

Don't forget to:
Like 👍
Comment 💬
Subscribe 🔔 for more tutorials
If you have any questions, feel free to leave them in the comments below!