Skip to content

Instantly share code, notes, and snippets.

@jonmbake
Last active August 29, 2015 14:02
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 jonmbake/da5e18b223ac4cac7e14 to your computer and use it in GitHub Desktop.
Save jonmbake/da5e18b223ac4cac7e14 to your computer and use it in GitHub Desktop.
Do not wait for mail to return in order to notify user of success
<?php
//start a session -- needed for Securimage Captcha check
session_start();
//add you e-mail address here
define("MY_EMAIL", "your-email@foo.com");
define("EMAIL_SUBJECT", "Feedback Form Results");
//a map of fields to include in email, along with if they are required or not
//aparently in PHP, arrays (maps) can't be constants?
$fields_req = array("name" => true, "title" => false, "company" => false, "company" => false,
"website" => false, "phone" => true, "message" => true);
/**
* Sets error header and json error message response.
*
* @param String $messsage error message of response
* @return void
*/
function errorResponse ($messsage) {
header('HTTP/1.1 500 Internal Server Error');
die(json_encode(array('message' => $messsage)));
}
/**
* Pulls posted values for all fields in $fields_req array.
* If a required field does not have a value, an error response is given.
*
* @param [Array] $fields_req a map of field name to required
*/
function setMessageBody ($fields_req) {
$message_body = "";
foreach ($fields_req as $name => $required) {
if ($required && empty($name)) {
errorResponse("$name is empty.");
} else {
$message_body .= ucfirst($name) . ": " . $_POST[$name] . "\n";
}
}
return $message_body;
}
$email = $_POST['email'];
header('Content-type: application/json');
//do some simple validation. this should have been validated on the client-side also
if (empty($email)) {
errorResponse('Email or message is empty.');
}
//do Captcha check, make sure the submitter is not a robot:)...
include_once './vender/securimage/securimage.php';
$securimage = new Securimage();
if (!$securimage->check($_POST['captcha_code'])) {
errorResponse('Invalid Security Code');
}
//try to send the message
echo json_encode(array('message' => 'Your message was successfully submitted.'));
mail(MY_EMAIL, EMAIL_SUBJECT, setMessageBody($fields_req), "From: $email");
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment