diff --git a/apps/OpenSign/src/pages/Form.js b/apps/OpenSign/src/pages/Form.js index 6ab655609..aaf5cd172 100644 --- a/apps/OpenSign/src/pages/Form.js +++ b/apps/OpenSign/src/pages/Form.js @@ -660,9 +660,7 @@ const Forms = (props) => { )}
{fileupload.length > 0 ? ( diff --git a/apps/OpenSignServer/cloud/main.js b/apps/OpenSignServer/cloud/main.js index a87022e44..b1f6c2e2f 100644 --- a/apps/OpenSignServer/cloud/main.js +++ b/apps/OpenSignServer/cloud/main.js @@ -68,6 +68,8 @@ import BuyCredits from './parsefunction/BuyCredits.js'; import getContact from './parsefunction/getContact.js'; import updateContactTour from './parsefunction/updateContactTour.js'; import declinedocument from './parsefunction/declinedocument.js'; +import addcustomsmtp from './parsefunction/addcustomsmtp.js'; +import deactivateMailAdapter from './parsefunction/deactivateMailAdapter.js'; // This afterSave function triggers after an object is added or updated in the specified class, allowing for post-processing logic. Parse.Cloud.afterSave('contracts_Document', DocumentAftersave); @@ -148,3 +150,5 @@ Parse.Cloud.define('buycredits', BuyCredits); Parse.Cloud.define('getcontact', getContact); Parse.Cloud.define('updatecontacttour', updateContactTour); Parse.Cloud.define('declinedoc', declinedocument); +Parse.Cloud.define('addsmtp', addcustomsmtp); +Parse.Cloud.define('deactivatemailadapter', deactivateMailAdapter); diff --git a/apps/OpenSignServer/cloud/parsefunction/addcustomsmtp.js b/apps/OpenSignServer/cloud/parsefunction/addcustomsmtp.js new file mode 100644 index 000000000..40e0986d5 --- /dev/null +++ b/apps/OpenSignServer/cloud/parsefunction/addcustomsmtp.js @@ -0,0 +1,33 @@ +export default async function addcustomsmtp(request) { + if (!request?.user) { + throw new Parse.Error(Parse.Error.INVALID_SESSION_TOKEN, 'User is not authenticated.'); + } + const host = request.params.host; + const port = request.params.port; + const username = request.params.username; + const password = request.params.password; + if (host && port && username && password) { + try { + const extUserCls = new Parse.Query('contracts_Users'); + extUserCls.equalTo('UserId', request.user); + const extUser = await extUserCls.first({ useMasterKey: true }); + if (extUser) { + const extUserCls = new Parse.Object('contracts_Users'); + extUserCls.id = extUser.id; + extUserCls.set('SmtpConfig', { host, port, username, password }); + extUserCls.set('active_mail_adapter', 'smtp'); + const updateExtUser = await extUserCls.save(null, { useMasterKey: true }); + // console.log('updateExtUser ', updateExtUser); + return updateExtUser.updatedAt; + } + return extUser; + } catch (err) { + console.log('Err in add custom smtp', err); + const code = err.code || 400; + const msg = err.message || 'Something went wrong.'; + throw new Parse.Error(code, msg); + } + } else { + throw new Parse.Error(Parse.Error.INVALID_QUERY, 'Please provide all parameters.'); + } +} diff --git a/apps/OpenSignServer/cloud/parsefunction/deactivateMailAdapter.js b/apps/OpenSignServer/cloud/parsefunction/deactivateMailAdapter.js new file mode 100644 index 000000000..a84087613 --- /dev/null +++ b/apps/OpenSignServer/cloud/parsefunction/deactivateMailAdapter.js @@ -0,0 +1,24 @@ +export default async function deactivateMailAdapter(request) { + if (!request?.user) { + throw new Parse.Error(Parse.Error.INVALID_SESSION_TOKEN, 'User is not authenticated.'); + } + try { + const extUserCls = new Parse.Query('contracts_Users'); + extUserCls.equalTo('UserId', request.user); + const extUser = await extUserCls.first({ useMasterKey: true }); + if (extUser) { + const extUserCls = new Parse.Object('contracts_Users'); + extUserCls.id = extUser.id; + extUserCls.unset('active_mail_adapter'); + const updateExtUser = await extUserCls.save(null, { useMasterKey: true }); + // console.log('updateExtUser ', updateExtUser); + return updateExtUser.updatedAt; + } + return extUser; + } catch (err) { + console.log('Err in add custom smtp', err); + const code = err.code || 400; + const msg = err.message || 'Something went wrong.'; + throw new Parse.Error(code, msg); + } +} diff --git a/apps/OpenSignServer/cloud/parsefunction/sendMailGmailProvider.js b/apps/OpenSignServer/cloud/parsefunction/sendMailGmailProvider.js index f614e5754..b3f8c10cd 100644 --- a/apps/OpenSignServer/cloud/parsefunction/sendMailGmailProvider.js +++ b/apps/OpenSignServer/cloud/parsefunction/sendMailGmailProvider.js @@ -75,15 +75,21 @@ const makeEmail = async (to, from, subject, html, url, pdfName) => { } } const attachmentParts = attachments.map(attachment => { - const content = fs.readFileSync(attachment.path); - const encodedContent = content.toString('base64'); - return [ - `Content-Type: ${attachment.type}\n`, - 'MIME-Version: 1.0\n', - `Content-Disposition: attachment; filename="${attachment.filename}"\n`, - `Content-Transfer-Encoding: base64\n\n`, - `${encodedContent}\n`, - ].join(''); + if (fs.existsSync(attachment.path)) { + try { + const content = fs.readFileSync(attachment.path); + const encodedContent = content.toString('base64'); + return [ + `Content-Type: ${attachment.type}\n`, + 'MIME-Version: 1.0\n', + `Content-Disposition: attachment; filename="${attachment.filename}"\n`, + `Content-Transfer-Encoding: base64\n\n`, + `${encodedContent}\n`, + ].join(''); + } catch (err) { + console.log('Err in read attachments sendmailv3', attachment.path); + } + } }); const attachmentBody = attachmentParts.join(`\n--${boundary}\n`); diff --git a/apps/OpenSignServer/cloud/parsefunction/sendMailv3.js b/apps/OpenSignServer/cloud/parsefunction/sendMailv3.js index 35e80ab78..508951156 100644 --- a/apps/OpenSignServer/cloud/parsefunction/sendMailv3.js +++ b/apps/OpenSignServer/cloud/parsefunction/sendMailv3.js @@ -187,6 +187,111 @@ async function sendMailProvider(req, plan, monthchange) { } } } +async function sendcustomsmtp(extRes, req) { + const smtpsecure = extRes.SmtpConfig.port !== '465' ? false : true; + const transporterSMTP = createTransport({ + host: extRes.SmtpConfig.host, + port: extRes.SmtpConfig.port, + secure: smtpsecure, + auth: { user: extRes.SmtpConfig.username, pass: extRes.SmtpConfig.password }, + }); + if (req.params.url) { + let Pdf = fs.createWriteStream('test.pdf'); + const writeToLocalDisk = () => { + return new Promise((resolve, reject) => { + if (useLocal !== 'true') { + https.get(req.params.url, async function (response) { + response.pipe(Pdf); + response.on('end', () => resolve('success')); + }); + } else { + const path = new URL(req.params.url)?.pathname; + const localurl = 'http://localhost:8080' + path; + http.get(localurl, async function (response) { + response.pipe(Pdf); + response.on('end', () => resolve('success')); + }); + } + }); + }; + // `writeToLocalDisk` is used to create pdf file from doc url + const ress = await writeToLocalDisk(); + if (ress) { + function readTolocal() { + return new Promise((resolve, reject) => { + setTimeout(() => { + let PdfBuffer = fs.readFileSync(Pdf.path); + resolve(PdfBuffer); + }, 100); + }); + } + // `PdfBuffer` used to create buffer from pdf file + let PdfBuffer = await readTolocal(); + const pdfName = req.params.pdfName ? `${req.params.pdfName}.pdf` : 'exported.pdf'; + const file = { filename: pdfName, content: PdfBuffer }; + let attachment; + const certificatePath = './exports/certificate.pdf'; + if (fs.existsSync(certificatePath)) { + try { + // `certificateBuffer` used to create buffer from pdf file + const certificateBuffer = fs.readFileSync(certificatePath); + const certificate = { filename: 'certificate.pdf', content: certificateBuffer }; + attachment = [file, certificate]; + } catch (err) { + attachment = [file]; + console.log('Err in read certificate sendmailv3', err); + } + } else { + attachment = [file]; + } + const from = req.params.from || ''; + const mailsender = extRes.SmtpConfig.username; + + const messageParams = { + from: from + ' <' + mailsender + '>', + to: req.params.recipient, + subject: req.params.subject, + text: req.params.text || 'mail', + html: req.params.html || '', + attachments: attachment, + }; + const res = await transporterSMTP.sendMail(messageParams); + console.log('custom smtp transporter res: ', res?.response); + if (!res.err) { + if (req.params?.extUserId) { + await updateMailCount(req.params.extUserId); //, plan, monthchange + } + if (fs.existsSync(certificatePath)) { + try { + fs.unlinkSync(certificatePath); + } catch (err) { + console.log('Err in unlink certificate sendmailv3'); + } + } + return { status: 'success', code: 200 }; + } + } + } else { + const from = req.params.from || ''; + const mailsender = extRes.SmtpConfig.username; + const messageParams = { + from: from + ' <' + mailsender + '>', + to: req.params.recipient, + subject: req.params.subject, + text: req.params.text || 'mail', + html: req.params.html || '', + }; + + const res = await transporterSMTP.sendMail(messageParams); + console.log('custom smtp transporter res: ', res?.response); + if (!res.err) { + if (req.params?.extUserId) { + await updateMailCount(req.params.extUserId); //, plan, monthchange + } + return { status: 'success', code: 200 }; + } + } +} async function sendmailv3(req) { const mailProvider = req.params.mailProvider || 'default'; if (mailProvider) { @@ -206,7 +311,11 @@ async function sendmailv3(req) { const extRes = await extUserQuery.get(extUserId, { useMasterKey: true }); if (extRes) { const _extRes = JSON.parse(JSON.stringify(extRes)); - if (_extRes.google_refresh_token && mailProvider === 'google') { + if ( + _extRes.active_mail_adapter === 'google' && + _extRes.google_refresh_token && + mailProvider === 'google' + ) { const res = await sendMailGmailProvider(_extRes, template); if (res.code === 200) { await updateMailCount(req.params.extUserId); @@ -214,6 +323,14 @@ async function sendmailv3(req) { } else { return { status: 'error' }; } + } else if (_extRes.active_mail_adapter === 'smtp' && mailProvider === 'smtp') { + const res = await sendcustomsmtp(_extRes, req); + if (res.code === 200) { + await updateMailCount(req.params.extUserId); + return { status: 'success' }; + } else { + return { status: 'error' }; + } } else { if (Plan && Plan === 'freeplan') { let MonthlyFreeEmails = _extRes?.MonthlyFreeEmails || 0;