Guest User

Untitled

a guest
Sep 24th, 2018
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.38 KB | None | 0 0
  1. <?php
  2. //Show errors
  3. ini_set('display_errors', 1);
  4. ini_set('display_startup_errors', 1);
  5. error_reporting(E_ALL);
  6.  
  7. //Load Required Components
  8. require_once 'src/recaptcha_autoload.php';
  9. require_once "functions.php";
  10. use PHPMailerPHPMailerPHPMailer;
  11. use PHPMailerPHPMailerException;
  12. require 'src/Exception.php';
  13. require 'src/PHPMailer.php';
  14. require 'src/SMTP.php';
  15.  
  16. $errorsMSG = array(); // array to hold validation errors
  17. $data = array(); // array to pass back data
  18. // validate the variables ======================================================
  19. // if any of these variables don't exist, add an error to our $errorsMSG array
  20. if (empty($_POST['firstName'])) $errorsMSG['firstName'] = 'First Name is required.';
  21. if (empty($_POST['lastName'])) $errorsMSG['lastName'] = 'Last Name is required.';
  22. if (empty($_POST['companyName'])) $errorsMSG['companyName'] = 'Company Name is required.';
  23. if (empty($_POST['companyAddress'])) $errorsMSG['companyAddress'] = 'Company Address is required.';
  24. if (empty($_POST['city'])) $errorsMSG['city'] = 'City is required.';
  25. // if (empty($_POST['state'])) $errorsMSG['state'] = 'State is required.';
  26. if (empty($_POST['emailAddress'])) $errorsMSG['emailAddress'] = 'Email Address is required.';
  27. if (empty($_POST['message'])) $errorsMSG['message'] = 'Message is required.';
  28. // return a response ===========================================================
  29. // if there are any errors in our errors array, return a success boolean of false
  30. if (!empty($errorsMSG)) {
  31. // if there are items in our errors array, return those errors
  32. $data['success'] = false;
  33. $data['errorsMSG'] = $errorsMSG;
  34. } else {
  35. checkRecaptcha();
  36. }
  37.  
  38.  
  39. function checkReCaptcha()
  40. {
  41. // Declare Variables
  42. $gRecaptchaResponse = $_POST['g-recaptcha-response'];
  43. $remoteIp = get_client_ip();
  44. $secret = 'SECRET_KEY';
  45.  
  46. // form submit check
  47. if (isset($_POST['submit']))
  48. {
  49.  
  50. // Google reCAPTCHA response check
  51. if (isset($_POST['g-recaptcha-response']) && !empty($_POST['g-recaptcha-response']))
  52. {
  53.  
  54. // Create an instance of the service using your secret
  55. $recaptcha = new ReCaptchaReCaptcha($secret);
  56. $resp = $recaptcha->verify($_POST['g-recaptcha-response'], $_SERVER['REMOTE_ADDR']);
  57. // check if response is a success
  58.  
  59.  
  60. if ($resp->isSuccess())
  61. {
  62.  
  63. //Send Email
  64. sendMail();
  65. }
  66. else
  67. {
  68. //Throw Error if Validation Failed
  69. $errMsg = 'Robot verification failed, please try again.';
  70.  
  71. };
  72.  
  73. }
  74. else
  75. {
  76. //Throw Error if User Forgot
  77. $errMsg = 'Please click on the reCAPTCHA box.';
  78.  
  79. };
  80.  
  81. }
  82. //End of Check Google Recaptcha Function
  83.  
  84. }
  85.  
  86.  
  87. function sendMail() {
  88. $mail = new PHPMailer(true); // Passing `true` enables exceptions
  89. try {
  90. //Server settings
  91. $mail->SMTPDebug = 2; // Enable verbose debug output
  92. $mail->isSMTP(); // Set mailer to use SMTP
  93. $mail->Host = 'smtp.server.com'; // Specify main and backup SMTP servers
  94. $mail->SMTPAuth = true; // Enable SMTP authentication
  95. $mail->Username = 'user@server.com'; // SMTP username
  96. $mail->Password = 'SECRET_PASSWORD'; // SMTP password
  97. $mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
  98. $mail->Port = 587; // TCP port to connect to
  99. //Recipients
  100. $mail->setFrom('user@server.com', 'Mailer');
  101. $mail->addAddress('user@server.com', 'Joe User'); // Add a recipient
  102. //Content
  103. $mail->isHTML(true); // Set email format to HTML
  104. $mail->Subject = 'New Message from Contact Form';
  105. //prepare email body
  106. $body_message = "";
  107. $body_message .= "Sender IP: " . get_client_ip() ."<br>";
  108. $body_message .= "First Name: " . $firstName ."<br>";
  109. $body_message .= "Last Name: " . $lastName ."<br>";
  110. $body_message .= "Company Name: " . $companyName ."<br>";
  111. $body_message .= "Company Address: " . $companyAddress ."<br>";
  112. $body_message .= "City: " . $city ."<br>";
  113. $body_message .= "State: " . $state ."<br>";
  114. $body_message .= "Sender email: " . $emailAddress ."<br>";
  115. $body_message .= "Sender Phone: " . $phoneNumber ."<br>";
  116. $body_message .= "nn". $message;
  117.  
  118. $mail->Body = $body_message;
  119. $mail->send();
  120. $errorsMSG = 'Message successfully sent.';
  121. }
  122. catch(Exception $e) {
  123. $mail->ErrorInfo;
  124. $errorsMSG = 'There has been an issue sending your message.';
  125. }
  126. //End of sendMail Function
  127. }
  128.  
  129. // return all our data to an AJAX call
  130. echo json_encode($data);
  131. ?>
  132.  
  133. // Start
  134. $(document).ready(function() {
  135.  
  136. // process the form
  137. $('form').submit(function(event) {
  138.  
  139. $('.form-group').removeClass('has-error'); // remove the error class
  140. $('.help-block').remove(); // remove the error text
  141.  
  142. // get the form data
  143. // there are many ways to get this data using jQuery (you can use the class or id also)
  144. var formData = {
  145. 'firstName' : $('input[name=firstName]').val(),
  146. 'lastName' : $('input[name=lastName]').val(),
  147. 'companyName' : $('input[name=companyName]').val(),
  148. 'companyAddress' : $('input[name=companyAddress]').val(),
  149. 'city' : $('input[name=city]').val(),
  150. // 'state' : $('input[name=state]').val(),
  151. 'emailAddress' : $('input[name=emailAddress]').val(),
  152. 'message' : $('input[name=message]').val(),
  153. 'g-recaptcha' : $("#g-recaptcha-response").val(),
  154. };
  155.  
  156. // process the form
  157. $.ajax({
  158. type : 'POST', // define the type of HTTP verb we want to use (POST for our form)
  159. url : 'formMaster.php', // the url where we want to POST
  160. data : formData, // our data object
  161. dataType : 'json', // what type of data do we expect back from the server
  162. encode : true
  163. })
  164. // using the done promise callback
  165. .done(function(data) {
  166.  
  167. // log data to the console so we can see
  168. console.log(data);
  169.  
  170. // here we will handle errorsMSG and validation messages
  171. if ( ! data.success) {
  172.  
  173. // handle errorsMSG for name ---------------
  174. if (data.errorsMSG.firstName) {
  175. $('#firstName-group').addClass('has-error'); // add the error class to show red input
  176. $('#firstName-group').append('<div class="help-block">' + data.errorsMSG.firstName + '</div>'); // add the actual error message under our input
  177. }
  178.  
  179. // handle errorsMSG for name ---------------
  180. if (data.errorsMSG.lastName) {
  181. $('#lastName-group').addClass('has-error'); // add the error class to show red input
  182. $('#lastName-group').append('<div class="help-block">' + data.errorsMSG.lastName + '</div>'); // add the actual error message under our input
  183. }
  184.  
  185. // handle errorsMSG for name ---------------
  186. if (data.errorsMSG.companyName) {
  187. $('#companyName-group').addClass('has-error'); // add the error class to show red input
  188. $('#companyName-group').append('<div class="help-block">' + data.errorsMSG.companyName + '</div>'); // add the actual error message under our input
  189. }
  190.  
  191. // handle errorsMSG for Company Address ---------------
  192. if (data.errorsMSG.companyAddress) {
  193. $('#companyAddress-group').addClass('has-error'); // add the error class to show red input
  194. $('#companyAddress-group').append('<div class="help-block">' + data.errorsMSG.companyAddress + '</div>'); // add the actual error message under our input
  195. }
  196.  
  197. // handle errorsMSG for Company Address ---------------
  198. if (data.errorsMSG.city) {
  199. $('#city-group').addClass('has-error'); // add the error class to show red input
  200. $('#city-group').append('<div class="help-block">' + data.errorsMSG.city + '</div>'); // add the actual error message under our input
  201. }
  202.  
  203. // handle errorsMSG for Company Address ---------------
  204. // if (data.errorsMSG.state) {
  205. // $('#state-group').addClass('has-error'); // add the error class to show red input
  206. // $('#state-group').append('<div class="help-block">' + data.errorsMSG.state + '</div>'); // add the actual error message under our input
  207. // }
  208.  
  209. // handle errorsMSG for Email Address ---------------
  210. if (data.errorsMSG.emailAddress) {
  211. $('#emailAddress-group').addClass('has-error'); // add the error class to show red input
  212. $('#emailAddress-group').append('<div class="help-block">' + data.errorsMSG.emailAddress + '</div>'); // add the actual error message under our input
  213. }
  214.  
  215. // handle errorsMSG for superhero alias ---------------
  216. if (data.errorsMSG.message) {
  217. $('#message-group').addClass('has-error'); // add the error class to show red input
  218. $('#message-group').append('<div class="help-block">' + data.errorsMSG.message + '</div>'); // add the actual error message under our input
  219. }
  220.  
  221. } else {
  222.  
  223. // ALL GOOD! just show the success message!
  224. $('form').append('<div class="alert alert-success">' + data.message + '</div>');
  225.  
  226. // usually after form submission, you'll want to redirect
  227. // window.location = '/thank-you'; // redirect a user to another page
  228.  
  229. }
  230. })
  231.  
  232. // using the fail promise callback
  233. .fail(function(data) {
  234.  
  235. // show any errorsMSG
  236. // best to remove for production
  237. console.log(data);
  238. });
  239.  
  240. // stop the form from submitting the normal way and refreshing the page
  241. event.preventDefault();
  242. });
  243.  
  244. });
Add Comment
Please, Sign In to add comment