简体   繁体   English

如何在angularjs中使用mailgun发送电子邮件

[英]How to send emails using mailgun in angularjs

I am using the following code to send emails in angularjs using the mailgun API.我正在使用以下代码使用 mailgun API 在 angularjs 中发送电子邮件。

.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我在我的代码中更改了以下内容

  1. YOUR_DOMAIN_HERE with with-my-domain YOUR_DOMAIN_HERE with -my-domain
  2. key-YOUR_API_KEY_HERE with with-my-api-key key-YOUR_API_KEY_HEREwith-my-api-key

You need to add the following to index.html您需要将以下内容添加到 index.html

<meta http-equiv="Content-Security-Policy" content="default-src *; style-src 'self' 'unsafe-inline'; script-src 'self' 'unsafe-inline' 'unsafe-eval'">

Also, you can add CORS extension to Google Chrome.此外,您可以将CORS 扩展程序添加到 Google Chrome。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM