Reputation: 149
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: "[email protected]&to=" + "[email protected]" + "&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?
Upvotes: 1
Views: 670
Reputation: 91
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.
Upvotes: 2