Ty *_*abs 8 email node.js firebase angular
我在firebase上托管了一个Angular 2应用程序.我想发一封联系表格作为电子邮件.理想情况下,我的解决方案将使用Nodejs,但我愿意使用能够正确完成工作的任何东西.以下是我的应用的细分.
这是我的表格:
<!-- contact-form.component.html -->
<form [formGroup]="formService.contactForm" (ngSubmit)="formService.onSubmitForm()">
<input type="text" formControlName="userFirstName">
<label>First Name</label>
<input type="text" formControlName="userLastName">
<label>Last Name</label>
<button type="submit">SUBMIT</button>
</form>Run Code Online (Sandbox Code Playgroud)
这是我的联系表单组件:
// contact-form.component.ts
import { Component } from '@angular/core';
import { ContactFormService } from './contact-form.service';
@Component({
selector: 'contact-form',
templateUrl: './contact-form.component.html',
styleUrls: ['./contact-content.component.css'],
providers: [ContactFormService]
})
export class ContactFormComponent {
constructor(private formService: ContactFormService) {
formService.buildForm();
}
}Run Code Online (Sandbox Code Playgroud)
这是我的联系表格服务:
// contact-form.service.ts
import { Injectable } from '@angular/core';
import { FormGroup, FormBuilder, FormControl, Validators } from '@angular/forms';
@Injectable()
export class ContactFormService {
constructor(public formBuilder: FormBuilder) { }
contactForm: FormGroup;
formSubmitted: boolean = false;
buildForm() {
this.contactForm = this.formBuilder.group({
userFirstName: this.formBuilder.control(null, Validators.required),
userLastName: this.formBuilder.control(null, Validators.required)
});
}
onSubmitForm() {
console.log(this.contactForm.value);
this.formSubmitted = true;
this.contactForm.reset();
}
}Run Code Online (Sandbox Code Playgroud)
单击"提交"按钮时,表单数据将在控制台中成功显示.
我可以使用SendGrid和Nodejs从命令提示符成功发送电子邮件:
示例:sendmail.js
var Sendgrid = require('sendgrid')(
process.env.SENDGRID_API_KEY || '<my-api-key-placed-here>'
);
var request = Sendgrid.emptyRequest({
method: 'POST',
path: '/v3/mail/send',
body: {
personalizations: [{
to: [{ email: 'my.email@gmail.com' }],
subject: 'Sendgrid test email from Node.js'
}],
from: { email: 'noreply@email-app.firebaseapp.com' },
content: [{
type: 'text/plain',
value: 'Hello Joe! Can you hear me Joe?.'
}]
}
});
Sendgrid.API(request, function (error, response) {
if (error) {
console.log('Mail not sent; see error message below.');
} else {
console.log('Mail sent successfully!');
}
console.log(response);
});Run Code Online (Sandbox Code Playgroud)
如果我在命令提示符下键入此邮件,则会成功发送电子邮件:
node sendmail
Run Code Online (Sandbox Code Playgroud)
但是,我无法弄清楚如何将我提交的表单数据链接到sendmail.js,而且我也无法弄清楚如何通过单击提交按钮来激活sendmail.js中的代码.
任何帮助将不胜感激.谢谢你的时间!
编辑:我刚刚看到您正在 Firebase 上提供服务,我将研究这将如何改变事情。
Angular 2 是客户端,如果你想使用你的秘密进行 API 调用,你可能应该在服务器端(又名 node.js 或任何你的服务器)进行它。
因为您有sendmail.js一个脚本,所以请考虑使用 node.js 为您的 Angular 2 应用程序提供服务,并使用 Express 提供 API 端点,就像/api/sendMail您可以从 Angular 2 应用程序发出 XHR/AJAX 请求一样。
| 归档时间: |
|
| 查看次数: |
20352 次 |
| 最近记录: |