I'm trying to send an email with an excel file attached, but when I open it an error pops up saying that the file is damaged. It also downloads the file (which I actually don't want to) and I can open that with no problem.
I'm using the js-xlsx
library to create the file and the nodemailer
library to send it.
Function in email.service.ts
:
sendEmail(values: any) {
let message: {from: string, to: string, subject: string, html: string, attachments: {}[] | null} = {
from: 'gestionepresenze@gmail.com',
to: values.email,
subject: values.oggetto,
html: this.storage.createTable(values.messaggio).outerHTML,
attachments: null
}
if (values.attach) {
message["attachments"] = [{
filename: values.fileName + '.xlsx',
content: this.excel.createExcel(values),
contentType: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
}]
}
return this.http.post('http://localhost:3000/send', message)
}
Function in excel.service.ts
:
createExcel(values: any) {
const fileName = values.fileName + '.xlsx';
const table = this.storage.createTable('');
const ws: XLSX.WorkSheet = XLSX.utils.table_to_sheet(table);
const wb: XLSX.WorkBook = XLSX.utils.book_new();
XLSX.utils.book_append_sheet(wb, ws, 'Presenze');
return XLSX.writeFile(wb, fileName);
}