2016-01-12 35 views
0

我无法使用ajax和php在我的共享服务器上发送邮件,服务器上的php邮件配置是否存在问题,我使用的是hostgator 我试过在我的本地Ubuntu的机器也但也同样的错误 以及我怎样才能得到这个错误 我得到了错误的更多细节无法在共享服务器上使用php发送邮件


Error in mail config

我的代码

<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-\.][email protected]([\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 = "[email protected]"; //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); 
} 
} 
?> 
+0

检查错误使用'的print_r($ send_mail);在''之前$ send_mail()'调用。让我知道什么是错误? –

+0

另外,PHP的默认邮件功能并不总是可靠的,你应该尝试phpmailer库 – user4804138

+0

使用PHPMailer,相信我。它快速学习,你可以避免你正在经历的所有bs。 http://phpmailer.worxware.com/?pg=tutorial。 – Naterade

回答

相关问题