Tôi đang cố gắng gửi email từ biểu mẫu liên hệ trên trang của tôi, tôi đang sử dụng tập lệnh PHP để gửi email tới email của mình, tôi đang theo dõi this hướng dẫn, tôi đã sử dụng ví dụ của mình và nó hoạt động ... nhưng tôi không thể làm cho nó hoạt động trong ứng dụng của mình, lúc đầu tôi gặp sự cố với lỗi 404
nhưng sau đó tôi xuất bản trang web của mình và đặt nó trên máy chủ trực tiếp và bây giờ tôi nhận được mã thành côngEmail không gửi Angular 4 & PHP
nhưng im không nhận được email, vì vậy tôi đã đi đến http://mysite/assets/email.php và Im nhìn thấy lỗi này
get-in-touch.component.ts
import { Component, OnInit } from '@angular/core';
import { AppService, IMessage } from '../../services/email.service';
@Component({
selector: 'app-get-in-touch',
templateUrl: './get-in-touch.component.html',
styleUrls: ['./get-in-touch.component.scss'],
providers: [AppService]
})
export class GetInTouchComponent implements OnInit {
message: IMessage = {};
constructor(
private appService: AppService
) { }
ngOnInit() {
}
sendEmail(message: IMessage) {
this.appService.sendEmail(message).subscribe(res => {
console.log('AppComponent Success', res);
}, error => {
console.log('AppComponent Error', error);
});
}
}
app.module.ts
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { RouterModule, Routes, ActivatedRoute, ParamMap } from '@angular/router';
import { AppComponent } from './app.component';
import { GetInTouchComponent } from './get-in-touch/get-in-touch.component';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { httpModule } from @angular/forms;
export const ROUTES: Routes = [
{ path: 'get-in-touch', component: GetInTouchComponent }
];
@NgModule({
declarations: [
AppComponent,
GetInTouchComponent
],
imports: [
BrowserModule,
RouterModule.forRoot(ROUTES),
FormsModule,
HttpModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
email.service.ts
import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
import { Observable } from 'rxjs/Observable';
import { Resolve } from '@angular/router';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/catch';
import 'rxjs/add/observable/throw';
export interface IMessage {
name?: string;
email?: string;
message?: string;
}
@Injectable()
export class AppService {
private emailUrl = '../app/get-in-touch/email.php';
constructor(private http: Http) {
}
sendEmail(message: IMessage): Observable<IMessage> | any {
return this.http.post(this.emailUrl, message)
.map(response => {
console.log('Sending email was successfull', response);
return response;
})
.catch(error => {
console.log('Sending email got error', error);
return Observable.throw(error);
});
}
}
email.php
<?php
header('Content-type: application/json');
$errors = '';
if(empty($errors)){
$postdata = file_get_contents("php://input");
$request = json_decode($postdata);
$from_email = $request->email;
$message = $request->message;
$from_name = $request->name;
$to_email = $from_email;
$contact = "<p><strong>Name: </strong> $from_name</p><p><strong>Email:</strong> $from_email</p>";
$content = "<p>$message</p>";
$website = "Thirsty Studios";
$email_subject = "Contact Form";
$email_body = '<html><body>';
$email_body .= '$contact $content';
$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;
} else {
$response_array['status'] = 'error';
echo json_encode($response_array);
header('Location: /error.html');
}
?>
Tôi chưa bao giờ làm bất cứ điều gì như thế này trước (góc + PHP), nhưng tôi đã làm tất cả những gì được nói trong hướng dẫn và tôi dường như không thể có được nó để làm việc, bất kỳ trợ giúp nào sẽ được đánh giá cao và vui lòng cho tôi biết nếu bạn cần thêm thông tin
Bạn có thể kiểm tra xem bạn có thực sự truy cập URL này http: // localhost: 4200/app/get-in-touch/email.php trong trình duyệt của mình hay không, vì bạn gặp lỗi 404. – ksoni
Xin chào, tôi đã cố gắng truy cập URL đó và nó không hoạt động nên những gì tôi đã làm đã được chuyển email.php vào thư mục nội dung của tôi và tôi có thể truy cập vào nó .. nhưng tôi vẫn gặp lỗi tương tự tự hỏi nếu nó không hoạt động vì tôi đang trên localhost? – A61NN5
URL đầy đủ của trang nơi biểu mẫu được hiển thị là gì? – ksoni