Guest User

Untitled

a guest
Feb 15th, 2019
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.93 KB | None | 0 0
  1. <?php
  2.  
  3. use PHPMailerPHPMailerPHPMailer;
  4. use PHPMailerPHPMailerException;
  5.  
  6. /* Exception class. */
  7. require '/home/saveyamo/public_html/PHPMailer/src/Exception.php';
  8.  
  9. /* The main PHPMailer class. */
  10. require '/home/saveyamo/public_html/PHPMailer/src/PHPMailer.php';
  11.  
  12. /* SMTP class, needed if you want to use SMTP. */
  13. require '/home/saveyamo/public_html/PHPMailer/src/SMTP.php';
  14.  
  15.  
  16. $errorMSG = "";
  17.  
  18.  
  19. /* NAME */
  20.  
  21. if (empty($_POST["firstName"])) {
  22. $errorMSG = "<li class='danger'>Your first name is required</<li>";
  23. } elseif (!filter_var($_POST["firstName"], FILTER_VALIDATE_INT) === false) {
  24. $errorMSG = "<li class='danger'>A number is not a valid name</<li>";
  25. } else {
  26. $firstname = filter_var($_POST["firstName"], FILTER_SANITIZE_STRING);
  27. }
  28.  
  29. if (empty($_POST["lastName"])) {
  30. $errorMSG = "<li class='danger'>Your last name is required</<li>";
  31. } elseif (!filter_var($_POST["lastName"], FILTER_VALIDATE_INT) === false) {
  32. $errorMSG = "<li class='danger'>A number is not a valid name</<li>";
  33. } else {
  34. $lastname = filter_var($_POST["lastName"], FILTER_SANITIZE_STRING);
  35. }
  36.  
  37.  
  38. /* EMAIL */
  39.  
  40. if (empty($_POST["email"])) {
  41. $errorMSG .= "<li class='danger'>Email is required</li>";
  42. } elseif (!filter_var($_POST["email"], FILTER_VALIDATE_EMAIL)) {
  43. $errorMSG .= "<li class='danger'>Invalid email format</li>";
  44. } else {
  45. $email = $_POST["email"];
  46. }
  47.  
  48. /*phone*/
  49. /*
  50. if (empty($_POST["phone"])) {
  51.  
  52. $errorMSG = "<li>Your phone number is required</<li>";
  53.  
  54. } else if (is_string($_POST["phone"])) {
  55.  
  56. $errorMSG = "<li>That is not a valid phone number</<li>";
  57.  
  58. } else if (is_int($_POST["phone"])) {
  59.  
  60. $phone = $_POST["phone"];
  61.  
  62. }
  63. */
  64.  
  65. function validate_phone_number($phones)
  66. {
  67. // Allow +, - and . in phone number
  68. $filtered_phone_number = filter_var($phones, FILTER_SANITIZE_NUMBER_INT);
  69. // Remove "-" from number
  70. $phone_to_check = str_replace("-", "", $filtered_phone_number);
  71. // Check the lenght of number
  72. // This can be customized if you want phone number from a specific country
  73. if (strlen($phone_to_check) < 10 || strlen($phone_to_check) > 14) {
  74. return false;
  75. } else {
  76. return true;
  77. }
  78. }
  79.  
  80. if (validate_phone_number($_POST["phone"]) == true) {
  81. $phone = $_POST["phone"];
  82. } elseif (validate_phone_number($_POST["phone"]) == false) {
  83. $errorMSG = "<li class='danger'>That is not a valid phone number, try again. Be sure to include your area code.</li>";
  84. } elseif (empty($_POST["phone"])) {
  85. $errorMSG = "<li class='danger'>Your phone number is required</<li>";
  86. }
  87.  
  88.  
  89.  
  90. /* marina */
  91.  
  92. if (empty($_POST["marina"])) {
  93. $errorMSG = "<li class='danger'>Your marina is required</<li>";
  94. } else {
  95. $marina = filter_var($_POST["marina"], FILTER_SANITIZE_STRING);
  96. }
  97.  
  98. /* checkboxes */
  99.  
  100. $boxes = $_POST["boxes"];
  101.  
  102.  
  103. /* MESSAGE */
  104.  
  105. if (empty($_POST["message"])) {
  106. $errorMSG .= "<li class='danger'>Message is required</li>";
  107. } else {
  108. $message = filter_var($_POST["message"], FILTER_SANITIZE_STRING);
  109. }
  110.  
  111.  
  112. if (empty($errorMSG)) {
  113. $message = "Name: ".$firstname." ".$lastname."<br />".", Email: ".$email."<br />".", Phone: ".$phone."<br />".", Marina: ".$marina."<br />".", Interests: ".$boxes."<br />".", Message:".$message;
  114. $ok = "<li class='ok'>Awesome! Your message was sent, we will be in touch shortly.</li>";
  115. //echo json_encode(['code'=>200, 'msg'=>$message]);
  116. echo json_encode(['code'=>200, 'msg'=>$ok]);
  117.  
  118. $to = "john.e.puaoi@gmail.com";
  119. $subject = "Contact";
  120.  
  121. //mail($to, $subject, $message);
  122. //phpmailer start
  123.  
  124. $mail = new PHPMailer(true); // Passing `true` enables exceptions
  125. try {
  126. //Server settings
  127. $mail->SMTPDebug = 2; // Enable verbose debug output
  128. $mail->isSMTP(); // Set mailer to use SMTP
  129. $mail->Host = 'smtp.example.com'; // Specify main and backup SMTP servers
  130. $mail->SMTPAuth = true; // Enable SMTP authentication
  131. $mail->Username = 'example@example.com'; // SMTP username
  132. $mail->Password = 'password'; // SMTP password
  133. $mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
  134. $mail->Port = 587; // TCP port to connect to
  135.  
  136. //Recipients
  137. $mail->setFrom('web@example.com', 'Contact Form');
  138. $mail->addAddress('example@gmail.com'); // Name is optional
  139. $mail->addReplyTo('$email', '$firstname');
  140.  
  141. //Content
  142. $mail->isHTML(true); // Set email format to HTML
  143. $mail->Subject = 'Contact Form Submission';
  144. $mail->Body = '$message';
  145. $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
  146.  
  147. $mail->send();
  148. } catch (Exception $e) {
  149. echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
  150. }
  151.  
  152. //phpmailer end
  153. exit;
  154. }
  155.  
  156.  
  157. echo json_encode(['code'=>404, 'msg'=>$errorMSG]);
Add Comment
Please, Sign In to add comment