Integrating Mailgun with Ionic

独自空忆成欢 提交于 2019-12-22 01:29:59

问题


I want to send email without using email composer, so I followed tutorial from https://www.thepolyglotdeveloper.com/2016/05/send-emails-ionic-2-mobile-app-via-rackspace-mailgun-api/ to use Mailgun API. Since Http from "@angular/http" has been deprecated, the code from tutorial is not working anymore. Here is what I have so far:

I replaced

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

with

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

and send method is

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 is added to the parameter for the constructor. I also added

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

to app.motule.ts file. When I run, I get this error:

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"}

I also added CORS Chrome extension. What is the correct way to send email using Mailgun API on Ionic?


回答1:


When you use this.mailgunApiKey, what's its value? If you are using your Mailgun API key it's wrong. You should use:

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

Where username is 'api' and password is your API Key.



来源:https://stackoverflow.com/questions/49205132/integrating-mailgun-with-ionic

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!