hupakeetje
hupakeetje

Reputation: 13

Sending email from browser mailgun

I want to send a simple email from the browser using mailgun. I send a working email from postman and with the download code function I managed to get the javascript code which is:

var data = new FormData();
data.append("from", "Mailgun Sandbox <[email protected]>");
data.append("to", "Example <[email protected]>");
data.append("subject", "Hello from Mailgun");
data.append("text", "Congratulations Example, you just sent an email with Mailgun!  You are truly awesome!");

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

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

xhr.open("POST", "https://api:[email protected]/v3/sandbox1985406854ad9e8b8dfe094531fa41e8.mailgun.org/messages");
xhr.setRequestHeader("cache-control", "no-cache");
xhr.setRequestHeader("postman-token", "0a3ad9d5-22b5-6308-d6e7-59f66360fa26");

xhr.send(data);

But when I execute this code from my browser (firefox) it does nothing. What am I doing wrong?

Changing the url from

https://api:[email protected]/v3/sandbox1985406854ad9e8b8dfe094531fa41e8.mailgun.org/messages

to

https://api.mailgun.net/v3/sandbox1985406854ad9e8b8dfe094531fa41e8.mailgun.org/messages

seems to execute, is the api:key-xx part invalid?

Upvotes: 1

Views: 1435

Answers (1)

user149341
user149341

Reputation:

Don't do this. This is not an appropriate use of the Mailgun API.

By delivering this Javascript to a browser, you are giving them access to your Mailgun API key. This API key is not limited in any way whatsoever -- a malicious client could misuse the key to send an unlimited number of messages to any recipient they want, potentially racking up large bills and/or getting your account blocked for spam.

Use the Mailgun API on the server side. If you need to send messages via Javascript, create an endpoint in your application which calls the Mailgun API with appropriate restrictions in place.

Upvotes: 1

Related Questions