Skip to content

Commit 251e705

Browse files
Merge pull request #1235 from OpenSignLabs/validation
feat: custom smtp support
2 parents f89ef54 + 368eb24 commit 251e705

File tree

6 files changed

+195
-13
lines changed

6 files changed

+195
-13
lines changed

apps/OpenSign/src/pages/Form.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -660,9 +660,7 @@ const Forms = (props) => {
660660
)}
661661
<div className="text-xs">
662662
<label className="block">
663-
{`${t("report-heading.File")} (${t("file-type")}${
664-
isEnableSubscription ? ", docx)" : ")"
665-
}`}
663+
{`${t("report-heading.File")} (${t("file-type")}`}
666664
<span className="text-red-500 text-[13px]">*</span>
667665
</label>
668666
{fileupload.length > 0 ? (

apps/OpenSignServer/cloud/main.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ import BuyCredits from './parsefunction/BuyCredits.js';
6868
import getContact from './parsefunction/getContact.js';
6969
import updateContactTour from './parsefunction/updateContactTour.js';
7070
import declinedocument from './parsefunction/declinedocument.js';
71+
import addcustomsmtp from './parsefunction/addcustomsmtp.js';
72+
import deactivateMailAdapter from './parsefunction/deactivateMailAdapter.js';
7173

7274
// This afterSave function triggers after an object is added or updated in the specified class, allowing for post-processing logic.
7375
Parse.Cloud.afterSave('contracts_Document', DocumentAftersave);
@@ -148,3 +150,5 @@ Parse.Cloud.define('buycredits', BuyCredits);
148150
Parse.Cloud.define('getcontact', getContact);
149151
Parse.Cloud.define('updatecontacttour', updateContactTour);
150152
Parse.Cloud.define('declinedoc', declinedocument);
153+
Parse.Cloud.define('addsmtp', addcustomsmtp);
154+
Parse.Cloud.define('deactivatemailadapter', deactivateMailAdapter);
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
export default async function addcustomsmtp(request) {
2+
if (!request?.user) {
3+
throw new Parse.Error(Parse.Error.INVALID_SESSION_TOKEN, 'User is not authenticated.');
4+
}
5+
const host = request.params.host;
6+
const port = request.params.port;
7+
const username = request.params.username;
8+
const password = request.params.password;
9+
if (host && port && username && password) {
10+
try {
11+
const extUserCls = new Parse.Query('contracts_Users');
12+
extUserCls.equalTo('UserId', request.user);
13+
const extUser = await extUserCls.first({ useMasterKey: true });
14+
if (extUser) {
15+
const extUserCls = new Parse.Object('contracts_Users');
16+
extUserCls.id = extUser.id;
17+
extUserCls.set('SmtpConfig', { host, port, username, password });
18+
extUserCls.set('active_mail_adapter', 'smtp');
19+
const updateExtUser = await extUserCls.save(null, { useMasterKey: true });
20+
// console.log('updateExtUser ', updateExtUser);
21+
return updateExtUser.updatedAt;
22+
}
23+
return extUser;
24+
} catch (err) {
25+
console.log('Err in add custom smtp', err);
26+
const code = err.code || 400;
27+
const msg = err.message || 'Something went wrong.';
28+
throw new Parse.Error(code, msg);
29+
}
30+
} else {
31+
throw new Parse.Error(Parse.Error.INVALID_QUERY, 'Please provide all parameters.');
32+
}
33+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
export default async function deactivateMailAdapter(request) {
2+
if (!request?.user) {
3+
throw new Parse.Error(Parse.Error.INVALID_SESSION_TOKEN, 'User is not authenticated.');
4+
}
5+
try {
6+
const extUserCls = new Parse.Query('contracts_Users');
7+
extUserCls.equalTo('UserId', request.user);
8+
const extUser = await extUserCls.first({ useMasterKey: true });
9+
if (extUser) {
10+
const extUserCls = new Parse.Object('contracts_Users');
11+
extUserCls.id = extUser.id;
12+
extUserCls.unset('active_mail_adapter');
13+
const updateExtUser = await extUserCls.save(null, { useMasterKey: true });
14+
// console.log('updateExtUser ', updateExtUser);
15+
return updateExtUser.updatedAt;
16+
}
17+
return extUser;
18+
} catch (err) {
19+
console.log('Err in add custom smtp', err);
20+
const code = err.code || 400;
21+
const msg = err.message || 'Something went wrong.';
22+
throw new Parse.Error(code, msg);
23+
}
24+
}

apps/OpenSignServer/cloud/parsefunction/sendMailGmailProvider.js

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -75,15 +75,21 @@ const makeEmail = async (to, from, subject, html, url, pdfName) => {
7575
}
7676
}
7777
const attachmentParts = attachments.map(attachment => {
78-
const content = fs.readFileSync(attachment.path);
79-
const encodedContent = content.toString('base64');
80-
return [
81-
`Content-Type: ${attachment.type}\n`,
82-
'MIME-Version: 1.0\n',
83-
`Content-Disposition: attachment; filename="${attachment.filename}"\n`,
84-
`Content-Transfer-Encoding: base64\n\n`,
85-
`${encodedContent}\n`,
86-
].join('');
78+
if (fs.existsSync(attachment.path)) {
79+
try {
80+
const content = fs.readFileSync(attachment.path);
81+
const encodedContent = content.toString('base64');
82+
return [
83+
`Content-Type: ${attachment.type}\n`,
84+
'MIME-Version: 1.0\n',
85+
`Content-Disposition: attachment; filename="${attachment.filename}"\n`,
86+
`Content-Transfer-Encoding: base64\n\n`,
87+
`${encodedContent}\n`,
88+
].join('');
89+
} catch (err) {
90+
console.log('Err in read attachments sendmailv3', attachment.path);
91+
}
92+
}
8793
});
8894

8995
const attachmentBody = attachmentParts.join(`\n--${boundary}\n`);

apps/OpenSignServer/cloud/parsefunction/sendMailv3.js

Lines changed: 118 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,111 @@ async function sendMailProvider(req, plan, monthchange) {
187187
}
188188
}
189189
}
190+
async function sendcustomsmtp(extRes, req) {
191+
const smtpsecure = extRes.SmtpConfig.port !== '465' ? false : true;
192+
const transporterSMTP = createTransport({
193+
host: extRes.SmtpConfig.host,
194+
port: extRes.SmtpConfig.port,
195+
secure: smtpsecure,
196+
auth: { user: extRes.SmtpConfig.username, pass: extRes.SmtpConfig.password },
197+
});
198+
if (req.params.url) {
199+
let Pdf = fs.createWriteStream('test.pdf');
200+
const writeToLocalDisk = () => {
201+
return new Promise((resolve, reject) => {
202+
if (useLocal !== 'true') {
203+
https.get(req.params.url, async function (response) {
204+
response.pipe(Pdf);
205+
response.on('end', () => resolve('success'));
206+
});
207+
} else {
208+
const path = new URL(req.params.url)?.pathname;
209+
const localurl = 'http://localhost:8080' + path;
210+
http.get(localurl, async function (response) {
211+
response.pipe(Pdf);
212+
response.on('end', () => resolve('success'));
213+
});
214+
}
215+
});
216+
};
217+
// `writeToLocalDisk` is used to create pdf file from doc url
218+
const ress = await writeToLocalDisk();
219+
if (ress) {
220+
function readTolocal() {
221+
return new Promise((resolve, reject) => {
222+
setTimeout(() => {
223+
let PdfBuffer = fs.readFileSync(Pdf.path);
224+
resolve(PdfBuffer);
225+
}, 100);
226+
});
227+
}
228+
// `PdfBuffer` used to create buffer from pdf file
229+
let PdfBuffer = await readTolocal();
230+
const pdfName = req.params.pdfName ? `${req.params.pdfName}.pdf` : 'exported.pdf';
231+
const file = { filename: pdfName, content: PdfBuffer };
232+
let attachment;
233+
const certificatePath = './exports/certificate.pdf';
234+
if (fs.existsSync(certificatePath)) {
235+
try {
236+
// `certificateBuffer` used to create buffer from pdf file
237+
const certificateBuffer = fs.readFileSync(certificatePath);
238+
const certificate = { filename: 'certificate.pdf', content: certificateBuffer };
239+
attachment = [file, certificate];
240+
} catch (err) {
241+
attachment = [file];
242+
console.log('Err in read certificate sendmailv3', err);
243+
}
244+
} else {
245+
attachment = [file];
246+
}
247+
const from = req.params.from || '';
248+
const mailsender = extRes.SmtpConfig.username;
249+
250+
const messageParams = {
251+
from: from + ' <' + mailsender + '>',
252+
to: req.params.recipient,
253+
subject: req.params.subject,
254+
text: req.params.text || 'mail',
255+
html: req.params.html || '',
256+
attachments: attachment,
257+
};
258+
const res = await transporterSMTP.sendMail(messageParams);
259+
console.log('custom smtp transporter res: ', res?.response);
260+
if (!res.err) {
261+
if (req.params?.extUserId) {
262+
await updateMailCount(req.params.extUserId); //, plan, monthchange
263+
}
264+
if (fs.existsSync(certificatePath)) {
265+
try {
266+
fs.unlinkSync(certificatePath);
267+
} catch (err) {
268+
console.log('Err in unlink certificate sendmailv3');
269+
}
270+
}
271+
return { status: 'success', code: 200 };
272+
}
273+
}
274+
} else {
275+
const from = req.params.from || '';
276+
const mailsender = extRes.SmtpConfig.username;
277+
const messageParams = {
278+
from: from + ' <' + mailsender + '>',
279+
to: req.params.recipient,
280+
subject: req.params.subject,
281+
text: req.params.text || 'mail',
282+
html: req.params.html || '',
283+
};
284+
285+
const res = await transporterSMTP.sendMail(messageParams);
286+
console.log('custom smtp transporter res: ', res?.response);
287+
if (!res.err) {
288+
if (req.params?.extUserId) {
289+
await updateMailCount(req.params.extUserId); //, plan, monthchange
290+
}
291+
return { status: 'success', code: 200 };
292+
}
293+
}
294+
}
190295
async function sendmailv3(req) {
191296
const mailProvider = req.params.mailProvider || 'default';
192297
if (mailProvider) {
@@ -206,14 +311,26 @@ async function sendmailv3(req) {
206311
const extRes = await extUserQuery.get(extUserId, { useMasterKey: true });
207312
if (extRes) {
208313
const _extRes = JSON.parse(JSON.stringify(extRes));
209-
if (_extRes.google_refresh_token && mailProvider === 'google') {
314+
if (
315+
_extRes.active_mail_adapter === 'google' &&
316+
_extRes.google_refresh_token &&
317+
mailProvider === 'google'
318+
) {
210319
const res = await sendMailGmailProvider(_extRes, template);
211320
if (res.code === 200) {
212321
await updateMailCount(req.params.extUserId);
213322
return { status: 'success' };
214323
} else {
215324
return { status: 'error' };
216325
}
326+
} else if (_extRes.active_mail_adapter === 'smtp' && mailProvider === 'smtp') {
327+
const res = await sendcustomsmtp(_extRes, req);
328+
if (res.code === 200) {
329+
await updateMailCount(req.params.extUserId);
330+
return { status: 'success' };
331+
} else {
332+
return { status: 'error' };
333+
}
217334
} else {
218335
if (Plan && Plan === 'freeplan') {
219336
let MonthlyFreeEmails = _extRes?.MonthlyFreeEmails || 0;

0 commit comments

Comments
 (0)