Guest User

Untitled

a guest
Jul 21st, 2018
288
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.07 KB | None | 0 0
  1. <?php
  2. add_action( 'admin_footer', 'html_form_code' ); // Write our JS below here
  3. //
  4. function html_form_code() {
  5. ?>
  6. <form action="<?php esc_url( $_SERVER['REQUEST_URI'] )?>" method="post" class="contact-form" id="contact-form" >
  7. <div class=header-contact>
  8. <p><h2>Contact Form</h2></p>
  9. <hr>
  10. </div>
  11. <div class=input-containers>
  12. <input type="text" id="name" name="cf-name" pattern="[a-zA-Z0-9 ]+" value="" size="40" placeholder="Име и фамилия"/>
  13. </div>
  14. <div class=input-containers>
  15. <input type="email" id="email" name="cf-email" value="" size="40" placeholder="Поща"/>
  16. </div>
  17. <div class=input-containers>
  18. <input type="text" id="subject" name="cf-subject" pattern="[a-zA-Z ]+" value="" size="40" placeholder="Относно"/>
  19. </div>
  20. <div class=input-containers>
  21. <textarea rows="10" id="message" cols="35" name="cf-message" placeholder="Текст"></textarea>
  22. </div>
  23. <div class=input-containers>
  24. <input type="submit" name="cf-submitted" value="Send" id="submitForm">
  25. </div>
  26. <p id="verify" style="display:none;">Your message has been sent.<br /><br /></p>
  27. </form>
  28. <script>
  29. //$("#btn").click(function(e){
  30. jQuery(document).ready(function($) {
  31. $('#contact-form').submit(function(e) {
  32. e.preventDefault();
  33. //e.stopPropagation();
  34.  
  35. $.ajax({
  36. type: 'post',
  37. url: '/',
  38. success: function () {
  39. //alert(formData);
  40. $('#verify').show();
  41. $('#verify').fadeIn().html("gff");
  42. setTimeout(function() {
  43. $('#verify').fadeOut("slow");
  44. }, 5000 );
  45. $(".contact-form")[0].reset();
  46. },
  47. error: function() {
  48. alert('Error!');
  49. }
  50. });
  51. });
  52. });
  53. </script>
  54. <?php
  55. }
  56. add_action( 'wp_ajax_my_action', 'deliver_mail' );
  57. add_action( 'wp_ajax_nopriv_my_action', 'deliver_mail' );
  58.  
  59. function deliver_mail() {
  60. require_once "wp-includes/class-phpmailer.php";
  61. if (isset($_POST["cf-submitted"])) {
  62.  
  63. // sanitize form values
  64. $name = sanitize_text_field( $_POST["cf-name"] );
  65. $email = sanitize_email( $_POST["cf-email"] );
  66. $subject = sanitize_text_field( $_POST["cf-subject"] );
  67. $message = esc_textarea( $_POST["cf-message"] );
  68.  
  69. // get the blog administrator's email address
  70.  
  71. //$to = "email@gmx.com";
  72. $headers = "From: $name <$email>" . "rn";
  73.  
  74. // Localhost
  75. $mail = new PHPMailer(true);
  76. $mail->IsSMTP(); // telling the class to use SMTP
  77. $mail->CharSet = 'UTF-8';
  78.  
  79. $mail->SMTPDebug = 0; // enables SMTP debug information (for testing)
  80. $mail->SMTPAuth = true; // enable SMTP authentication
  81. $mail->SMTPSecure = "ssl"; // sets the prefix to the servier
  82. $mail->Host = "mail.gmx.com"; // sets GMX as the SMTP server for example: mail.gmx.com
  83. $mail->Port = 465; // set the SMTP port for the GMX server
  84.  
  85.  
  86. $mail->Username = $email;
  87. $mail->Password = 'pass';
  88.  
  89. $mail->SetFrom($email, $name);
  90. $mail->AddAddress($email);
  91.  
  92. $mail->Subject = $subject;
  93. $mail->MsgHTML($message);
  94.  
  95. $headers .= "Content-Type: text/html; charset=utf-8";
  96. $headers .= "Content-Transfer-Encoding: 8bit";
  97.  
  98. try {
  99. $mail->send();
  100. $msg = "An email has been sent for verfication.";
  101. $msgType = "success";
  102.  
  103. wp_safe_redirect( home_url(), 302 );
  104. exit();
  105.  
  106. } catch (Exception $ex) {
  107. $msg = $ex->getMessage();
  108. $msgType = "warning";
  109.  
  110. wp_safe_redirect( home_url(), 302 );
  111. exit();
  112. }
  113.  
  114. add_action( 'init', function() {
  115. if ( ! empty( $_POST['form_submitted'] ) ) {
  116. deliver_mail();
  117. }
  118. });
  119.  
  120. function cf_shortcode() {
  121. ob_start();
  122. //deliver_mail();
  123. html_form_code();
  124.  
  125. //ob_end_flush();
  126. return ob_get_clean();
  127. }
  128. add_shortcode( 'contact_form_second', 'cf_shortcode' );
Add Comment
Please, Sign In to add comment