|
| 1 | +import { createTransport } from 'nodemailer'; |
| 2 | +import { appName } from '../../Utils.js'; |
| 3 | +// Online Javascript Editor for free |
| 4 | +// Write, Edit and Run your Javascript code using JS Online Compiler |
| 5 | + |
| 6 | +function generateOtpWithSum10() { |
| 7 | + let otp = []; |
| 8 | + let sum = 0; |
| 9 | + |
| 10 | + // First, generate 3 random digits (0-9) and add them to the sum |
| 11 | + for (let i = 0; i < 3; i++) { |
| 12 | + let digit = Math.floor(Math.random() * 10); |
| 13 | + otp.push(digit); |
| 14 | + sum += digit; |
| 15 | + } |
| 16 | + |
| 17 | + // Calculate the last digit to ensure the sum equals 10 |
| 18 | + let lastDigit = 10 - sum; |
| 19 | + |
| 20 | + // If the last digit is not valid (e.g., greater than 9), regenerate |
| 21 | + if (lastDigit >= 0 && lastDigit <= 9) { |
| 22 | + otp.push(lastDigit); |
| 23 | + } else { |
| 24 | + return generateOtpWithSum10(); // Recursively generate again |
| 25 | + } |
| 26 | + |
| 27 | + return otp.join(''); |
| 28 | +} |
| 29 | + |
| 30 | +export default async function validateSmtp(request, response) { |
| 31 | + const host = request.body.host; |
| 32 | + const port = request.body?.port?.toString(); |
| 33 | + const username = request.body.username; |
| 34 | + const password = request.body.password; |
| 35 | + const secure = request.body?.secure; |
| 36 | + const email = request.body?.email; |
| 37 | + const otp = generateOtpWithSum10(); |
| 38 | + // console.log('Generated OTP:', otp); |
| 39 | + |
| 40 | + try { |
| 41 | + const smtpsecure = secure || port !== '465' ? false : true; |
| 42 | + const transporterSMTP = createTransport({ |
| 43 | + host: host, |
| 44 | + port: port, |
| 45 | + secure: smtpsecure, |
| 46 | + auth: { user: username, pass: password }, |
| 47 | + }); |
| 48 | + const from = appName; |
| 49 | + const mailsender = username; |
| 50 | + const messageParams = { |
| 51 | + from: from + ' <' + mailsender + '>', |
| 52 | + to: email, |
| 53 | + subject: `Your ${appName} SMTP credentials verification code`, |
| 54 | + text: 'mail', |
| 55 | + html: |
| 56 | + `<html><head><meta http-equiv='Content-Type' content='text/html; charset=UTF-8' /></head><body><div style='background-color:#f5f5f5;padding:20px'><div style='box-shadow: rgba(0, 0, 0, 0.1) 0px 4px 12px;background-color:white;'><div style='background-color:red;padding:2px;font-family:system-ui; background-color:#47a3ad;'> <p style='font-size:20px;font-weight:400;color:white;padding-left:20px',>SMTP Verification Code</p></div><div style='padding:20px'><p style='font-family:system-ui;font-size:14px'>Your verification code is:</p><p style=' text-decoration: none; font-weight: bolder; color:blue;font-size:45px;margin:20px'>` + |
| 57 | + otp + |
| 58 | + '</p></div> </div> </div></body></html>', |
| 59 | + }; |
| 60 | + |
| 61 | + const res = await transporterSMTP.sendMail(messageParams); |
| 62 | + console.log('custom smtp transporter res: ', res?.response); |
| 63 | + if (!res.err) { |
| 64 | + response.status(200).json({ message: 'success' }); |
| 65 | + } |
| 66 | + } catch (err) { |
| 67 | + console.log('err ', err); |
| 68 | + const code = err?.responseCode || 400; |
| 69 | + const message = err?.response || 'failed!'; |
| 70 | + response.status(code).json({ error: message }); |
| 71 | + } |
| 72 | +} |
0 commit comments