2016-01-12 75 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

回答

相關問題