将Mailgun与Ionic集成

时间:2018-03-10 03:50:33

标签: angularjs email http ionic-framework mailgun

我想在不使用电子邮件编辑器的情况下发送电子邮件,因此我按照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发送电子邮件的正确方法是什么?

1 个答案:

答案 0 :(得分:2)

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

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

用户名是' api'和密码是您的API密钥。