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 0
Add a ment  | 

2 Answers 2

Reset to default 6

You 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