fork download
  1. <?php
  2. header('Content-type: application/json');
  3.  
  4. $email_to = 'email@gmail.com'; //your email
  5.  
  6. $status = array(
  7. 'success'=>null,
  8. 'message'=>null,
  9. 'user_error'=>false
  10. );
  11.  
  12. $name = trim(stripslashes($_POST['name']));
  13. $email = trim(stripslashes($_POST['email']));
  14. $subject = trim(stripslashes($_POST['subject']));
  15. $message = trim(stripslashes($_POST['message']));
  16.  
  17. $alpha = '/^[a-zA-Z]+$/';
  18.  
  19. /* error validation */
  20. $errors = false;
  21. if(isset($_POST['submit']))
  22. {
  23. if(empty($name)){ //check if the name field was empty
  24. $errors['name'][] = "Name field was empty.";
  25. }
  26. if(preg_match($alpha, $name)){ //if the name has other letters then a-z or A-Z
  27. $errors['name'][] = "Name must contain only letters from a-z and A-Z";
  28. }
  29. if($subject){
  30. $errors['subject'][] = "Subject field was empty";
  31. }
  32. } else {
  33. $errors['submit'][] = "You shouldn't be here!";
  34. }
  35.  
  36. if($errors)
  37. {
  38. $status['success'] = false;
  39. $status['user_error'] = $errors;
  40.  
  41. die( json_encode($status) );
  42. }
  43.  
  44. /* prepare e-mail */
  45. $body = 'Name: $name '."\n\n" . 'Email: ' . $email . "\n\n" . 'Subject: ' . $subject . "\n\n" . 'Message: ' . $message;
  46.  
  47. /* send it & note any error */
  48. $status['success'] = $success = mail($email_to, $subject, $body, 'From: <'.$email_from.'>');
  49. $status['message'] = $success ? "Email sent!" : "Email failed.";
  50.  
  51.  
  52. die( json_encode($status) );
Success #stdin #stdout #stderr 0.02s 52472KB
stdin
Standard input is empty
stdout
{"success":false,"message":null,"user_error":{"submit":["You shouldn't be here!"]}}
stderr
PHP Notice:  Undefined index: name in /home/vohuHm/prog.php on line 12
PHP Notice:  Undefined index: email in /home/vohuHm/prog.php on line 13
PHP Notice:  Undefined index: subject in /home/vohuHm/prog.php on line 14
PHP Notice:  Undefined index: message in /home/vohuHm/prog.php on line 15