Reputation: 41
I want to write a csv file, and then email it with nodemailer as an attachment. I have this code:
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'));
how can I upload the file as an attachment to nodemailer?
Upvotes: 0
Views: 1266
Reputation: 24565
According to the docs, nodemailer supports different ways of setting attachments. So one way to do it is:
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
});
One more thing - if you don't necessarily need to write the csv file to a file (and it's not too big) you can just use createObjectCsvStringifier
from your csv-library and use the resulting string. This will speed things up as you don't need to write/read from a file.
Upvotes: 2