Last active
December 16, 2015 22:29
-
-
Save ncloward/5507029 to your computer and use it in GitHub Desktop.
Simple PHP REST mailer.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Simple PHP form handler. | |
* | |
* LICENSE: | |
* | |
* The MIT License (MIT) | |
* | |
* Copyright (c) 2013 Nicholas Cloward | |
* | |
* Permission is hereby granted, free of charge, to any person obtaining a copy | |
* of this software and associated documentation files (the "Software"), to deal | |
* in the Software without restriction, including without limitation the rights | |
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
* copies of the Software, and to permit persons to whom the Software is | |
* furnished to do so, subject to the following conditions: | |
* | |
* The above copyright notice and this permission notice shall be included in | |
* all copies or substantial portions of the Software. | |
* | |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
* THE SOFTWARE. | |
* | |
* @author Nicholas Cloward <[email protected]> | |
* @copyright 2013 Nicholas Cloward | |
* @license http://www.opensource.org/licenses/mit-license.html MIT License | |
* @version 1.0 | |
* | |
*/ | |
// Fill these out... | |
$site_name = ""; | |
$to = ""; | |
$from = ""; | |
// Check honey pot field. | |
if(!empty($_POST["honey"])) | |
{ | |
// If its not empty the request is invalid. Probably a bot. | |
header("HTTP/1.1 403 Forbidden"); | |
echo "Unauthorized access. You do not have sufficient rights to do this action."; | |
exit(); | |
} | |
// Setup form fields based on form type. | |
$fields = array(); | |
$form_type = ucfirst(strtolower($_POST["type"])); | |
// Add form types and fields here. | |
switch ($form_type) | |
{ | |
case "Contact": | |
$fields = array("Name", "Phone", "Email", "Message"); | |
break; | |
case "Quote": | |
$fields = array("Name", "Phone", "Zip", "Email"); | |
break; | |
default: | |
header("HTTP/1.1 400 Bad Request"); | |
echo "Invalid form type."; | |
exit(); | |
} | |
// Setup Variables | |
$errors = array(); | |
$email = ""; | |
$subject = "$site_name Website -- $form_type form submission"; | |
$body = "You have received a new message from the $site_name website $form_type form. Here are the details:" . PHP_EOL . PHP_EOL; | |
// Validate fields | |
foreach ($fields as &$field) | |
{ | |
// Get value of field. | |
$value = stripslashes(strip_tags($_POST[strtolower($field)])); | |
// Check if its empty. | |
if(empty($value)) | |
{ | |
array_push($errors, array(strtolower($field), $field . "$field is required.")); | |
} | |
// Check if its an email field. | |
if(strtolower($field) == "email") | |
{ | |
// Validate email address | |
if(!preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i", $value)) | |
{ | |
array_push($errors, array(strtolower($field), "$field is invalid.")); | |
} | |
else | |
{ | |
$email = $value; | |
} | |
} | |
// Append form data to email body. | |
$body .= "$field: $value" . PHP_EOL; | |
} | |
// Check if there were any errors. | |
if(!empty($errors)) | |
{ | |
// Return bad request with json of error fields. | |
header("HTTP/1.1 400 Bad Request"); | |
echo json_encode(array("fields" => $errors)); | |
exit(); | |
} | |
// Setup Email headers. | |
$headers = "From: $from" . PHP_EOL; | |
$headers .= "Reply-To: $email" . PHP_EOL; | |
$headers .= "X-Mailer: PHP/" . phpversion(); | |
// Send email and check if it was successful. | |
if(!mail($to,$subject,$body,$headers)) | |
{ | |
// Sending email faild return 500. | |
header("HTTP/1.1 500 Internal Server Error"); | |
echo json_encode(array("error" => "An error occurred while sending the email.")); | |
exit(); | |
} | |
// Email was successful return success. | |
echo json_encode(array("success" => true)); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment