Guest User

Untitled

a guest
Dec 3rd, 2017
365
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.15 KB | None | 0 0
  1. <?php
  2.  
  3. require 'BootstrapLandinPage/lib/PHPMailer-master/src/Exception.php';
  4.  
  5. require 'BootstrapLandinPage/lib/PHPMailer-master/src/PHPMailer.php';
  6.  
  7. require 'BootstrapLandinPage/lib/PHPMailer-master/src/SMTP.php';
  8.  
  9.  
  10. $error_msg = array();
  11. $success_msg = array();
  12. $data = '';
  13.  
  14. // prevent warnings or errors from displaying, else you won't get proper json result
  15. ini_set('display_errors',0);
  16.  
  17. if ($_SERVER["REQUEST_METHOD"] == "POST") {
  18.  
  19. if (empty($_POST["name"])) {
  20. $error_msg['name'] = "Name is required";
  21. } else {
  22. $name = test_input($_POST["name"]);
  23. // check if name only contains letters and whitespace
  24. if (!preg_match("/^[a-zA-Zא-ת ]*$/",$name)) {
  25. $error_msg['name'] = "Only letters and white space allowed";
  26. }
  27. }
  28.  
  29.  
  30. if (empty($_POST["email"])) {
  31. $error_msg['email'] = "Email is required";
  32. } else {
  33. $email = test_input($_POST["email"]);
  34. // check if e-mail address is well-formed
  35. if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
  36. $error_msg['email'] = "Invalid email format";
  37. }
  38. }
  39.  
  40. if (empty($_POST["phone"])) {
  41. $error_msg['phone'] = "Phone is required";
  42. } else {
  43. $phone = test_input($_POST["phone"]);
  44. // check if e-mail address is well-formed
  45. if (!preg_match("/^(d[s-]?)?[([s-]{0,2}?d{3}[)]s-]{0,2}?d{3}[s-]?d{4}$/i",$phone)) {
  46. $error_msg['phone'] = "Invalid phone number";
  47. }
  48. }
  49. if (empty($_POST["subject"])) {
  50. $error_msg['subject'] = "Subject is required";
  51. }
  52. if (empty($_POST["message"])) {
  53. $error_msg['message'] = "Message is required";
  54. }
  55.  
  56. if (empty($_POST["subject"])) {
  57. $subject = "";
  58. } else {
  59. $subject = test_input($_POST["subject"]);
  60. }
  61.  
  62. if (empty($_POST["message"])) {
  63. $message = "";
  64. } else {
  65. $message = test_input($_POST["message"]);
  66. }
  67. if ($_POST['submit']) {
  68. if (mail ($to, $subject, $body, $from)) {
  69. echo '<p>Your message has been sent!</p>';
  70. } else {
  71. echo '<p>Something went wrong, go back and try again!</p>';
  72. }
  73. }
  74. if (empty($error_msg)){
  75. $message_body = '';
  76. unset($_POST['submit']);
  77. foreach ($_POST as $key => $value){
  78. $message_body .= "$key: $valuen";
  79. }
  80.  
  81. $to = 'ilonaXXX@gmail.com';// some email I use
  82. $subjectm = 'Contact Form Submit';
  83. $mail = new PHPMailer(true);
  84. try {
  85. //Server settings
  86. $mail->SMTPDebug = 2;
  87. $mail->isSMTP();
  88. $mail->Host = 'smtp.gmail.com';
  89. $mail->SMTPAuth = true;
  90. $mail->Username = 'XXXX@gmail.com'; // my gmail
  91. $mail->Password = '*****';// my gmail password
  92. $mail->SMTPSecure = 'ssl';
  93. $mail->Port = 465;// my xampp port is 80 , which I tried to put here ,but it wont work anyway
  94.  
  95. //Recipients
  96. $mail->setFrom('xxxxx@gmail.com', 'Ilona Sem');
  97. $mail->addAddress($email, $name);
  98.  
  99. //Content
  100. $mail->isHTML(true);
  101. $mail->Subject = 'Contact Form Submit';
  102. $mail->Body = $message;
  103. $mail->AltBody = preg_replace( "/ns+/", "n", rtrim(html_entity_decode(strip_tags($message))) ); // For clients that do not support html / "spam control"
  104.  
  105. $mail->send();
  106.  
  107. $data = array('success'=>'Message sent, thank you for contacting us!');
  108. outputJson($data);
  109.  
  110. } catch (Exception $e) {
  111. $data = array('mail_error'=>'Could not send email');
  112. outputJson($data);
  113. //echo 'Mailer Error: ' . $mail->ErrorInfo; // Debug message that you can see in network tab in web developer browser extension
  114. }
  115.  
  116. $name = $email = $phone = $message = $subject = '';
  117.  
  118. }
  119. }
  120.  
  121. // output json that you can parse with jquery
  122. function outputJson($data) {
  123. // output json that you can parse with jquery
  124. header('Content-Type: application/json');
  125. echo json_encode($data);
  126. exit;
  127. }
  128. function test_input($data) {
  129. $data = trim($data);
  130. $data = stripslashes($data);
  131. $data = htmlspecialchars($data);
  132. return $data;
  133. }
Add Comment
Please, Sign In to add comment