2015-06-02 34 views
0

我试图调用一个php脚本来发送包含来自联系表单信息的邮件。表单验证通过后,我尝试调用代码获取500内部服务器错误。调用php脚本时内部服务器错误500

这就是我所说的脚本,它的工作原理。

var xmlHttp = new XMLHttpRequest(); 
//Check if the validation was passed. 
if($("#contact-form .error-message").size() === 0) { 

    xmlHttp.open("POST", "scripts/send_mail.php"); 
    xmlHttp.setRequestHeader("Content-type","application/x-www-form-urlencoded"); 
    xmlHttp.send("name="+name.value+"&email="+email.value+"&message="+message.value+"&g-recaptcha-response="+captcha); 
} 

这是剧本被称为

<?php 
require_once '../init.php'; 

$name = $_POST['name']; 
$email = $_POST['email']; 
$message = $_POST['message']; 
$captcha = $_POST['g-recaptcha-response']; 

$from = '[email protected]'; 
$to = '[email protected]'; 
$subject = 'Message from ' . $name; 
$body ="From: $name\n E-Mail: $email\n Message:\n $message"; 

//Verify the captcha by sending a POST-request to Google's server. 
$url = 'https://www.google.com/recaptcha/api/siteverify'; 
$data = array('secret' => 'xxx', 'response' => $captcha); 
$options = array(
'http' => array(
    'header' => "Content-type: application/x-www-form-urlencoded\r\n", 
    'method' => 'POST', 
    'content' => http_build_query($data), 
), 
); 
$context = stream_context_create($options); 
$result = file_get_contents($url, false, $context); 
$result = json_decode($result, true); 



//If the verification of the captcha was successful. 
if($result['success']) { 

mail($to, $subject, $body, $from) 

echo "Thank you! Your message has been received."; 
} 
else echo "Failed to submit, please try again."; 

有谁知道什么可能导致此?提前

+1

启用PHP的error_reporting,并给出精确的错误。 – jAC

+0

看看服务器的错误日志了解有关500的详细信息。哦,谢谢你的谷歌密钥。 –

+0

在整个代码中添加日志消息,查看服务器日志,用硬编码变量替换该帖子,并在浏览器中打开页面,或者至少说出你已经尝试了什么,并得到了什么结果... – BlunT

回答

1

感谢你需要就行添加结束分号:

mail($to, $subject, $body, $from) 

应该

mail($to, $subject, $body, $from); 
+0

哇,我是怎样的没有看到...非常感谢 – bryo

相关问题