Process form submissions into templated emails.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

process.php 1.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. require_once('config.php');
  3. function templateLogic($matches)
  4. {
  5. $temp = trim($matches[0], "{}");
  6. if(isset($_POST[$temp]))
  7. {
  8. return $_POST[$temp];
  9. }
  10. elseif($temp == "phpfEVERYTHING")
  11. {
  12. foreach($_POST as $key => $value)
  13. {
  14. if($key == "recaptcha_challenge_field"
  15. || $key == "recaptcha_response_field")
  16. {
  17. continue;
  18. }
  19. $everything .= $key.": ".$value."<br>\n";
  20. }
  21. return $everything;
  22. }
  23. return "{TEMPLATE FIELD NOT SPECIFIED}";
  24. }
  25. if($recaptchaEnabled)
  26. {
  27. if(!isset($_POST["recaptcha_challenge_field"])
  28. || !isset($_POST["recaptcha_response_field"]))
  29. {
  30. die("Invalid CAPTCHA verification. Please go back and try again.");
  31. }
  32. require_once('recaptchalib.php');
  33. $resp = recaptcha_check_answer($recaptchaPrivKey,
  34. $_SERVER["REMOTE_ADDR"],
  35. $_POST["recaptcha_challenge_field"],
  36. $_POST["recaptcha_response_field"]);
  37. if(!$resp->is_valid)
  38. {
  39. die("Invalid CAPTCHA verification. Please go back and try again.");
  40. }
  41. }
  42. if(isset($_POST["template"]))
  43. {
  44. $template = file($_POST["template"]);
  45. }
  46. else
  47. {
  48. $template = file($templateFile);
  49. }
  50. $emailBody = "";
  51. $subject = "";
  52. if(isset($_POST["email"]))
  53. {
  54. $subject = "Contact form message from ".$_POST["email"];
  55. }
  56. else
  57. {
  58. $subject = "Contact form message.";
  59. }
  60. $pattern = "/\{([a-zA-Z0-9_]+)\}/i";
  61. foreach($template as $line)
  62. {
  63. $emailBody .= preg_replace_callback(
  64. $pattern, "templateLogic",
  65. $line);
  66. }
  67. if(isset($_POST["redirectURL"]))
  68. {
  69. $redirectDestination = $_POST["redirectURL"];
  70. }
  71. if(@mail($sendToEmail, $subject, $emailBody, NULL, $sendmailParam))
  72. {
  73. if(isset($redirectDestination))
  74. {
  75. header("Location: $redirectDestination");
  76. }
  77. else
  78. {
  79. echo "Thank You for using John's form processing script. Your message has delivered successfuly.";
  80. }
  81. }
  82. else
  83. {
  84. echo "An error prevented your message from sending. Please try again later.";
  85. }
  86. ?>