import { createTransport } from 'nodemailer'; import { appName } from '../../Utils.js'; // Online Javascript Editor for free // Write, Edit and Run your Javascript code using JS Online Compiler function generateOtpWithSum10() { let otp = []; let sum = 0; // First, generate 3 random digits (0-9) and add them to the sum for (let i = 0; i < 3; i++) { let digit = Math.floor(Math.random() * 10); otp.push(digit); sum += digit; } // Calculate the last digit to ensure the sum equals 10 let lastDigit = 10 - sum; // If the last digit is not valid (e.g., greater than 9), regenerate if (lastDigit >= 0 && lastDigit <= 9) { otp.push(lastDigit); } else { return generateOtpWithSum10(); // Recursively generate again } return otp.join(''); } export default async function validateSmtp(request, response) { const host = request.body.host; const port = request.body?.port?.toString(); const username = request.body.username; const password = request.body.password; const secure = request.body?.secure; const email = request.body?.email; const otp = generateOtpWithSum10(); // console.log('Generated OTP:', otp); try { const smtpsecure = secure || port !== '465' ? false : true; const transporterSMTP = createTransport({ host: host, port: port, secure: smtpsecure, auth: { user: username, pass: password }, }); const from = appName; const mailsender = username; const messageParams = { from: from + ' <' + mailsender + '>', to: email, subject: `Your ${appName} SMTP credentials verification code`, text: 'mail', html: `
SMTP Verification Code
Your verification code is:
` + otp + '