/ Published in: PHP
Form needs to call the page like this:
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
<?php // CONFIGURATION -------------------------------------------------------------- // This is the email where the contact mails will be sent to. // This is the subject line for contact emails. // The variable %name% will be replaced with the name of the sender. $config['subject'] = 'Contact message from %name%'; // These are the messages displayed in case of form errors. ( 'no_name' => 'Please enter your name', 'no_email' => 'Your Email adresse is required.', 'invalid_email' => 'You entered an invalid email address.', 'no_message' => 'Please, include your message.', ); // END OF CONFIGURATION ------------------------------------------------------- // Ignore non-POST requests if ( ! $_POST) // Was this an AJAX request or not? $ajax = (isset($_SERVER['HTTP_X_REQUESTED_WITH']) AND strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest'); // Set the correct HTTP headers // Extract and trim contactform values // Take care of magic quotes if needed (you really should have them disabled) { } // Initialize the errors array which will also be sent back as a JSON object $errors = NULL; // Validate name { $errors['name'] = $config['errors']['no_name']; } // Validate email if ($email == '') { $errors['email'] = $config['errors']['no_email']; } elseif ( ! preg_match('/^[-_a-z0-9\'+*$^&%=~!?{}]++(?:\.[-_a-z0-9\'+*$^&%=~!?{}]+)*+@(?:(?![-.])[-a-z0-9.]+(?<![-.])\.[a-z]{2,6}|\d{1,3}(?:\.\d{1,3}){3})(?::\d++)?$/iD', $email)) { $errors['email'] = $config['errors']['invalid_email']; } // Validate message if ($message == '') { $errors['message'] = $config['errors']['no_message']; } // Validation succeeded { // Prepare subject line // Additional mail headers $headers = 'Content-Type: text/plain; charset=utf-8'." "; $headers .= 'From: '.$email; // Send the mail { $errors['server'] = 'There seems to be a technical problem with our server. We are sorry. '. 'Could you mail your message directly at '.$config['recipient'].'? Thank you.'; } } if ($ajax) { // Output the possible errors as a JSON object } else { // Show a simple HTML feedback message in case of non-javascript support { echo '<h1>Thank you</h1>'; echo '<p>Your message has been sent.</p>'; } else { echo '<h1>Oops!</h1>'; echo '<p>Please go back and fix the following errors:</p>'; echo '<ul><li>'; echo '</li></ul>'; } }