<分区>
此帖编辑提交审核1年前 重新打开失败帖子:
原始关闭原因未解决
如何在 Angular 7 中创建包含发送电子邮件方法的电子邮件服务?
例如:
// email.service.ts
import { Injectable } from ‘@angular/core’;
@Injectable()
export class EmailService {
constructor() { }
// method to fetch data from server
public sendEmail(): void {
// logic to send email
...
}
}
我刚开始使用 Angular 进行开发,因此非常感谢任何示例、用例和/或代码片段。
您可以使用一些 BaaS(后端即服务)提供商,例如 Formspree .它使用起来非常简单,使您无需编写或设置后端即可发送电子邮件。您所要做的就是从您的 Angular 应用程序发布一个 Http 请求,其余的由该服务负责。
在 formspree.io 创建一个帐户
点击“+新建表单”按钮
输入您希望接收电子邮件的电子邮件地址
然后您将为这个新创建的表单获得一个唯一的端点,您可以使用它从您的 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>
背后的代码
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);
}
);
}
}
您可以使用此(或其他)服务做更多事情,但这将是它的基本要点。