node.js Nodemailer 对象数组到 CSV 文件作为电子邮件中的附件
node.js Nodemailer array of objects to CSV file as attachment in email
我正在 node.js 使用 nodemailer 模块发送电子邮件。我有一组对象,我试图将其转换为 CSV 文件并将其附加到电子邮件中。我能够成功发送电子邮件,但 CSV 附件未正确填充(显示为空白)。我的代码类似于以下内容
var nodemailer = require('nodemailer');
// create reusable transporter object using SMTP transport
var transporter = nodemailer.createTransport({
service: 'Gmail',
auth: {
user: 'gmail.user@gmail.com',
pass: 'userpass'
}
});
var data = [{age:24,name:"bob"},{age:35,name:"andy"},...];
var mailOptions = {
from: 'foo@blurdybloop.com', // sender address
to: 'bar@blurdybloop.com', //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);
});
我猜它无法读取对象数组,只能读取以逗号分隔的字符串?
您应该将附加 content
属性 指定为字符串。
NodeMailer 无法自行转换格式。所以你需要在发送之前将数据转换成CSV。使用像 to-csv.
这样的模块
您可以创建对象数组或数组数组,然后使用 convert-array-to-csv 将其转换为 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: 'foo@blurdybloop.com',
to: 'bar@blurdybloop.com',
subject: 'Hello',
text: 'Hello world',
html: '<b>Hello world</b>',
attachments: [{
filename: 'test.csv',
content: csvData // attaching csv in the content
}],
};
我正在 node.js 使用 nodemailer 模块发送电子邮件。我有一组对象,我试图将其转换为 CSV 文件并将其附加到电子邮件中。我能够成功发送电子邮件,但 CSV 附件未正确填充(显示为空白)。我的代码类似于以下内容
var nodemailer = require('nodemailer');
// create reusable transporter object using SMTP transport
var transporter = nodemailer.createTransport({
service: 'Gmail',
auth: {
user: 'gmail.user@gmail.com',
pass: 'userpass'
}
});
var data = [{age:24,name:"bob"},{age:35,name:"andy"},...];
var mailOptions = {
from: 'foo@blurdybloop.com', // sender address
to: 'bar@blurdybloop.com', //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);
});
我猜它无法读取对象数组,只能读取以逗号分隔的字符串?
您应该将附加 content
属性 指定为字符串。
NodeMailer 无法自行转换格式。所以你需要在发送之前将数据转换成CSV。使用像 to-csv.
这样的模块您可以创建对象数组或数组数组,然后使用 convert-array-to-csv 将其转换为 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: 'foo@blurdybloop.com',
to: 'bar@blurdybloop.com',
subject: 'Hello',
text: 'Hello world',
html: '<b>Hello world</b>',
attachments: [{
filename: 'test.csv',
content: csvData // attaching csv in the content
}],
};