I made a PHP mailer script does the basic validation of fields, return errors, else submit if all is good. But it also has a honeypot field that is not required to be filled in (I'm assuming by hiding it using CSS a spambot will fill in the field anyway). If the field is not empty, it opens a text file and writes/appends the attempt on it, and it also sends an email alert of the attempt.
<?php
//print_r($_POST);
$error['name'] ="";
$error['company']="";
$error['email'] ="";
$error['subject'] ="";
$error['message'] ="";
$error['website'] ="";
$success = "";
$thistime = time();
$current_date = date('m/d/Y/T ==> H:i:s');
if(isset($_POST['_save'])) {
$name = stripslashes($_POST['name']);
$email = stripslashes($_POST['email']);
$company = stripslashes($_POST['company']);
$message = stripslashes($_POST['message']);
$subject = stripslashes($_POST['subject']);
$website = stripslashes($_POST['website']);
if (empty($name) || empty($email) || empty($subject) || empty($message) ||
!empty($website)) {
if (empty($name))
$error['name'] = "Please enter your Full Name";
if (empty($email))
$error['email'] = "Please enter a valid Email Address";
if (empty($company))
$error['company'] = "Please enter Your Company Name";
if (empty($subject))
$error['subject'] = "Please Write a Subject";
if (empty($message))
$error['message'] = "Please write a message, inquiries or
other concerns above";
if (!empty($website)) // this is the honeypot
$error['subject'] = "Opps looks like you're a spambot. You
just filled in a not required field.;
$myFile = "botlog.txt";
$fh = fopen($myFile, 'a') or die("can't open file");
$stringData = "bot trapped" . " " . "-" . " " . $website . " " . "-
" . " " . $current_date . "\r\n";
fwrite($fh, $stringData);
fclose($fh);
$donot="[email protected]";
$headers="From: {$email}\r\nReply-To: {$donot}"; //create headers
mail('[email protected]',$headers,$stringData);
}
else { //if not empty
stripslashes($headers);
$headers="From: {$email}\r\nReply-To: {$email}"; //create headers
$content="Name: ".$name."\r\n\r\nCompany: "
.$company."\r\n\r\nSubject: ".$subject."\r\n\r\nMessage: ".$message;
mail('[email protected]',$subject,$content,$headers); //mails it
$success = "Thank you! You're email has been sent.";
#done;
}
}
?>
Am I doing it right? Does this open any vulnerabilities? I'm open to any suggestions and improvements.