0
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?

2
  • 1
    "not working" is not a proper problem description. If some code works on machine A but not on machine B it's most of the time not an issue with the code itself but with different configurations or environments. You get any errors? Commented Nov 6, 2024 at 21:06
  • For instance, if you transporter is using GMAIL read this nodemailer.com/usage/using-gmail Commented Nov 6, 2024 at 21:10

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.