Hello,
I am using @sendgrid/mail - "^6.3.1" & node - 10.14.1.
My problem is that when i send single row data from database in excel attachment , I am able to received mail with attachment but when i send multiple row data from database data in excel as attachment, I receive attachment with 0 bytes . I have embeded images also in the file. I am trying to attach same file creating through exceljs module. I I had tried a lot but failed .. where as I am able to generate the same excel file- with this code in the browser-
"
res.setHeader("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
res.setHeader("Content-Disposition", "attachment;filename='" + excelFile.excelOffFile + "'");
workbookFinal.xlsx.write(res).then(() => {
res.end(); });s
"
My code for sending mail is -
sgMail.send({
to: config.get("mail.defaultAdd"),
from: fromMailAdd,
subject: "Order with excel attachment send through software ",
html: <h1>Test Mail</h1>
,
attachments: [
{
content: fs.readFileSync(excelFile.excelOffFilePath, {encoding:"base64"}),
filename: excelFile.excelOffFile,
type: "application/vnd.ms-excel"
},
{
content: fs.readFileSync(excelFile.excelFactFilePath, { encoding: "base64" }),
filename: excelFile.excelFactFile,
type: "application/vnd.ms-excel"
},
{
content: fs.readFileSync(excelFile.excelBrFilePath, { encoding: "base64" }),
filename: excelFile.excelBrFile,
type: "application/vnd.ms-excel"
},
{
content: fs.readFileSync(excelFile.excelSuFilePath, { encoding: "base64" }),
filename: excelFile.excelSuFile,
type: "application/vnd.ms-excel"
}
]
})
mail screenshot -
Please help me how to solve the same.
Hello @prasoojalan,
It sounds like you either have a problem with the path to the file or the file encoding. I would double check the values of excelFile.*
in your code to ensure the file exists. Then, I would ensure that the file itself is base64 encoded correctly. Maybe this tool can help with the latter.
With Best Regards,
Elmer
Hello @ Elmer
My full code for creating and sending file is as -
I tried giving one attachment root path , but it also did not worked. The same excel file generated , is successfully downloaded in browser .... what code I am missing or I am unable to understand the concept properly... can you help me out..
In createExcelFile function with "C" as parameter, I am returning the workbook where as not in other cases.
exports.getExcelFile = (req, res, next) => {
const excelFile = {};
Order.findById(req.params.id).then(order => {
excelFile.excelOffFile = "Off_" + order.factory + "_" + order._id + ".xls";
excelFile.excelOffFilePath = path.join("data", "excel", excelFile.excelOffFile);
excelFile.excelFactFile = "Fact_" + order.factory + "_" + order._id + ".xls";
excelFile.excelFactFilePath = path.join("data", "excel", excelFile.excelFactFile);
excelFile.excelBrFile = "Br_" + order.factory + "_" + order._id + ".xls";
excelFile.excelBrFilePath = path.join("data", "excel", excelFile.excelBrFile);
excelFile.excelSuFile = "Su_" + order.factory + "_" + order._id + ".xls";
excelFile.excelSuFilePath = path.join("data", "excel", excelFile.excelSuFile);
let workbookFinal = createExcelFile(order, "C", excelFile.excelOffFilePath);
createExcelFile(order, "F", excelFile.excelFactFilePath);
createExcelFile(order, "B", excelFile.excelBrFilePath);
createExcelFile(order, "S", excelFile.excelSuFilePath);
sgMail.send({
to: config.get("mail.defaultAdd"),
from: defaultMailId,
subject: "Order with excel attachment send through software ",
html: `<h1>
The mail has been sent as trial through program for testing excel file attachement .
</h1>
<h3>
Office file - ${url.format({ protocol: req.protocol, host: req.get("host"), pathname: "orders/genExcelFile/" + excelFile.excelOffFile })}</br>
Fact file - ${url.format({ protocol: req.protocol, host: req.get("host"), pathname: "orders/genExcelFile/" + excelFile.excelFactFile })}</br>
Br file - ${url.format({ protocol: req.protocol, host: req.get("host"), pathname: "orders/genExcelFile/" + excelFile.excelBrFile })}</br>
Su file - ${url.format({ protocol: req.protocol, host: req.get("host"), pathname: "orders/genExcelFile/" + excelFile.excelSuFile })}</br>
</h3>
`,
attachments: [
{
content: fs.readFileSync(path.join(rootDir.rootDir, excelFile.excelOffFilePath), { encoding: "base64" }),
filename: excelFile.excelOffFile,
},
{
content: fs.readFileSync(excelFile.excelFactFilePath, { encoding: "base64" }),
filename: excelFile.excelFactFile,
},
{
content: fs.readFileSync(excelFile.excelBrFilePath, { encoding: "base64" }),
filename: excelFile.excelBrFile,
},
{
content: fs.readFileSync(excelFile.excelSuFilePath, { encoding: "base64" }),
filename: excelFile.excelSuFile,
}
]
}).then(result => {
console.log("mail sent successfully...");
}).catch(err => console.log(err));
res.setHeader("Content-Type", "application/vnd.ms-excel");
res.setHeader("Content-Disposition", "attachment;filename='" + excelFile.excelOffFile + "'");
workbookFinal.xlsx.write(res).then(() => {
res.end();
});
}).catch(err => console.log(err));
};
Hello @Elmer
Thanks for your suggestion, it helped me understanding the flow of my code which was puzzling me a lot.
I figure out my mistake and solved it. My code generated excel file with no records by the time mail was sent. So , I removed my mail code from the function and created a separate function and then called the function after the getExcelFile function. This way I was sure that the files were created and the file were attached correctly.
Once again thanks a lot for helping me focus on my code before raising any further questions.
With Best Regards,
Prasoon Kumar Jalan