5

I have a csv string that I am trying to send to as an attachment in an email but the content is coming out as gibberish. This is a node script. Any ideas?

                 // csv is a csv string

                 var message = {
                    "html": msg,
                    "subject": 'Test CSV Attachment',
                    "from_email": from,
                    "from_name": "Tester",
                    "to": [{
                            "email": email
                            }],
                    "headers": {
                        "Reply-To": email
                    },
                    "attachments": [{
                        "type": 'text/csv',
                        "name": filename,
                        "content": csv
                    }],
                  };

                    mandrill_client.messages.send({"message": message}, function(result) {
                    console.log('result NOTIFICATION! ', result);
                  });
user3527354
  • 586
  • 1
  • 7
  • 20

1 Answers1

7

According to the documentation of the Mnadrill API, you need to encode the content in base64:

enter image description here

So, modify the following ...

"content": csv

...to:

"content": Buffer.from(csv).toString('base64')
Nehal J Wani
  • 16,071
  • 3
  • 64
  • 89
  • worked! i previously tried csv.toString('base64') and that failed. Can you explain how that is different from what you did? – user3527354 Feb 25 '17 at 22:31
  • @user3527354 https://cloud.githubusercontent.com/assets/1779189/23337067/3d1756fe-fc08-11e6-90da-12f3920ca44b.png I think the difference is between calling `Object.prototype.toString()` v/s `buffer.toString()` . The former doesn't take any argument and even if you specify any garbage other than 'base64' it won't complain. But the latter takes at most three arguments: encoding, start and end – Nehal J Wani Feb 26 '17 at 04:22