Created
June 13, 2012 00:01
-
-
Save jcblw/2920914 to your computer and use it in GitHub Desktop.
simple email php script that return json for ajax mail scripting
This file contains 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 | |
function process(){ | |
$displayForm = true; | |
$success = false; | |
$error = array(); | |
// insert a valid email from the domain your sending | |
$headers = "From: domain.com <[email protected]>\r\n"; | |
// html emails because were bada55 | |
$headers .= "MIME-Version: 1.0\r\n"; | |
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n"; | |
// just making sure that information is there | |
function isValid($val) { | |
$value = trim($val); | |
if($value === '' || $value === null) { | |
return false; | |
} else { | |
return true; | |
} | |
}; | |
//our success obj | |
function success($num, $msg){ | |
$result = array( | |
'status' => 'success', | |
'confirmationNumber' => $num, | |
'message' => $msg | |
); | |
return $result; | |
}; | |
// valid email test against a regexp | |
function emailValidate($em){ | |
if(preg_match('/^[a-z0-9A-Z_\+-]+(\.[a-z0-9A-Z_\+-]+)*@[a-z0-9A-Z-]+(\.[a-z0-9A-Z-]+)*\.([a-z]{2,4})$/', $em)){ | |
return true; | |
}else{ | |
return false; | |
} | |
}; | |
// our error obj | |
function errors($err){ | |
$result = array( | |
'status' => 'errors', | |
'message' => $err | |
); | |
return $result; | |
}; | |
//Test Form Post | |
if(!empty($_POST['submit'])){ | |
foreach($_POST as $key => $val): | |
//array_push($error, "Testing -> $key : $val"); | |
// you make conditionals to skip not required fields | |
//this skips suffix and title | |
if(!isValid($val) and $key !== 'suffix' and $key !== 'title' ): | |
array_push($error, $key); | |
endif; | |
endforeach; | |
$valEm = emailValidate($_POST['email']); | |
if(!$valEm){array_push($error, 'email');} | |
//Results | |
if(empty($error)) { | |
$success = true; | |
$displayForm = false; | |
$head = "<h2>Email Header</h2>"; | |
// Pull the variables into this body variable | |
$body = "<p>Body of infomation</p>"; | |
$message = $head . $body; | |
// In case any of our lines are larger than 70 characters, we should use wordwrap() | |
$message = wordwrap($message, 70); | |
// Insert your email address here | |
mail('[email protected]','New Web Form Submission' , $message , $headers); | |
return success($confirmation_no, $message); | |
}else{ | |
return errors($error, 'Please fix errors then resubmit'); | |
} | |
}else{ | |
array_push($error, 'You have submitted this form in a Bad Method'); | |
return errors($error); | |
} | |
} | |
header('Cache-Control: no-cache, must-revalidate'); | |
header('Content-type: application/json'); | |
echo json_encode(process()); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
removing all referances to emails