我在使用PHP发送电子邮件时使用了Angular $ http帖子但是当我尝试获取控制台日志时,我获得了 index.html 内容。我正在使用ng-view渲染 contact_us.html
的html模板Controller.js
..
$scope.sendEmail = function(){
var request = $http({
method: "post",
url: window.location.href + "MailerProcess.php",
data: {
name: $scope.contactInput.name,
email: $scope.contactInput.email,
message: $scope.contactInput.message
},
headers: { 'Content-Type': 'application/x-www-form-urlencoded'}
});
request.success(function(data){
console.log(data);
})
.error(function(data){
console.log(data);
});
$scope.contactInput = angular.copy($scope.defaultForm);
$scope.contactForm.$setPristine();
};
..
MailerProcess.php
<?php
# Data from angular POST
$postdata = file_get_contents("php://input");
$request = json_decode($postdata);
@$name = $request->name;
@$email = $request->email;
@$message = $request->message;
$myemail = "someemail@gmail.com";
$name = strip_tags($name);
$email = strip_tags($email);
$message = strip_tags($message);
if ( $name && $email && $message) {
if($email!="" AND !filter_var($email, FILTER_VALIDATE_EMAIL)){
$data = array(
"message" => "Please enter a valid email address.",
"type" => "error"
);
echo "Please enter a valid email address.";
}else {
$message = "
Someone has sent you a message from Some Website:
Name: $name
Email: $email
Subject: Email from Some Website
Message:
$message
";
mail($myemail,"Email from Website",$message);
$data = array(
"message" => "Your message was sent, Thank You!",
"type" => "success"
);
echo "Your message was sent, Thank you!";
}
}else{
$data = array(
"message" => "There was an error on the system, please try again later.",
"type" => "error"
);
echo "There was an error on the system, please try again later.";
}
?>