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
     }],
};

1 Answer 1

2

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
}
Sign up to request clarification or add additional context in comments.

3 Comments

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 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 how to read binary data with request
Thank you so much! The answer on that link got the attachment to work. This was a huge help, very much appreciated!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.