Skip to content

Instantly share code, notes, and snippets.

@bmelton
Created March 23, 2014 22:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bmelton/9731095 to your computer and use it in GitHub Desktop.
Save bmelton/9731095 to your computer and use it in GitHub Desktop.
<?php
//error_reporting(E_ALL ^ E_NOTICE); // hide all basic notices from PHP
$response = array(
"errors" => false,
"success" => true,
"messages" => "",
);
if($_SERVER["REQUEST_METHOD"] == "POST") {
if(isset($_POST['submitted'])) {
// require a name from user
if(trim($_POST['contactName']) === '') {
$nameError = 'Forgot your name!';
$hasError = true;
$response["messages"] = "Forgot your name!";
$response["errors"] = true;
$response["success"] = false;
} else {
$name = trim($_POST['contactName']);
}
// need valid email
if(trim($_POST['email']) === '') {
$emailError = 'Forgot to enter in your e-mail address.';
$hasError = true;
$response["messages"] = "Forgot to enter in your e-mail address.";
$response["errors"] = true;
$response["success"] = false;
} else if (!preg_match("/^[[:alnum:]][a-z0-9_.-]*@[a-z0-9.-]+\.[a-z]{2,4}$/i", trim($_POST['email']))) {
$emailError = 'You entered an invalid email address.';
$response["messages"] = "You entered an invalid email address.";
$response["errors"] = true;
$response["success"] = false;
$hasError = true;
} else {
$email = trim($_POST['email']);
}
// we need at least some content
if(trim($_POST['comments']) === '') {
$commentError = 'You forgot to enter a message!';
$hasError = true;
$response["messages"] = "Forgot to enter a message!";
$response["errors"] = true;
$response["success"] = false;
} else {
if(function_exists('stripslashes')) {
$comments = stripslashes(trim($_POST['comments']));
} else {
$comments = trim($_POST['comments']);
}
}
// we need phone number
if(trim($_POST['phone']) === '') {
$phoneError = 'You forgot to enter a phone number';
$hasError = true;
$response["messages"] = "You forgot to enter a phone number";
$response["errors"] = true;
$response["success"] = false;
} else {
if(function_exists('stripslashes')) {
$phone = stripslashes(trim($_POST['phone']));
} else {
$phone = trim($_POST['phone']);
}
}
// upon no failure errors let's email now!
if(!isset($hasError)) {
$emailTo = 'gzuiderweg@gmail.com';
$subject = 'Submitted message from '.$name;
$sendCopy = trim($_POST['sendCopy']);
$body = "Name: $name \n\nEmail: $email \n\Phone: $phone \n\nComments: $comments";
$headers = 'From: ' .' <'.$emailTo.'>' . "\r\n" . 'Reply-To: ' . $email;
mail($emailTo, $subject, $body, $headers);
// set our boolean completion value to TRUE
$emailSent = true;
}
}
header('Content-Type: application/json');
echo json_encode($response);
} else {
echo "This page should only be POSTed to.";
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment