我无法使用ajax和php在我的共享服务器上发送邮件,服务器上的php邮件配置是否有问题,我使用hostgator 我已经在我的本地ubuntu机器上尝试了但也有同样的错误 以及如何获得有关此错误的更多详细信息 我得到的错误是
我的代码
<script type="text/javascript">
$(document).ready(function () {
$("#submit_btn").click(function () {
var proceed = true;
$("#contact_form input[required=true], #contact_form textarea[required=true]").each(function () {
$(this).css('border-color', '');
if (!$.trim($(this).val())) { //if this field is empty
$(this).css('border-color', 'red'); //change border color to red
proceed = false; //set do not proceed flag
}
var email_reg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
if ($(this).attr("type") == "email" && !email_reg.test($.trim($(this).val()))) {
$(this).css('border-color', 'red'); //change border color to red
proceed = false; //set do not proceed flag
}
});
if (proceed) //everything looks good! proceed...
{
post_data = {
'user_name': $('input[name=name]').val(),
'user_email': $('input[name=email]').val(),
'msg': $('textarea[name=message]').val()
};
$.post('contact/contact.php', post_data, function (response) {
if (response.type == 'error') { //load json data from server and output message
output = '<div class="error">' + response.text + '</div>';
} else {
output = '<div class="success">' + response.text + '</div>';
//reset values in all input fields
$("#contact_form input[required=true], #contact_form textarea[required=true]").val("");
$("#contact_form #contact_body").slideUp(); //hide form after success
window.setTimeout(function () { location.reload() }, 3000);
}
$("#contact_form #contact_results").hide().html(output).slideDown();
}, 'json');
}
});
$("#contact_form input[required=true], #contact_form textarea[required=true]").keyup(function () {
$(this).css('border-color', '');
$("#contact_results").slideUp();
});
});
</script>
和我的PHP代码
<?php
if($_POST)
{$to_email = "info@example.com"; //Recipient email, Replace with own email here
//check if its an ajax request, exit if not
if(!isset($_SERVER['HTTP_X_REQUESTED_WITH']) AND strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) != 'xmlhttprequest') {
$output = json_encode(array( //create JSON data
'type'=>'error',
'text' => 'Sorry Request must be Ajax POST'
));
die($output); //exit script outputting json data
}
//Sanitize input data using PHP filter_var().
$user_name = filter_var($_POST["user_name"], FILTER_SANITIZE_STRING);
$user_email = filter_var($_POST["user_email"], FILTER_SANITIZE_EMAIL);
$message = filter_var($_POST["msg"], FILTER_SANITIZE_STRING);
//additional php validation
if(strlen($user_name)<4){ // If length is less than 4 it will output JSON error.
$output = json_encode(array('type'=>'error', 'text' => 'Name is too short or empty!'));
die($output);
}
if(!filter_var($user_email, FILTER_VALIDATE_EMAIL)){ //email validation
$output = json_encode(array('type'=>'error', 'text' => 'Please enter a valid email!'));
die($output);
}
if(strlen($message)<3){ //check emtpy message
$output = json_encode(array('type'=>'error', 'text' => 'Too short message! Please enter something.'));
die($output);
}
//email body
$message_body = $message."\r\n\r\n-".$user_name."\r\nEmail : ".$user_email."\r\nEmail : ". $user_email;
$subject = "Mail From website";
//proceed with PHP email.
$headers = 'From: '.$user_name.'' . "\r\n" .
'Reply-To: '.$user_email.'' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
$send_mail = mail($to_email, $subject, $message_body, $headers);
if(!$send_mail)
{
//If mail couldn't be sent output error. Check your PHP email configuration (if it ever happens)
$output = json_encode(array('type'=>'error', 'text' => 'Could not send mail! Please check your PHP mail configuration.'));
die($output);
}else{
$output = json_encode(array('type'=>'message', 'text' => 'Hi '.$user_name .' Thank you for your email'));
die($output);
}
}
?>