OTP Verification in CampusX Using Brevo SMTP



This content originally appeared on DEV Community and was authored by Prathviraj H

Introduction

Email verification is a critical security feature in any web application. CampusX leverages Brevo SMTP (formerly Sendinblue) to send OTPs for user authentication and email verification. This ensures secure and reliable email delivery.

Why Brevo SMTP?

  • Reliable Delivery: Ensures OTP emails don’t land in spam.
  • Scalability: Handles high email traffic effortlessly.
  • Easy Integration: Works with Nodemailer for seamless email sending.
  • Cost-Effective: Affordable compared to other email services.

Setting Up Brevo SMTP in CampusX

Step 1: Configure Brevo SMTP

  1. Sign up on Brevo and go to SMTP & API.
  2. Generate SMTP credentials.
  3. Add these credentials to your .env file:
   SMTP_HOST=smtp-relay.brevo.com
   SMTP_PORT=587
   SMTP_USER=your_brevo_username
   SMTP_PASS=your_brevo_password


`

Implementing OTP Sending in CampusX

Install Nodemailer

sh
npm install nodemailer

OTP Email Service

`js
import nodemailer from ‘nodemailer’;
import dotenv from ‘dotenv’;
dotenv.config();

const transporter = nodemailer.createTransport({
host: process.env.SMTP_HOST,
port: process.env.SMTP_PORT,
auth: {
user: process.env.SMTP_USER,
pass: process.env.SMTP_PASS,
},
});

export const sendOTP = async (email, otp) => {
const mailOptions = {
from: ‘no-reply@campusx.com‘,
to: email,
subject: ‘CampusX OTP Verification’,
text: Your OTP is: ${otp}. It is valid for 10 minutes.
};
await transporter.sendMail(mailOptions);
};
`

Verifying OTP

CampusX stores OTPs temporarily in Redis with an expiration time.

`js
import Redis from ‘ioredis’;
const redis = new Redis();

export const verifyOTP = async (email, enteredOtp) => {
const storedOtp = await redis.get(otp:${email});
if (!storedOtp || storedOtp !== enteredOtp) {
throw new Error(‘Invalid or expired OTP’);
}
await redis.del(otp:${email}); // Remove OTP after verification
return true;
};
`

Conclusion

Using Brevo SMTP with CampusX ensures secure, scalable, and efficient OTP delivery. Integrating Redis for OTP storage prevents unnecessary database queries while enhancing security.

🔹 Looking for a reliable email verification setup? Try Brevo SMTP! 🚀


This content originally appeared on DEV Community and was authored by Prathviraj H