<?php
function test_input($data){
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
// define variables and set to empty values
$first_nameErr = $last_nameErr = $emailErr = $overviewErr = "";
$first_name = $last_name = $email = $overview = "";
if(isset($_POST['email'])) {
$email_to = "myself@mydomain.com";
$email_subject = "Contact us - My company's name";
{
if (empty($_POST["first_name"]))
{$first_nameErr = "(First Name is required)";}
else
{$first_name = test_input($_POST["first_name"]);
// check if name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$first_name))
{
$first_name = "(Only letters and white space allowed)";
}
}
if (empty($_POST["last_name"]))
{$last_nameErr = "(Last Name is required)";}
else
{$last_name = test_input($_POST["last_name"]);
// check if name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$last_name))
{
$last_name = "(Only letters and white space allowed)";
}
}
if (empty($_POST["email"]))
{$emailErr = "(Email ID is required)";}
else
{$email = test_input($_POST["email"]);
// check if e-mail address syntax is valid
if (!preg_match("/([w-]+@[w-]+.[w-]+)/",$email))
{
$emailErr = "(Invalid email format)";
}
}
if (empty($_POST["overview"]))
{$overviewErr = "(Overview is required)";}
else
{$overview = test_input($_POST["overview"]);
// check if name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$overview))
{
$overview = "(Only letters and white space allowed)";
}
}
}
//Email & SEND INFO
$email_message = "Form details below.nn";
$email_message .= "First Name: ".clean_string($first_name)."n";
$email_message .= "Last Name: ".clean_string($last_name)."n";
$email_message .= "Email: ".clean_string($email)."n";
$email_message .= "Services: ".clean_string(implode(', ', $service))."n";
$email_message .= "Overview: ".clean_string($overview)."n";
// create email headers
$headers = 'From: '.$email_from."rn".
'Reply-To: '.$email_from."rn" .
'X-Mailer: PHP/' . phpversion();
mail($email_to, $email_subject, $email_message, $headers);
?>
<!-- Success HTML -->
Thank you for contacting us. We will be in touch with you very soon.
<?php
}
?>
<?php
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
// define variables and set to empty values
$first_nameErr = $last_nameErr = $emailErr = $overviewErr = "";
$first_name = $last_name = $email = $overview = "";
if(isset($_POST['email'])) {
$email_to = "myself@mydomain.com";
$email_subject = "Contact us - My company's name";
}
if (empty($_POST["first_name"])) {
$first_nameErr = "(First Name is required)";
} else {
$first_name = test_input($_POST["first_name"]);
}
// check if name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$first_name)) {
$first_name = "(Only letters and white space allowed)";
}
if (empty($_POST["last_name"])) {
$last_nameErr = "(Last Name is required)";
} else {
$last_name = test_input($_POST["last_name"]);
}
// check if name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$last_name)) {
$last_name = "(Only letters and white space allowed)";
}
if (empty($_POST["email"])) {
$emailErr = "(Email ID is required)";
} else {
$email = test_input($_POST["email"]);
}
// check if e-mail address syntax is valid
if (!preg_match("/([w-]+@[w-]+.[w-]+)/",$email)) {
$emailErr = "(Invalid email format)";
}
if (empty($_POST["overview"])) {
$overviewErr = "(Overview is required)";
} else {
$overview = test_input($_POST["overview"]);
}
// check if name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$overview)) {
$overview = "(Only letters and white space allowed)";
}
//Email & SEND INFO
$email_message = "Form details below.nn";
$email_message .= "First Name: ".clean_string($first_name)."n";
$email_message .= "Last Name: ".clean_string($last_name)."n";
$email_message .= "Email: ".clean_string($email)."n";
$email_message .= "Services: ".clean_string(implode(', ', $service))."n";
$email_message .= "Overview: ".clean_string($overview)."n";
// create email headers
$headers = 'From: '.$email_from."rn".
'Reply-To: '.$email_from."rn" .
'X-Mailer: PHP/' . phpversion();
mail($email_to, $email_subject, $email_message, $headers);
if ($first_nameErr || $last_nameErr || $emailErr || $overviewErr) {
echo "Thank you for contacting us. We will be in touch with you very soon.";
} else {
echo "There are some errors in your form: " . $first_nameErr . ', ' . $last_nameErr . ', ' . $emailErr . ', ' . $overviewErr;
}