0

I have a URL to an Excel file that I'm trying to add as an attachment to an email being sent out by Mandrill. Mandrill is expecting the attachment content in base64:

enter image description here

I'm trying to get the content of the Excel file, and add it to my Mandrill message, but it's not working. I need to use the native node request because I can't add a bunch of dependencies to the project as a whole.

I believe my problem is coming from my code not waiting for the request to finish, so the data variable is still blank when it's being passed to my message object. But I'm new to this, so I'm trying to figure out if there's a way to use "async/await" here to make sure my message object gets the data after the request is finished.

Any tips on what I should change here?

var https = require('https');

var data = '';
var request = https.request(myExcelFileURL, function (res) {
    res.on('data', function (chunk) {
        data += chunk;
    });
    res.on('end', function () {
        console.log(data);

    });
});
request.on('error', function (e) {
    console.log(e.message);
});
request.end();


// mandrill message
var message = {
   "html": msg,
   "subject": 'Test Excel Attachment',
   "from_email": from,
   "from_name": "Tester",
   "to": [{
          "email": email
    }],
    "headers": {
           "Reply-To": email
     },
    "attachments": [{
        "type": 'application/xlsx',
        "name": 'test.xlsx',
        "content": data.toString('base64') //this is coming up blank
     }],
};
Cineno28
  • 889
  • 1
  • 22
  • 41

1 Answers1

1

The request for getting the excel file is async but you are not waiting for it to finish. You must call your message sending in the end handler of the request

var data = '';
var request = https.request(myExcelFileURL, function (res) {
    res.on('data', function (chunk) {
        data += chunk;
    });
    res.on('end', function () {
        console.log(data);
        sendmail(data);
    });
});
request.on('error', function (e) {
    console.log(e.message);
});
request.end();



function sendmail (data){
  var message = {
   "html": msg,
   "subject": 'Test Excel Attachment',
   "from_email": from,
   "from_name": "Tester",
   "to": [{
          "email": email
    }],
    "headers": {
           "Reply-To": email
     },
    "attachments": [{
        "type": 'application/xlsx',
        "name": 'test.xlsx',
        "content": data.toString('base64')
     }],
  };

  //send the message
}
derpirscher
  • 14,418
  • 3
  • 18
  • 35
  • Thanks! This was a huge help. This seems to be working in order now, so I'll mark the answer as correct. The only issue now is that the Excel attachment seems to not be working. I get the email and the attachment, but when I go to open it it says it's an invalid format and won't open in Excel. Any ideas there? – Cineno28 Dec 05 '20 at 17:11
  • @Cineno28 The excel file is a binary format, but just having copied your code, `data` is a string, so probably some data is lost. See for instance [this question](https://stackoverflow.com/questions/17836438/getting-binary-content-in-node-js-with-http-request) how to read binary data with request – derpirscher Dec 05 '20 at 19:19
  • Thank you so much! The answer on that link got the attachment to work. This was a huge help, very much appreciated! – Cineno28 Dec 05 '20 at 20:01