Last active
April 20, 2016 15:57
-
-
Save aalexeev239/67e768a4e2c3c7c82d027b5d65a83b72 to your computer and use it in GitHub Desktop.
simple php submit
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 | |
$FORM_EMAIL = "[email protected]"; // recepient | |
$FORM_EMAIL_FROM = "[email protected]"; // 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 = "[email protected]"; | |
$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