Guest User

Untitled

a guest
Oct 5th, 2018
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.51 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. var_dump($_POST);
  8.  
  9. //Load Required Components
  10. require_once 'src/recaptcha_autoload.php';
  11. require_once "functions.php";
  12. use PHPMailer\PHPMailer\PHPMailer;
  13. use PHPMailer\PHPMailer\Exception;
  14. require 'src/Exception.php';
  15. require 'src/PHPMailer.php';
  16. require 'src/SMTP.php';
  17.  
  18. function validate($formData)
  19. {
  20.  
  21. // Initiate Arrays
  22.  
  23. $errorsMSG = array(); // array to hold validation errors
  24. $data = array(); // array to pass back data k:not needed
  25.  
  26. $firstName = $formData['firstName'] ?? '';
  27. $lastName = $formData['lastName'];
  28. $companyName = $formData['companyName'];
  29. $companyAddress = $formData['companyAddress'];
  30. $emailAddress = $formData['emailAddress'];
  31. $message = $formData['message'];
  32. $pname_exp = '/^[a-zA-Z0-9\_]{2,20}/';
  33.  
  34. // Validate firstName
  35.  
  36. if (empty($firstName)) {
  37. $errorsMSG['firstName'] = 'First Name is required.';
  38. }
  39.  
  40. // Check RegEx for Personal Name
  41.  
  42. if (!preg_match($pname_exp, $firstName)) {
  43. $errorsMSG['firstName'] = 'First Name is required.';
  44. }
  45.  
  46. // Validate lastName
  47.  
  48. if (empty($lastName)) {
  49. $errorsMSG['lastName'] = 'Last Name is required.';
  50. }
  51.  
  52. // Check RegEx for Personal Name
  53.  
  54. if (!preg_match($pname_exp, $lastName)) {
  55. $errorsMSG['firstName'] = 'Last Name is required.';
  56. }
  57.  
  58. // Validate companyName
  59.  
  60. if (empty($companyName)) {
  61. $errorsMSG['companyName'] = 'Company Name is required.';
  62. }
  63.  
  64. // Validate companyAddress
  65.  
  66. if (empty($companyAddress)) {
  67. $errorsMSG['companyAddress'] = 'Company Address is required.';
  68. }
  69.  
  70. // Validate emailAddress
  71.  
  72. if (empty($emailAddress)) {
  73. $errorsMSG['emailAddress'] = 'Email Address is required.';
  74. }
  75.  
  76. // Check if emailAddress is a valid email address
  77.  
  78. if (!filter_var($emailAddress, FILTER_VALIDATE_EMAIL)) {
  79. $errorsMSG['emailAddress'] = 'Email address is not valid.';
  80. }
  81.  
  82. // Validate message
  83.  
  84. if (empty($message)) {
  85. $errorsMSG['message'] = 'Message is required.';
  86. }
  87.  
  88. $captcha = checkCaptcha($formData['g-recaptcha-response']);
  89. if(!$captcha['isSuccess']){
  90. $errorsMSG ['captcha'] = $captcha['errorCodes'];
  91. }
  92. return $errorsMSG;
  93.  
  94. //Send Errors
  95. if ( ! empty($errorsMSG)) {
  96.  
  97. // if there are items in our errors array, return those errors
  98. $data['success'] = false;
  99. $data['errors'] = $errorsMSG;
  100. }
  101.  
  102. echo json_encode($data);
  103.  
  104. }
  105.  
  106. // [
  107. // 'isSuccess' => bool,
  108. // 'errorCodes' => k: actually IDK :)
  109. // ]
  110.  
  111. function checkCaptcha($g_recaptcha_response)
  112. {
  113. $recaptcha_secret_key = 'SECRET_PASSWORD';
  114. // $recaptcha = new ReCaptchaReCaptcha($recaptcha_secret_key, new ReCaptchaRequestMethodCurlPost());
  115. $recaptcha = new \ReCaptcha\ReCaptcha($recaptcha_secret_key);
  116. $resp = $recaptcha->verify($g_recaptcha_response, $_SERVER['REMOTE_ADDR']);
  117. return [
  118. 'isSuccess' => $resp->isSuccess(),
  119. 'errorCodes' => $resp->getErrorCodes(),
  120. ];
  121. }
  122.  
  123. function sendMail($formData)
  124. {
  125. $mail = new PHPMailer(true); // Passing `true` enables exceptions
  126. // Server settings
  127.  
  128. $mail->SMTPDebug = 2; // Enable verbose debug output
  129. $mail->isSMTP(); // Set mailer to use SMTP
  130. $mail->Host = 'smtp.server.com'; // Specify main and backup SMTP servers
  131. $mail->SMTPAuth = true; // Enable SMTP authentication
  132. $mail->Username = 'user@server.com'; // SMTP username
  133. $mail->Password = 'SECRET_PASSWORD'; // SMTP password
  134. $mail->SMTPSecure = 'ssl'; // Enable TLS encryption, `ssl` also accepted
  135. $mail->Port = 465; // TCP port to connect to
  136.  
  137. // Recipients
  138.  
  139. $mail->setFrom('user@server.com', 'Mailer');
  140. $mail->addAddress('user@server.com', 'Joe User'); // Add a recipient
  141.  
  142. // Content
  143.  
  144. $mail->isHTML(true); // Set email format to HTML
  145. $mail->Subject = 'New Message from Contact Form';
  146.  
  147. // prepare email body
  148.  
  149. $body_message = "";
  150. $body_message.= "Sender IP: " . get_client_ip() . "<br />";
  151.  
  152. // @todo: make the other rows the same way, i.e. $formData['key'];
  153.  
  154. $body_message.= "First Name: " . $formData['firstName'] . "<br />";
  155. $body_message.= "Last Name: " . $formData['lastName'] . "<br />";
  156. $body_message.= "Company Name: " . $formData['companyName'] . "<br />";
  157. $body_message.= "Company Address: " . $formData['companyAddress'] . "<br />";
  158. $body_message.= "City: " . $formData['city'] . "<br />";
  159. //$body_message.= "State: " . $state . "<br />";
  160. $body_message.= "Sender email: " . $formData['emailAddress'] . "<br />";
  161. // $body_message.= "Sender Phone: " . $formData['phoneNumber'] . "<br />";
  162. $body_message.= "\n\n" . $formData['message'];
  163. $mail->Body = $body_message;
  164. $mail->send();
  165. }
  166.  
  167. /////////////////////////////////////////////////
  168. // process
  169.  
  170. //this will be our whole response (jsoned later)
  171. $response = [
  172. //we'll change these later, possibly:
  173. 'success' => false,
  174. 'errors' => [],
  175. 'message' => 'There has been an issue sending your message',//could be an "OK" error message as well, depends on the 'success' key.
  176. ];
  177.  
  178. // Copy $_POST to $formData
  179. $formData = $_POST;
  180.  
  181. // mock / change $formData if needed (for tests etc)
  182. // $formData['blah'] = 'bleh';
  183.  
  184.  
  185. //validate
  186. $errors = validate($formData);
  187.  
  188. if(!empty($errors)){
  189. $response['success'] = false;
  190. $response['errors'] = $errors;
  191.  
  192. }else {//it's ok
  193. //send it
  194. try{
  195. sendMail($formData);
  196. //if we are here, then everything theoretically went fine.
  197. $response['success'] = true;
  198. $response['message'] = 'OK';
  199. }
  200. catch(Exception $e) {
  201. // @todo - in javascript, in the if ( ! data.success) .... do something about the
  202. $response['success'] = false;
  203. $response['message'] = 'There has been an issue sending your message';
  204.  
  205. }
  206.  
  207. }
  208.  
  209. echo json_encode($response);
  210. exit;
  211.  
  212. ?>
Add Comment
Please, Sign In to add comment