javascript - 如何在没有后端的情况下发送 Angular 6 表单?
问题描述
我正在尝试使用 angular 6 创建一个网站并从联系表发送电子邮件。
该网站部署在 apache 服务器上,没有后端,只有 Angular 6 html 和 javascript 文件。
我的电子邮件地址上有 maillet/mailgun 帐户、firebase 帐户和 smtp 访问权限。
是否可以仅使用 angular 6 发送电子邮件?我怎样才能做到这一点 ?
谢谢。:)
解决方案
你不能只使用 Angular 来做到这一点,这是一个使用 Angular 和一些 PHP 来发送电子邮件的示例:
HTML 表单:
<form [formGroup]="subscribeForm" novalidate (ngSubmit)="sendMail($event)">
<input type="text" placeholder="Enter your email" formControlName="email"/>
<button class="button margin-top-15" type="submit">Subscribe</button>
</form>
ts文件:
export class MyComponent implements OnInit, OnDestroy {
public subscribeForm: FormGroup;
public email: FormControl;
private unsubscribe = new Subject<void>();
constructor(private databaseService: DatabaseService, private mailerService: MailerService, private http: HttpClient) { }
ngOnInit() {
this.createFormControls();
this.createForm();
}
createFormControls() {
this.email = new FormControl('', [
Validators.required
]);
}
createForm() {
this.subscribeForm = new FormGroup({
email: this.email
});
}
sendMail() {
if (this.subscribeForm.valid) {
this.http.post("link to the php file.", email).subscribe();
}
}
ngOnDestroy(): void {
this.unsubscribe.next();
this.unsubscribe.complete();
}
}
并在 php 文件中:
<?php
header('Content-type: application/json');
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept");
$request = json_decode(file_get_contents("php://input"));
$from_email = "your email goes here";
$message = "Welcome.";
$from_name = "your name goes here";
$to_email = $request->email;
$contact = "<p><strong>Name:</strong>$from_name</p><p><strong>Email:</strong> $from_email</p>";
$email_subject = "Angular Php Email Example: Neue Nachricht von $from_name erhalten";
$email_body = '<html><body>';
$email_body .= "$<p><strong>Name:</strong>$from_name</p><p><strong>Email:</strong> $from_email</p>
<p>$message</p>";
$email_body .= '</body></html>';
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$headers .= "From: $from_email\n";
$headers .= "Reply-To: $from_email";
mail($to_email,$email_subject,$email_body,$headers);
$response_array['status'] = 'success';
$response_array['from'] = $from_email;
echo json_encode($response_array);
echo json_encode($from_email);
header($response_array);
return $from_email;
?>
推荐阅读
- c++ - typedef 名称与可变参数模板参数名称一致时的 GCC 错误
- python - Pyinstaller 转换的 exe 文件不在 Windows 7 上运行
- delphi - 如何使用 ZXing 找出 qrcode 方向?
- python - Numpy:加载 csv 文件并使用 genfromtxt 读取字符串值给出警告
- python - 我得到 django.db.utils.IntegrityError: UNIQUE constraint failed: auth_user.username 无明显原因
- django - Django 自定义用户函数
- swift - SwiftUI:如何实现 SwipGesture?
- node.js - 为了尽量减少 Firebase Cloud 函数的冷启动时间,如何导入一个类以在一个函数中使用?
- android - 无法在云控制台中启用 Maps SDK for Android
- python-3.x - 使用命令“ls -t | tail -n +4 | xargs rm --”删除另一个位置的文件