簡體   English   中英

如何在angularjs中使用mailgun發送電子郵件

[英]How to send emails using mailgun in angularjs

我正在使用以下代碼使用 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 -my-domain
  2. key-YOUR_API_KEY_HEREwith-my-api-key

您需要將以下內容添加到 index.html

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

此外,您可以將CORS 擴展程序添加到 Google Chrome。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM