Advertisement
ruebenmira

sendmail.php file

Mar 20th, 2014
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.82 KB | None | 0 0
  1. <?php
  2. //start a session -- needed for Securimage Captcha check
  3. session_start();
  4.  
  5. //add you e-mail address here
  6. define("MY_EMAIL", "ruebenmira@gmail.com");
  7.  
  8. /**
  9. * Sets error header and json error message response.
  10. *
  11. * @param String $messsage error message of response
  12. * @return void
  13. */
  14. function errorResponse ($messsage) {
  15. header('HTTP/1.1 500 Internal Server Error');
  16. die(json_encode(array('message' => $messsage)));
  17. }
  18.  
  19. /**
  20. * Return a formatted message body of the form:
  21. * Name: <name of submitter>
  22. * Comment: <message/comment submitted by user>
  23. *
  24. * @param String $name name of submitter
  25. * @param String $message message/comment submitted
  26. */
  27. function setMessageBody ($name, $message) {
  28. $message_body = "Name: " . $name."\n\n";
  29. $message_body .= "Comment:\n" . nl2br($message);
  30. return $message_body;
  31. }
  32.  
  33. $email = $_POST['email'];
  34. $message = $_POST['message'];
  35.  
  36. header('Content-type: application/json');
  37. //do some simple validation. this should have been validated on the client-side also
  38. if (empty($email) || empty($message)) {
  39. errorResponse('Email or message is empty.');
  40. }
  41.  
  42. //do Captcha check, make sure the submitter is not a robot:)...
  43. include_once './securimage/securimage.php';
  44. $securimage = new Securimage();
  45. if (!$securimage->check($_POST['captcha_code'])) {
  46. errorResponse('Invalid Security Code');
  47. }
  48.  
  49. //try to send the message
  50. if(mail(MY_EMAIL, "Feedback Form Results", setMessageBody($_POST["name"], $message), "From: $email")) {
  51. echo json_encode(array('message' => 'Your message was successfully submitted.'));
  52. } else {
  53. header('HTTP/1.1 500 Internal Server Error');
  54. echo json_encode(array('message' => 'Unexpected error while attempting to send e-mail.'));
  55. }
  56. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement