├── .gitignore ├── LICENSE ├── README.md └── ajax-contact-form.php /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | .idea -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2018 nK, https://nkdev.info/ 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # AJAX Contact Form 2 | PHP class for send emails using php `mail()` function. 3 | 4 | ## Configure PHP 5 | Configuration placed in variables on the top of main class in file `ajax-contact-form.php`. 6 | 7 | ## Configure HTML 8 | ```html 9 |
10 |

11 | 12 |

13 |

14 | 15 |

16 |

17 | 18 |

19 |

20 | 21 |

22 | 23 |
24 |
25 |
26 | ``` 27 | 28 | ## Configure JS 29 | Example with jQuery, but you can use plain JS. 30 | ```javascript 31 | $('.ajax-contact-form').on('submit', function(e) { 32 | e.preventDefault(); 33 | 34 | var $form = $(this); 35 | var $responseSuccess = $form.find('.ajax-contact-form-response-success'); 36 | var $responseError = $form.find('.ajax-contact-form-response-error'); 37 | 38 | $.ajax({ 39 | type: 'POST', 40 | url: $form.attr('action'), 41 | data: $form.serialize(), 42 | success: function(response) { 43 | response = JSON.parse(response); 44 | if (response.type && response.type === 'success') { 45 | $responseError.hide(); 46 | $responseSuccess.html(response.response).show(); 47 | $form[0].reset(); 48 | } else { 49 | $responseSuccess.hide(); 50 | $responseError.html(response.response).show(); 51 | } 52 | }, 53 | error: function(response) { 54 | $responseSuccess.hide(); 55 | $responseError.html(response.responseText).show(); 56 | } 57 | }); 58 | }); 59 | ``` 60 | 61 | ## Thanks 62 | We created this class for our premium templates https://nkdev.info/ 63 | 64 | ## License 65 | Copyright (c) 2018 nK Licensed under the MIT license. 66 | -------------------------------------------------------------------------------- /ajax-contact-form.php: -------------------------------------------------------------------------------- 1 | 8 | * @link https://github.com/nk-o/ajax-contact-form 9 | * @version 1.0.0 10 | * @license MIT License 11 | */ 12 | class Ajax_Contact_Form { 13 | /** 14 | * Message destination email. 15 | * 16 | * @var string 17 | */ 18 | protected $address_destination = 'email@example.com'; 19 | 20 | /** 21 | * Message subject 22 | * 23 | * @var string 24 | */ 25 | protected $message_subject = 'Message from AJAX Contact Form'; 26 | 27 | /** 28 | * Strings to translate or change it. 29 | * 30 | * @var array 31 | */ 32 | protected $strings = array( 33 | 'body' => ' 34 |

{{subject}}

35 |

From: {{name}}

36 |

E-Mail: {{email}}

37 |

Message:
{{message}}

', 38 | 'success' => 'Thank You! I will be in touch.', 39 | 'error' => 'Sorry there was an error sending your message. Please check server PHP mail configuration.', 40 | 'demo' => 'This is demo message from PHP', 41 | 'header_injection' => 'Header injection detected.', 42 | 'enter_name' => 'Please enter your name.', 43 | 'enter_email' => 'Please enter a valid email address.', 44 | 'enter_message' => 'Please enter your message.', 45 | 'ajax_only' => 'Allowed only XMLHttpRequest.', 46 | ); 47 | 48 | /** 49 | * Demo mode, will return always success and demo message without email send. 50 | * 51 | * @var bool 52 | */ 53 | protected $demo = false; 54 | 55 | /** 56 | * nK_Contact_Form constructor. 57 | */ 58 | public function __construct() { 59 | // Demo message. 60 | if ( $this->demo ) { 61 | $this->successHandler('demo'); 62 | } 63 | 64 | // Ajax check. 65 | if ( ! isset( $_SERVER['HTTP_X_REQUESTED_WITH'] ) || 'XMLHttpRequest' !== $_SERVER['HTTP_X_REQUESTED_WITH'] ) { 66 | $this->errorHandler('ajax_only'); 67 | } 68 | 69 | // Get post data. 70 | $name = stripslashes(trim($_POST['name'])); 71 | $email = stripslashes(trim($_POST['email'])); 72 | $message = stripslashes(trim($_POST['message'])); 73 | 74 | // Sanitize fields. 75 | $name = filter_var($name, FILTER_SANITIZE_STRING); 76 | $email = filter_var($email, FILTER_SANITIZE_EMAIL); 77 | $message = filter_var($message, FILTER_SANITIZE_STRING); 78 | $message = nl2br($message, false); // false gives
, true gives
79 | 80 | // Check header injection. 81 | $pattern = '/[\r\n]|Content-Type:|Bcc:|Cc:/i'; 82 | if ( preg_match($pattern, $name) || preg_match($pattern, $email) ) { 83 | $this->errorHandler('header_injection'); 84 | } 85 | 86 | // Validate email. 87 | $isEmailValid = filter_var($email, FILTER_VALIDATE_EMAIL); 88 | 89 | // Check if name has been entered. 90 | if ( ! $name ) { 91 | $this->errorHandler('enter_name'); 92 | } 93 | 94 | // Check if email has been entered and is valid. 95 | if ( ! $isEmailValid || ! $email ) { 96 | $this->errorHandler('enter_email'); 97 | } 98 | 99 | // Check if message has been entered. 100 | if ( ! $message ) { 101 | $this->errorHandler('enter_message'); 102 | } 103 | 104 | // Prepare headers. 105 | $headers = 'MIME-Version: 1.1' . PHP_EOL; 106 | $headers .= 'Content-type: text/html; charset=utf-8' . PHP_EOL; 107 | $headers .= "From: $name <$email>" . PHP_EOL; 108 | $headers .= "Return-Path: $this->address_destination" . PHP_EOL; 109 | $headers .= "Reply-To: $email" . PHP_EOL; 110 | $headers .= "X-Mailer: PHP/". phpversion() . PHP_EOL; 111 | 112 | // Prepare body. 113 | $body = $this->getString('body'); 114 | $body = $this->template( $body, array( 115 | 'subject' => $this->message_subject, 116 | 'name' => $name, 117 | 'email' => $email, 118 | 'message' => $message, 119 | ) ); 120 | $body = " 121 | 122 | 123 | 124 | {$this->message_subject} 125 | 126 | 127 | 128 | {$body} 129 | "; 130 | 131 | // If there is no error, send the email. 132 | $result = @mail($this->address_destination, $this->message_subject, $body, $headers); 133 | if ( $result ) { 134 | $this->successHandler('success'); 135 | } else { 136 | $this->errorHandler('error'); 137 | } 138 | } 139 | 140 | /** 141 | * Template string. 142 | * 143 | * @param $string 144 | * @param $vars 145 | * 146 | * @return string 147 | */ 148 | public function template($string, $vars) { 149 | foreach ( $vars as $name => $val ) { 150 | $string = str_replace("{{{$name}}}", $val, $string); 151 | } 152 | return $string; 153 | } 154 | 155 | /** 156 | * Get string from $string variable. 157 | * 158 | * @param $string 159 | * 160 | * @return string 161 | */ 162 | public function getString($string) { 163 | return isset( $this->strings[$string] ) ? $this->strings[$string] : $string; 164 | } 165 | 166 | /** 167 | * Error result. 168 | * 169 | * @param $message 170 | */ 171 | public function errorHandler($message) { 172 | die(json_encode(array( 173 | 'type' => 'error', 174 | 'response' => $this->getString($message), 175 | ))); 176 | } 177 | 178 | /** 179 | * Success result. 180 | * 181 | * @param $message 182 | */ 183 | public function successHandler($message) { 184 | die(json_encode(array( 185 | 'type' => 'success', 186 | 'response' => $this->getString($message), 187 | ))); 188 | } 189 | } 190 | new Ajax_Contact_Form(); 191 | --------------------------------------------------------------------------------