const sendEmail = async () => {
try {
for (const notification of emailNotification) {
const excelBuffer = await createExcelFile(notification?.task);
const fileName = 'Monthly_Report.xlsx';
const contentType = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';
await sendMonthlyReportEmail(notification?.username, notification?.email, notification?.subject, notification?.message, fileName, excelBuffer, contentType);
}
emailNotification = [];
} catch (error) {
console.log(error.message);
}
}
const createExcelFile = async (data) => {
try {
const allKeys = [...new Set(data.flatMap(Object.keys))];
const headerKeys = allKeys.map(convertCamelCaseToUpper);
const workbook = new ExcelJS.Workbook();
const worksheet = workbook.addWorksheet('Sheet1');
const headerRow = worksheet.addRow(headerKeys);
headerRow.eachCell((cell) => {
cell.font = styleCellFont();
cell.alignment = styleCellAlignment();
cell.fill = styleCellBackground();
});
worksheet.columns = allKeys.map(() => ({ width: 20 }));
data.forEach(item => {
const row = allKeys.map(key => item[key]);
worksheet.addRow(row);
});
const buffer = await workbook.xlsx.writeBuffer();
return buffer;
} catch (error) {
throw new Error(error.message);
}
}
exports.sendMonthlyReportEmail = async(username, email, subject, message, fileName, excelBuffer, contentType) => {
try {
let payload = {
username,
subject,
companyName: "XYZ",
message,
}
const template = getFormattedTemplateById(6, payload);
return sendAttachmentEmail(email, template.subject, template.message, fileName, excelBuffer, contentType)
} catch (error) {
throw new Error(error.message);
}
}
exports.sendAttachmentEmail = async (senderEmail, subject, body, fileName, excelBuffer, contentType) => {
try {
return transporter.sendMail({
from: EMAIL,
to: senderEmail,
subject: subject,
html: body,
attachments: [
{
filename: fileName,
content: excelBuffer,
contentType: contentType
}
]
})
} catch (error) {
throw new Error(error.message);
}
}
i'm generating the excel sheet data from the database converting it into excel file using exceljs and then sending it to the user email using nodemailer. the problem i'm encountring is that this code is working fine on my local but not on the server is their anything wrong that i'm doing? what should i do?