角4 mailgun

问题描述 投票:0回答:1

我试图做一个简单的电子邮件的形式对我的网站,允许人们与我联系的一个。该网站所使用的角度4,并且mailgun作为邮件服务。在我的邮件服务文件我有这个方法发送邮件,但我得到一个错误的请求错误说来自不存在。

 public sendMail(){
        let url = 'https://api.mailgun.net/v3/XXXXXXXXXXXX.mailgun.org/messages';
        let headers: Headers = new Headers();
        headers.append('Authorization','Basic '+  btoa('api:key-XXXXXXXXXXXXXXXXXXX'));
        headers.append("Content-Type", "application/x-www-form-urlencoded");
        let opts: RequestOptions = new RequestOptions();
        opts.headers = headers;
        this.http.post(url, 
            {
                from: '"Mailgun Sandbox" <[email protected]>', 
                to: "Test <[email protected]>",
                subject: 'Hello ',
                text: 'Congratulations, you just sent an email with Mailgun!  You are truly awesome!'
            },
            opts
        ).subscribe(
            success => {
                console.log("SUCCESS -> " + JSON.stringify(success));
            }, error => {
                console.log("ERROR -> " + JSON.stringify(error));
            }
        );
    }

我有一个很难理解为什么从被显示出来还不如现在,当我发送请求。任何帮助是巨大的。

javascript angular mailgun
1个回答
0
投票
import { Injectable } from '@angular/core';
import { HttpHeaders, HttpClient } from '@angular/common/http';

@Injectable({
  providedIn: 'root'
})

export class PostService {

  constructor(private http: HttpClient) {
  }

  sendMail() {
    const headers = new HttpHeaders({
      'enctype': 'multipart/form-data',
      'Authorization': 'Basic ' + btoa('api:xxxxxxxxxxxxxxxxxxxxxxxxxx-xxxxxx-xxxxxx')
    });

    const formData = new FormData();
    formData.append('from', 'Mailgun Sandbox <[email protected]>');
    formData.append('to', 'xxxxxxxxxxxxxx.com');
    formData.append('subject', 'Hello');
    formData.append('text', 'This is cool !');

    this.http
      .post(
        'https://api.mailgun.net/v3/sandboxxxxxxxxxxxxxxxxxxxxxxxxxxb.mailgun.org/messages',
        formData,
        { headers }
      ).subscribe(
        res => { console.log('res : ', res); },
        err => { console.log('err : ', err); }
      );
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.