PHP 邮件程序 + angular4 - 404 找不到问题



我正在尝试使用 Angular 服务做一个 PHPform 发送电子邮件,数据正在检索,但服务器返回 404,因为我是 PHP 新手,我找不到我缺少的东西。有人可以帮助我吗?

我一直在寻找angular4 + phpmailer并遵循了一些教程,实际上我不认为这是phpcode本身。

服务业:

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';
export interface IMessage {
nome?: string,
email?: string,
assunto?:string, 
mensagem?: string
}
@Injectable()
export class AppService {
private emailUrl = '../../../api/sendemail.php';
constructor(private http: Http) {
}
sendEmail(message: IMessage): Observable<IMessage> | any {
return this.http.post(this.emailUrl, message)
.map(response => {
console.log('Email enviado com sucesso', response);
return response;
})
.catch(error => {
console.log('Houve uma falha na solicitação de serviço', error);
return Observable.throw(error)
})
}
}

Contact.component.ts

import { Component, OnInit } from '@angular/core';
import { AppService, IMessage } from '../app.service';
@Component({
selector: 'app-contact',
templateUrl: './contact.component.html',
styleUrls: ['./contact.component.scss'],
providers: [AppService]
})
export class ContactComponent implements OnInit {
message: IMessage = {};
constructor(private appService: AppService) { }

sendEmail(message:IMessage){
this.appService.sendEmail(message).subscribe(res => {
}, error => {
console.log('ContactComponent Error', error);
});
}
ngOnInit() {
function Toggle(){
var faq = document.querySelectorAll('.faq-box');
for(var i = 0 ; i < faq.length ; i++ ){
faq[i].addEventListener('click',function(){
this.classList.toggle('active');
})
}
}
Toggle();
}
}

.PHP

<?php
use PHPMailerPHPMailerPHPMailer;
use PHPMailerPHPMailerException;
require 'includes/phpmailer/src/Exception.php';
require 'includes/phpmailer/src/PHPMailer.php';
require 'includes/phpmailer/src/SMTP.php';

if( !isset( $_POST['nome'] ) || !isset( $_POST['email'] ) || !isset( $_POST['mensagem'] ) || !isset( $_POST['assunto'] ) ) {
}
$pnome = $_POST['nome'];
$pemail = $_POST['email'];
$passunto = $_POST['assunto'];
$pmsg = $_POST['mensagem'];
$to = "contato@corpomaisleve.com.br";
$bcc = "";
$subject = 'Formulário de Contato Corpo Leve - ' . $pnome;
$body = $pmsg;
$body = "
Nome: $pnome
<br>assunto: $passunto
<br>
<br>Mensagem:
<p>
$pmsg
</p>
";
//PHPMailer Object
$mail = new PHPMailer;
//From email address and name
$mail->From = $pemail;
$mail->FromName = $pnome;
//To address and name
$mail->addAddress($to);
//$mail->addAddress("recepient1@example.com"); //Recipient name is optional
//Address to which recipient will reply
$mail->addReplyTo($pemail);
//CC and BCC
// $mail->addCC("cc@example.com");
$mail->addBCC($bcc);
//Send HTML or Plain Text email
$mail->isHTML(true);
$mail->Subject = $subject;
$mail->Body = $body;
$mail->AltBody = $body;
$retorno;
if(!$mail->send()) 
{
$retorno = "Problema";
echo ":" . $mail->ErrorInfo;
} 
else 
{
$retorno = "E-mail enviado com sucesso. Você será redirecionado em 3 segundos";
header( "refresh:3;url=index.php" );
}
/*
$to = "edgar@bulbcriacoes.com.br";
$subject = 'Formulário de Contato B&F Assessoria - ' . $pnome;
$body = $pmsg;
$body = `
Nome: $pnome
<br>assunto: $passunto
<br>
<br>Mensagem:
<p>
$pmsg
</p>
`;
$headers = 'From: ' . $pnome ."rn";
$headers .= 'Reply-To: ' . $pemail ."rn";
$headers .= 'Return-Path: ' . $pemail ."rn";
$headers .= 'X-Mailer: PHP5'."n";
$headers .= 'MIME-Version: 1.0'."n";
$headers .= 'Content-type: text/html; charset=iso-8859-1'."rn";
$retorno;
try {
if( mail($to, $subject, $body, $headers) ) {
header( "refresh:5;url=wherever.php" );
$retorno =  "E-mail Enviado com sucesso. Em 3 segundos você será redirecionado, obrigado por entrar em contato!";  
}
} catch( Exception $e ) {
print_r( "Problema:" . e );
}*/
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Corpo Leve - E-mail enviado</title>
<body>
<?php
echo $retorno;
?>
</body>
</html>

您必须运行Web服务器才能运行PHP脚本。WAMP XAMP 或任何网络服务器。

private emailUrl = '../../../api/sendemail.php';

这里的问题出在网址上。emailURL 在角度位置找到 PHP 脚本,所以它给你 404。

您需要在 Web 服务器中部署 PHP 文件,然后可以通过 URL 访问它。

示例:http://localhost/phpmailer/api/sendemail.php

希望这有帮助。

最新更新