Skip to content

Commit ce671d4

Browse files
feat: add validatesmtp endpoint
1 parent f699d93 commit ce671d4

File tree

3 files changed

+74
-1
lines changed

3 files changed

+74
-1
lines changed

apps/OpenSign/src/primitives/GetReportDisplay.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -777,7 +777,6 @@ const ReportTable = (props) => {
777777
"X-Parse-Application-Id": localStorage.getItem("parseAppId"),
778778
sessionToken: localStorage.getItem("accesstoken")
779779
};
780-
console.log("doc?.ExtUserPtr ", doc?.ExtUserPtr);
781780
let params = {
782781
mailProvider: doc?.ExtUserPtr?.active_mail_adapter || "",
783782
extUserId: doc?.ExtUserPtr?.objectId,

apps/OpenSignServer/cloud/customRoute/customApp.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import saveInvoice from './saveInvoice.js';
88
import savePayments from './savePayments.js';
99
import gooogleauth from './googleauth.js';
1010
import autoReminder from './autoReminder.js';
11+
import validateSmtp from './validateSmtp.js';
1112
export const app = express();
1213

1314
dotenv.config();
@@ -22,3 +23,4 @@ app.post('/saveinvoice', saveInvoice);
2223
app.post('/savepayment', savePayments);
2324
app.post('/googleauth', gooogleauth);
2425
app.post('/sendreminder', autoReminder);
26+
app.post('/validatesmtp', validateSmtp);
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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

Comments
 (0)