如何使用Angular 7发送电子邮件

Mok*_*oru 4 email scripting typescript angular

我现在正在使用Angular7,开始开发应用程序,我想知道如何从angular7应用程序发送电子邮件。请分享示例代码

小智 17

您可以使用一些 BaaS(后端即服务)提供商,例如Formspree。它使用起来非常简单,使您无需编写或设置后端即可发送电子邮件。您所要做的就是从您的 Angular 应用程序发布一个 Http 请求,该服务会负责其余的工作。

  1. 在 formspree.io 创建一个帐户
  2. 单击“+新建表单”按钮
  3. 输入要接收电子邮件的电子邮件

然后,您将获得这个新创建的表单的唯一端点,您可以使用该端点从 angular 应用程序发送(发布)电子邮件。端点将如下所示:https://formspree.io/asdlf7asdf

下面是一些使用模板驱动表单的示例代码:

html表单

<form (ngSubmit)="onSubmit(contactForm)" #contactForm="ngForm">
    <input type="text" placeholder="Name" name="name" ngModel required #name="ngModel">
    <input type="text" placeholder="Email" email name="email" ngModel required #email="ngModel">
    <textarea placeholder="Messages" name="messages" ngModel required #messages="ngModel"></textarea>
    <input type="submit" value="Send">
</form>
Run Code Online (Sandbox Code Playgroud)

背后的代码

 onSubmit(contactForm: NgForm) {
    if (contactForm.valid) {
      const email = contactForm.value;
      const headers = new HttpHeaders({ 'Content-Type': 'application/json' });
      this.http.post('https://formspree.io/asdlf7asdf',
        { name: email.name, replyto: email.email, message: email.messages },
        { 'headers': headers }).subscribe(
          response => {
            console.log(response);
          }
        );
    }
  }
Run Code Online (Sandbox Code Playgroud)

你可以用这个(或其他)服务做更多的事情,但这将是它的基本要点。


Dha*_*val 6

Angular在客户端工作并且要发送电子邮件,您需要服务器,因此,我认为现在不可能仅使用需要使用服务器端应用程序发送电子邮件的角度来发送电子邮件。