我第一次尝试将recaptcha添加到联系表单中,我已经取得了一些进展,但现在我有点卡住了。
它不允许我发送任何电子邮件,我找不到原因。在我遇到问题之前,recaptcha并没有停止错误的答案,但是在我将recaptcha代码放在表单顶部后,它现在显示自己并保护表单。
这是代码:
<?php
require_once('recaptchalib.php');
$privatekey = "xxxxxxxxxxxxxxxxxxxxxx";
$resp = recaptcha_check_answer ($privatekey,
$_SERVER["REMOTE_ADDR"],
$_POST["recaptcha_challenge_field"],
$_POST["recaptcha_response_field"]);
if (!$resp->is_valid) {
// What happens when the CAPTCHA was entered incorrectly
die ("The reCAPTCHA wasn't entered correctly. Go back and try it again." .
"(reCAPTCHA said: " . $resp->error . ")");
} else {
// Your code here to handle a successful verification
if(@mail($your_email,$email_subject,$email_content)) {
echo 'Message sent!'; }
}
if(!$_POST) exit;
$email = $_POST['email'];
//$error[] = preg_match('/\b[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b/i', $_POST['email']) ? '' : 'INVALID EMAIL ADDRESS';
if(!eregi("@",$email )){
$error.="Invalid email address entered";
$errors=1;
}
if($errors==1) echo $error;
else{
$values = array ('name','email','telephone','message');
$required = array('name','email','telephone','message');
$your_email = "email@mydomain.com";
$email_subject = "New Message from XXX: ".$_POST['subject'];
$email_content = "New message:\n";
foreach($values as $key => $value){
if(in_array($value,$required)){
if ($key != 'subject' && $key != 'telephone') {
if( empty($_POST[$value]) ) {
echo 'PLEASE FILL IN REQUIRED FIELDS'; exit;
}
}
$email_content .= $value.': '.$_POST[$value]."\n";
}
}
if(@mail($your_email,$email_subject,$email_content)) {
echo 'Message sent!';
}
else {
echo 'ERROR!';
}
}
?>
以下是表格中的代码:
<form action="./contact.php" method="post" id="contactform">
<?php
require_once('./recaptchalib.php');
$publickey = "xxxxxxxxxxxxxxxxxx"; // you got this from the signup page
echo recaptcha_get_html($publickey);
?>
<ol>
<li>
<label for="name">Name</label>
<input id="name" name="name" class="text" />
</li>
<li>
<label for="email">Email</label>
<input id="email" name="email" class="text" />
</li>
<li>
<label for="telephone">Telephone</label>
<input id="telephone" name="telephone" class="text" />
</li>
<li>
<label for="message">Message</label>
<textarea id="message" name="message" rows="6" cols="50"></textarea>
</li>
<li class="buttons">
<input type="image" name="imageField" id="imageField" src="images/send.gif" class="send" />
<div class="clr"></div>
</li>
</ol>
</form>
感谢您的帮助!
答案 0 :(得分:1)
“不让我发邮件”是什么意思?验证码是否失败,你得到“重新收回未正确输入”?
或者它是否尝试发送邮件但没有显示任何内容?
filter_var($email, FILTER_VALIDATE_EMAIL)
代替,这将比您的正则表达式更好。mail()
暂停@
来电。邮件本身不会输出任何错误消息,它只会返回true / false。检查返回值并输出错误消息(如果为false)。目前,只有在确实如此时才输出。毕竟,您似乎尝试使用$your_email
,$email_subject
等发送邮件...在您设置这些变量并从$中检索值之前彦博。所以你最有可能用空变量调用mail()
,这些变量什么也没做。