简体   繁体   English

带mailgun的Angular 4

[英]Angular 4 with mailgun

I am trying to make a simple email form for one of my websites that allows people to contact me. 我正在尝试为我的一个网站制作一个简单的电子邮件表格,允许人们与我联系。 This site is using angular 4, and mailgun as the mail service. 该站点使用angular 4,并将mailgun作为邮件服务。 In my mail service file I have this method that sends the message, but I am getting a Bad Request error saying from is not present. 在我的邮件服务文件中,我有这种发送消息的方法,但是我收到一个Bad Request错误消息,提示from不存在。

  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" <postmaster@XXXXXXXXXX.mailgun.org>', to: "Test <test@gmail.com>", 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)); } ); } 

I am having a hard time understanding why from is showing up not as present when I send the request. 我很难理解为什么在发送请求时from不会显示为当前状态。 Any help is great. 任何帮助都很棒。

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 <postmaster@sandboxxxxxxxxxxxxxx.mailgun.org>');
    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); }
      );
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM