【问题标题】:How to send emails using mailgun in angularjs如何在angularjs中使用mailgun发送电子邮件
【发布时间】:2016-09-05 23:29:12
【问题描述】:

我正在使用以下代码使用 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));
    });
  }

})

但我收到以下错误!

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.

我在代码中更改了以下内容

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

【问题讨论】:

    标签: javascript angularjs rest email mailgun


    【解决方案1】:

    需要在 index.html 中添加以下内容

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

    此外,您可以将CORS extension 添加到 Google Chrome。

    【讨论】:

    猜你喜欢
    • 2015-06-20
    • 1970-01-01
    • 1970-01-01
    • 2017-10-12
    • 2021-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-24
    • 2014-04-14
    相关资源
    最近更新 更多