[英]How can I write a csv file and send it with nodemailer? with nodejs
我想寫一個 csv 文件,然后用 nodemailer 作為附件的 email 它。 我有這個代碼:
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?
根據文檔,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 文件寫入文件(並且它不是太大),您可以使用csv-library中的createObjectCsvStringifier
並使用生成的字符串。 這將加快速度,因為您不需要從文件中寫入/讀取。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.