fix: valid file is not attached to mail in signyourself flow

This commit is contained in:
prafull-opensignlabs
2024-09-25 10:11:01 +05:30
parent 7708b7e938
commit 80d3200d57
2 changed files with 45 additions and 17 deletions
@@ -29,6 +29,28 @@ function EmailComponent({
const pdfName = pdfDetails[0]?.Name;
setIsLoading(true);
let sendMail;
const docId = !pdfDetails?.[0]?.IsEnableOTP
? pdfDetails?.[0]?.objectId
: "";
let presignedUrl = pdfUrl;
try {
// const url = await Parse.Cloud.run("getsignedurl", { url: pdfUrl });
const axiosRes = await axios.post(
`${localStorage.getItem("baseUrl")}/functions/getsignedurl`,
{ url: pdfUrl, docId: docId },
{
headers: {
"content-type": "Application/json",
"X-Parse-Application-Id": localStorage.getItem("parseAppId"),
"X-Parse-Session-Token": localStorage.getItem("accesstoken")
}
}
);
presignedUrl = axiosRes.data.result;
} catch (err) {
console.log("err in getsignedurl", err);
}
for (let i = 0; i < emailList.length; i++) {
try {
const imgPng =
@@ -46,7 +68,7 @@ function EmailComponent({
mailProvider: activeMailAdapter,
extUserId: extUserId,
pdfName: pdfName,
url: pdfUrl,
url: presignedUrl,
recipient: emailList[i],
subject: `${sender.name} has signed the doc - ${pdfName}`,
from: sender.email,
@@ -68,28 +68,26 @@ const makeEmail = async (to, from, subject, html, url, pdfName) => {
type: 'application/pdf',
path: './exports/certificate.pdf',
};
attachments = [file, certificate];
if (fs.existsSync(certificate.path)) {
attachments = [file, certificate];
} else {
attachments = [file];
}
} catch (err) {
attachments = [file];
console.log('Err in read certificate sendmailv3', err);
}
}
const attachmentParts = attachments.map(attachment => {
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 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('');
});
const attachmentBody = attachmentParts.join(`\n--${boundary}\n`);
@@ -149,6 +147,14 @@ export default async function sendMailGmailProvider(_extRes, template) {
},
});
console.log('gmail provider res: ', response?.status);
const certificatePath = './exports/certificate.pdf';
if (fs.existsSync(certificatePath)) {
try {
fs.unlinkSync(certificatePath);
} catch (err) {
console.log('Err in unlink certificate sendmailgmail provider');
}
}
return { code: 200, message: 'Email sent successfully' };
} catch (error) {
console.error('Error sending email:', error);