Sinambela

Untitled

Apr 3rd, 2016
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.58 KB | None | 0 0
  1. <?php
  2. /*
  3. * Contact Form Class
  4. */
  5.  
  6.  
  7. header('Cache-Control: no-cache, must-revalidate');
  8. header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
  9. header('Content-type: application/json');
  10.  
  11. $admin_email = '[email protected]'; // Your Email
  12. $message_min_length = 5; // Min Message Length
  13.  
  14.  
  15. class Contact_Form{
  16. function __construct($details, $email_admin, $message_min_length){
  17.  
  18. $this->name = stripslashes($details['name']);
  19. $this->email = trim($details['email']);
  20. $this->subject = '[email protected]'; // Subject
  21. $this->message = stripslashes($details['message']);
  22.  
  23. $this->email_admin = $email_admin;
  24. $this->message_min_length = $message_min_length;
  25.  
  26. $this->response_status = 1;
  27. $this->response_html = '';
  28. }
  29.  
  30.  
  31. private function validateEmail(){
  32. $regex = '/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i';
  33.  
  34. if($this->email == '') {
  35. return false;
  36. } else {
  37. $string = preg_replace($regex, '', $this->email);
  38. }
  39.  
  40. return empty($string) ? true : false;
  41. }
  42.  
  43.  
  44. private function validateFields(){
  45. // Check name
  46. if(!$this->name)
  47. {
  48. $this->response_html .= '<p>Please enter your name</p>';
  49. $this->response_status = 0;
  50. }
  51.  
  52. // Check email
  53. if(!$this->email)
  54. {
  55. $this->response_html .= '<p>Please enter an e-mail address</p>';
  56. $this->response_status = 0;
  57. }
  58.  
  59. // Check valid email
  60. if($this->email && !$this->validateEmail())
  61. {
  62. $this->response_html .= '<p>Please enter a valid e-mail address</p>';
  63. $this->response_status = 0;
  64. }
  65.  
  66. // Check message length
  67. if(!$this->message || strlen($this->message) < $this->message_min_length)
  68. {
  69. $this->response_html .= '<p>Please enter your message. It should have at least '.$this->message_min_length.' characters</p>';
  70. $this->response_status = 0;
  71. }
  72. }
  73.  
  74.  
  75. private function sendEmail(){
  76. $mail = mail($this->email_admin, $this->subject, $this->message,
  77. "From: ".$this->name." <".$this->email.">\r\n"
  78. ."Reply-To: ".$this->email."\r\n"
  79. ."X-Mailer: PHP/" . phpversion());
  80.  
  81. if($mail)
  82. {
  83. $this->response_status = 1;
  84. $this->response_html = '<p>Thank You!</p>';
  85. }
  86. }
  87.  
  88.  
  89. function sendRequest(){
  90. $this->validateFields();
  91. if($this->response_status)
  92. {
  93. $this->sendEmail();
  94. }
  95.  
  96. $response = array();
  97. $response['status'] = $this->response_status;
  98. $response['html'] = $this->response_html;
  99.  
  100. echo json_encode($response);
  101. }
  102. }
  103.  
  104.  
  105. $contact_form = new Contact_Form($_POST, $admin_email, $message_min_length);
  106. $contact_form->sendRequest();
  107.  
  108. ?>
Advertisement
Add Comment
Please, Sign In to add comment