将 Mailgun 与 Ionic 集成

Integrating Mailgun with Ionic

我想在不使用电子邮件编辑器的情况下发送电子邮件,所以我按照 https://www.thepolyglotdeveloper.com/2016/05/send-emails-ionic-2-mobile-app-via-rackspace-mailgun-api/ 中的教程使用 Mailgun API。由于来自“@angular/http”的 Http 已被弃用,教程中的代码不再有效。这是我目前所拥有的:

我换了

import {Http, Request, RequestMethod} from "@angular/http";

import {HttpClient, HttpHeaders} from '@angular/common/http';

发送方式为

send() {
    this.http.post(this.mailgunUrl + "/messages",
    body: "from=test@example.com&to=" + "recipient@example.com" + "&subject=" + "test subject" + "&text=" + "test message sent", 
    {
        headers: {'Authorization': 'Basic ' + this.mailgunApiKey}
    }).subscribe(success => {
        console.log("SUCCESS -> " + JSON.stringify(success));
    }, error => {
        console.log("ERROR -> " + JSON.stringify(error));
    });
}

http: HttpClient 被添加到构造函数的参数中。我还添加了

import { HttpClientModule } from '@angular/common/http';

到app.motule.ts 文件。 当我 运行 时,出现此错误:

POST https://api.mailgun.net/v3/mydomainthaticopiedfrommailgunwebsite.mailgun.org/messages 401 (UNAUTHORIZED)

ERROR -> {"headers":{"normalizedNames":{},"lazyUpdate":null},"status":401,"statusText":"UNAUTHORIZED","url":"https://api.mailgun.net/v3/mydomainthaticopiedfrommailgunwebsite.mailgun.org/messages","ok":false,"name":"HttpErrorResponse","message":"Http failure response for https://api.mailgun.net/v3/mydomainthaticopiedfrommailgunwebsite.mailgun.org/messages: 401 UNAUTHORIZED","error":"Forbidden"}

我还添加了 CORS Chrome 扩展。在 Ionic 上使用 Mailgun API 发送电子邮件的正确方法是什么?

当你使用this.mailgunApiKey时,它的值是多少?如果您使用的是 Mailgun API 密钥,那就错了。你应该使用:

"Authorization", "Basic " + btoa("username:password")

其中用户名是 'api',密码是您的 API 密钥。