madebymt
madebymt

Reputation: 367

parse data to CSV send email use nodeMailer Node.JS

I've been looking nodeMailer documentation about attachment, the issue I have I need send a string of data like below, I need to convert them to CSV then send it through nodeMailer.

Saw another post of an array to CSV, mention I need to parse before I send it, so I tried to use papaparse the data and pass in their custom attachment, but I will get an error.

Set up in Node

const attachment = 
    ""test1,","test2,","test3,","test4,"" 
const parseInfo = papaparse.parse(attachment, {delimiter: ",", newline: ""})

const options = {
  to: toAddress,
  from: fromAddress,
  subject: subject,
  text: text,
  html: html,
  attachments:{ // define custom content type for the attachment
    filename: 'sample.csv',
    content: parseData,
    contentType: 'text/csv'
  },
};

Error message

An error occured while sending the email ---:TypeError [ERR_INVALID_ARG_TYPE]: The "chunk" argument must be one of type string or Buffer. Received type object

Do I miss anything? Thanks for the help!

Upvotes: 0

Views: 1447

Answers (1)

madebymt
madebymt

Reputation: 367

My data is already a string, so I don't need parse date, I directly pass in as content, then it' works. Got to make sure file name with .csv and contentType: 'text/csv'. I hope that helps someone have the same issue as me.

const attachment = 
    `"test1,","test2,","test3,","test4"`
const options = {
  to: toAddress,
  from: fromAddress,
  subject: subject,
  text: text,
  html: html,
  attachments:{ // define custom content type for the attachment
    filename: 'sample.csv',
    content: parseData,
    contentType: 'text/csv'
  },
};

Upvotes: 1

Related Questions