Search code examples
node.jsmandrill

How to attach a csv file to email using Mandrill?


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);
                  });

Solution

  • 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')