[英]How to send angular 6 form without backend?
我正在嘗試創建一個角度為6的網站,並通過聯系表格發送電子郵件。
該網站部署在apache服務器上,沒有后端,只有Angular 6 html和javascript文件。
我的電子郵件地址上有maillet / mailgun帳戶,firebase帳戶和smtp訪問權限。
是否可以僅發送角度為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;
?>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.