我正在尝试使用 jquery 和 php 向自己发送电子邮件我希望消息的内容由 jquery 决定这是我的发送代码
$.ajax({
url: 'http://mywebpage.com/email.php',
type: 'POST',
contentType: 'application/json; charset=utf-8',
data:'test',
dataType: "text",
success: function(data){
console.log(data);
console.log("IT WORKED");
},
failure: function(result){
console.log("FAILED");
console.log(result);
}
});
这是我的 php 脚本
<?php
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: POST, GET, OPTIONS');
header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept");
$to = "myemail@gmail.com";
$subject = "Test mail";
$message = $_POST["data"];
echo var_dump($_POST);
echo $message;
$from = "someonelse@example.com";
$headers = "From:" . $from;
mail($to,$subject,$message,$headers);
echo "Mail Sent.";
?>
电子邮件已发送,但 $_POST["data"] 总是返回空怎么办?