Skip to content

Instantly share code, notes, and snippets.

@aalexeev239
Last active April 20, 2016 15:57
Show Gist options
  • Save aalexeev239/67e768a4e2c3c7c82d027b5d65a83b72 to your computer and use it in GitHub Desktop.
Save aalexeev239/67e768a4e2c3c7c82d027b5d65a83b72 to your computer and use it in GitHub Desktop.
simple php submit
<?php
$FORM_EMAIL = "info@foo.bar"; // recepient
$FORM_EMAIL_FROM = "baz@foo.bar"; // sender
$SITE_NAME = "foo.bar";
$EMPTY_FIELD_TEXT = " field is not filled";
$SUCCESS_TEXT = "Message successfully sent.";
$FAIL_TEXT = "Failed to send message";
$FORM_WAS_SUBMITTED_FROM = "Form was filled on ";
$ERROR_TEXT = "Error!";
$required_fields = array("name", "email", "message");
function translateField($inp) {
switch ($inp) {
case "name":
return "Name";
case "email":
return "E-mail";
case "message":
return "Message";
default:
return ucfirst($inp);
}
}
if(isset($_POST)){
$response = array('type'=>'', 'message'=>'');
try{
foreach($required_fields as $field){
if(empty($_POST[$field])){
throw new Exception($ERROR_TEXT." ".translateField($field).$EMPTY_FIELD_TEXT);
}
}
// if OK
$out = $FORM_WAS_SUBMITTED_FROM.$SITE_NAME;
foreach($required_fields as $field){
$out .= "\r\n\n".translateField($field).": ".$_POST[$field];
}
$headers = "Content-Type: text/plain; charset=UTF-8\r\n";
$headers = $headers."From: ".$FORM_EMAIL_FROM."\r\n";
if ($name === 'testtest') {
$FORM_EMAIL = "alexeev.andrey.a@gmail.com";
$out = "TEST \r\n".$out;
}
if( mail( $FORM_EMAIL, $FORM_WAS_SUBMITTED_FROM.$SITE_NAME, $out, $headers ) ) {
$response['type'] = 'success';
$response['message'] = $SUCCESS_TEXT;
}
else {
$response['type'] = 'error';
$response['message'] = $FAIL_TEXT;
}
} catch(Exception $e){
$response['type'] = 'error';
$response['message'] = $e->getMessage();
}
// return json responce
print json_encode($response);
exit;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment