admin管理员组文章数量:1278859
I'm sending an email in node.js using nodemailer module. I have an array of objects that I'm trying to turn into a CSV file and attach it to the email. I am able to successfully send an email, but the CSV attachment doesn't populate correctly (showing up as blank). My code looks similar to the following
var nodemailer = require('nodemailer');
// create reusable transporter object using SMTP transport
var transporter = nodemailer.createTransport({
service: 'Gmail',
auth: {
user: '[email protected]',
pass: 'userpass'
}
});
var data = [{age:24,name:"bob"},{age:35,name:"andy"},...];
var mailOptions = {
from: '[email protected]', // sender address
to: '[email protected]', //receiver
subject: 'Hello', // Subject line
text: 'Hello world', // plaintext body
html: '<b>Hello world</b>', // html body
attachments: [{
filename: 'test.csv',
content: data
}],
};
// send mail with defined transport object
transporter.sendMail(mailOptions, function(error, info){
if(error){
return console.log(error);
}
console.log('Message sent: ' + info.response);
});
I'm guessing it's not able to read the array of objects and only works for a string separated by mas?
I'm sending an email in node.js using nodemailer module. I have an array of objects that I'm trying to turn into a CSV file and attach it to the email. I am able to successfully send an email, but the CSV attachment doesn't populate correctly (showing up as blank). My code looks similar to the following
var nodemailer = require('nodemailer');
// create reusable transporter object using SMTP transport
var transporter = nodemailer.createTransport({
service: 'Gmail',
auth: {
user: '[email protected]',
pass: 'userpass'
}
});
var data = [{age:24,name:"bob"},{age:35,name:"andy"},...];
var mailOptions = {
from: '[email protected]', // sender address
to: '[email protected]', //receiver
subject: 'Hello', // Subject line
text: 'Hello world', // plaintext body
html: '<b>Hello world</b>', // html body
attachments: [{
filename: 'test.csv',
content: data
}],
};
// send mail with defined transport object
transporter.sendMail(mailOptions, function(error, info){
if(error){
return console.log(error);
}
console.log('Message sent: ' + info.response);
});
I'm guessing it's not able to read the array of objects and only works for a string separated by mas?
Share Improve this question edited Mar 3, 2018 at 14:15 Paul Rumkin 6,8832 gold badges28 silver badges36 bronze badges asked Jul 12, 2016 at 11:32 user6101759user6101759 02 Answers
Reset to default 6You should to specify attach content
property as a string.
NodeMailer couldn't convert formats itself. So you need to convert data into CSV before sending it. Use module like to-csv.
You can create an array of objects, or an array of arrays and use convert-array-to-csv to convert it to csv
const {convertArrayToCSV} = require('convert-array-to-csv');
var data = [{age:24,name:"bob"},{age:35,name:"andy"},...];
//Array of objects so dont need to pass the header (age, name)
const csvData = convertArrayToCSV(data);
//In case you have array of arrays
data= [[24,'bob'],[35,'andy']]
header= ['age','name']
const csvData = convertArrayToCSV(data, {
header,
separator: ','
});
var mailOptions = {
from: '[email protected]',
to: '[email protected]',
subject: 'Hello',
text: 'Hello world',
html: '<b>Hello world</b>',
attachments: [{
filename: 'test.csv',
content: csvData // attaching csv in the content
}],
};
本文标签: javascriptnodejs Nodemailer array of objects to CSV file as attachment in emailStack Overflow
版权声明:本文标题:javascript - node.js Nodemailer array of objects to CSV file as attachment in email - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741276659a2369767.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论