我想写一个 csv 文件,然后用 nodemailer 作为附件通过电子邮件发送它。我有这个代码: const csvWriter = createCsvWriter({ path: 'out.csv', header: [ {id: 'name', title: 'Name'}, {id: 'desc', title: 'Description'}, {id: 'image', title: 'Image'} ] }); csvWriter .writeRecords(allAds) .then(()=> console.log('The CSV file was written successfully'));如何将文件作为附件上传到 nodemailer?
1 回答

慕斯王
TA贡献1864条经验 获得超2个赞
根据文档,nodemailer 支持设置附件的不同方式。所以一种方法是:
csvWriter
.writeRecords(allAds)
.then(() => {
let message = {
// ... message details
attachments: [{
filename: 'csv-data.csv',
path: '/path/to/out.csv' // stream this file
}
};
// ... code for sending message
});
还有一件事 - 如果您不一定需要将 csv 文件写入文件(并且它不是太大),您可以createObjectCsvStringifier
从您的csv-library中使用并使用生成的字符串。这将加快速度,因为您不需要从文件中写入/读取。
添加回答
举报
0/150
提交
取消