sheetjs
sheetjs copied to clipboard
xlsx file as email attatchment
Hi, can you please provide me a example how i can send a generated xlsx file as a attachment via nodemailer?
My attempt:
exports.prepareMail = async (report, SheetData) => {
try {
let filename = 'report_123.xlsx';
let sheetname = 'report';
let workbook = xlsx.utils.book_new();
let worksheet = xlsx.utils.aoa_to_sheet(SheetData);
xlsx.utils.book_append_sheet(workbook, worksheet, sheetname);
let data = xlsx.write(workbook, { type: 'binary', bookType: 'xlsx', bookSST: false });
return { filename, data };
} catch (error) {
console.log('Failed to prepare Report Data: ' + error);
throw error;
}
};
Calling let MailData = this.prepareMail(report, SheetData);
if(MailData) {
this.sendMail(ReportConfig, MailData)
}
exports.sendMail = async (ReportConfig, MailData) => {
try {
let mailOptions = {
from: ReportConfig.sender_address,
to: ReportConfig.recipient_addresss,
subject: ReportConfig.subject,
text: ReportConfig.message,
attachments: [
{
filename: MailData.filename,
content: MailData.data
}
]
};
let Status = await transporter.sendMail(mailOptions);
return { Status };
} catch (error) {
console.log('Failed to send Report Data: ' + error);
throw error;
}
};
To generate a nodejs Buffer, use type "buffer":
let data = xlsx.write(workbook, { type: 'buffer', bookType: 'xlsx', bookSST: false });
Then follow the nodemailer instructions for attaching a buffer. If that part is causing issues, raise an issue with the project at https://github.com/nodemailer/nodemailer