I am using the following code to send emails in angularjs using the mailgun API.
.controller("MailgunController", function($scope, $http) { var mailgunUrl = "YOUR_DOMAIN_HERE"; var mailgunApiKey = window.btoa("api:key-YOUR_API_KEY_HERE") $scope.send = function() { $http({"method": "POST","url": "https://api.mailgun.net/v3/"+ mailgunUrl +"/messages","headers": {"Content-Type": "application/x-www-form-urlencoded","Authorization": "Basic "+ mailgunApiKey }, data: "from="+"test@example.com"+"&to="+"soeone@gmail.com"+"&subject="+"MailgunTest"+"&text="+"EmailBody" }).then(function(success) { console.log("SUCCESS "+ JSON.stringify(success)); }, function(error) { console.log("ERROR "+ JSON.stringify(error)); }); }})
But I am getting the following error!
XMLHttpRequest cannot load https://api.mailgun.net/v3/MY-URL/messages. Request header field Authorization is not allowed by Access-Control-Allow-Headers in preflight response.
I had changed the following things in my code
- YOUR_DOMAIN_HERE with with-my-domain
- key-YOUR_API_KEY_HERE with with-my-api-key