-2

I want use my api to send e-mail in some cases,the service (infobip) docs show an example in JS but it don't work in my api with nodejs and expressjs. Can someone help me?

/*----Sending fully featured email----*/

function createFormData(data) {
    var formData = new FormData();
    for (var key in data) {
        formData.append(key, data[key]);
    }
    return formData;
}

//Dummy File Object
var file = new File([""], "filename");

var data = {
    'from': 'Sender Name <from@example.com>',
    'to': 'recipient1@example.com',
    'subject': 'Test Subject',
    'html': '<h1>Html body</h1><p>Rich HTML message body.</p>',
    'text': 'Sample Email Body',
    'attachment': file,
    'intermediateReport': 'true',
    'notifyUrl': 'https://www.example.com/email/advanced'
};


var xhr = new XMLHttpRequest();
xhr.withCredentials = false;

xhr.addEventListener('readystatechange', function () {
    if (this.readyState === this.DONE) {
        console.log(this.responseText);
    }
});

xhr.open('POST', 'https://{base_url}.infobip.com/email/1/send', false);
xhr.setRequestHeader('authorization', 'Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==');
xhr.setRequestHeader('accept', 'application/json');

xhr.send(createFormData(data));
msmolcic
  • 6,407
  • 8
  • 32
  • 56
rodrigofbm
  • 31
  • 1
  • 6

1 Answers1

0

You should use https from nodejs.

Here an example code to getting started. For infopib it seems to be so normal Post request.

I tried to create an account on this page, but registration seems to can be completed only over sales. So I couldn't provide a working example...

This is why I can only provide a general example how to make an https POST call, which should be a good starting point to develop your solution:

const https = require('https')

const data = JSON.stringify({
  todo: 'Buy the milk'
})

const options = {
  hostname: 'yourURL.com',
  port: 443,
  path: '/todos',
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
    'Content-Length': data.length
  }
}

const req = https.request(options, (res) => {
  console.log(`statusCode: ${res.statusCode}`)

  res.on('data', (d) => {
    process.stdout.write(d)
  })
})

req.on('error', (error) => {
  console.error(error)
})

req.write(data)
req.end()
  • Thanks! I change your code to this: https://pastebin.com/Z2kcWTd6 But i'm receiving: `statusCode: 400 {"requestError":{"serviceException":{"messageId":"BAD_REQUEST","text":"Bad request"}}}` – rodrigofbm Nov 30 '18 at 14:08
  • I hope you replaced the URL with a correct one and not using {base_url}.api.infobip.com directly, like in your pasebin example? – Christian Müller Nov 30 '18 at 14:11
  • I replace it with my url! – rodrigofbm Nov 30 '18 at 14:13
  • 1
    @ChristianMüller : 'experiance' != 'experience' && 'espacially' != 'especially' (in Deinem Profil..) ;) – iLuvLogix Nov 30 '18 at 14:25
  • Thank you iLuvLogix :) Next suggestion would be to check, if you using the correct basic authentication string. Your example from pastebin looks correct. After all I would like to point out to create a new question, since this is answered for your question what to use instead of "XMLHttpRequest" in nodejs? -> Which is use require("https") – Christian Müller Nov 30 '18 at 17:02