I recently began to have problems with spambots using the forms on my website to send large amounts of spam. I took the forms off for now, and am trying to use Recaptcha to make them secure. The code I have written seems to be fine, and when the form is submitted, it returns a successful message. The only problem is that I never receive the email. My code is split into two pages, as follow:
The form code for survey.php:
<form method="post" name="send" action="surveym.php">
<input type="hidden" name="action" value="send" />
<span style="color:#997867"><strong>How did you find out about the Troop website?</strong></span><br />
<input type="radio" name="how" value="troopmeeting" /> When it was announced at a Troop meeting<br />
<input type="radio" name="how" value="anotherperson" /> From another Troop member<br />
<input type="radio" name="how" value="google" /> By searching Google<br />
<input type="radio" name="how" value="other" /> Other: <input type="text" name="other" id="other" size="20" /><br /><br />
<span style="color:#997867"><strong>So far, what do you think about the site?</strong></span><br />
<textarea rows="6" name="thinkabout" cols="40"></textarea><br /><br />
<span style="color:#997867"><strong>In your opinion, where does the site need improvement?</strong</span><br />
<textarea rows="6" name="improvement" cols="40"></textarea><br /><br />
<span style="color:#997867"><strong>The site is working on the development of the following features. Please select three options as ones you would look forward to the most</strong></span><br />
<input type="checkbox" name="new1" value="pictures" /> Pictures from Troop events<br />
<input type="checkbox" name="new2" value="gearreview" /> Gear review section<br />
<input type="checkbox" name="new3" value="games" /> Interactive games<br />
<input type="checkbox" name="new4" value="contactinfo" /> Contact information (password protected)<br />
<input type="checkbox" name="new5" value="redesign" /> A site redesign bringing in new colors and more room for information<br />
<input type="checkbox" name="new6" value="other" /> Other: <input type="text" name="other2" size="20" /><br /><br />
<?php
require_once('recaptchalib.php');
$publickey = "MY KEY IS HERE";
echo recaptcha_get_html($publickey);
?>
<br />
<input type="submit" name="submit" value="Submit" /><br /><br />
</form>
The code in surveym.php:
<?php
require_once('recaptchalib.php');
$privatekey = "MY KEY IS HERE";
$resp = recaptcha_check_answer ($privatekey,
$_SERVER["REMOTE_ADDR"],
$_POST["recaptcha_challenge_field"],
$_POST["recaptcha_response_field"]);
if ($_POST["submit"]) {
if (!$resp->is_valid) {
die ("The reCAPTCHA wasn't entered correctly. Go back and try it again." .
"(reCAPTCHA said: " . $resp->error . ")");
} else {
$to_name = "ME";
$to_email = "MY EMAIL";
$subject = "Troop Survey";
$how_find = $_POST['how'];
$other_find = $_POST['other'];
$think_about = $_POST['thinkabout'];
$n_improvement = $_POST['improvement'];
$fav_feat1 = $_POST['new1'];
$fav_feat2 = $_POST['new2'];
$fav_feat3 = $_POST['new3'];
$fav_feat4 = $_POST['new4'];
$fav_feat5 = $_POST['new5'];
$fav_feat6 = $_POST['new6'];
$other_feat = $_POST['other2'];
$body = "How person found site: $how_find\n Other: $other_find\n What person thinks about site:\n $think_about\n What person thinks needs improvement:\n $n_improvement\n Person's favorite features: $fav_feat1, $fav_feat2, $fav_feat3, $fav_feat4, $fav_feat5, $fav_feat6\n Other: $other_feat";
mail("$to_name<$to_email>", $subject, $body);
print "<h2>Thank you for participating! Click the back button in your browser to return to the survey page.</h2>";
}
}
?>