Contact us form – source code

Contact us – source code. share with everybody free.
keywords: contact us form php code, contact us php source code,联系我们源代码,php源代码

/**
* A PHP contact form.
* Author: donny12345@gmail.com
* Last revision: 01/15/2011 01:03
*
* Mail header injection prevention based on comments from:
* http://www.php.net/mail
*
*/
// Replace you@example.com with your own e-mail address.
define('YOUR_EMAIL', 'you@example.com');
// If the user does not fill in a subject, this will be used.
define('DEFAULT_SUBJ', 'A message from your contact form');
// This is the maximum length of a subject, in characters.
define('MAX_SUBJ_LEN', 1000);
if (isset($_POST['mail'])) {
$errors = array();
// Sanitize the subject;
if (preg_match('/(%0A|%0D|\\n+|\\r+)/i', $_POST['subj'])) {
$errors[] = 'Your subject contains illegal characters.';
} else {
if (!strlen($_POST['subj']) || is_null($_POST['subj'])) {
$subj = DEFAULT_SUBJ;
} else {
$subj = substr($_POST['subj'], 0, MAX_SUBJ_LEN);
}
}
// Validate their e-mail address.
if (!preg_match('/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i', $_POST['email'])) {
$errors[] = 'Invalid e-mail address.';
}
// Validate the body.
if (preg_match('/(%0A|%0D|\\n+|\\r+)(content-type:|to:|cc:|bcc:)/i', $_POST['body'])) {
$errors[] = 'Your message body contains invalid characters.';
}
if (!strlen($_POST['body'])) {
$errors[] = 'The body of your message was blank.';
}
if (count($errors)) {
for ($i = 0; $i < count($errors); $i++) {
printf('

%s

', $errors[$i]);
}
} else {
$headers = sprintf("From: %s\r\n", $_POST['email']);
if (mail(YOUR_EMAIL, $subj, $_POST['body'], $headers)) {
print '

Your message was sent.

';
} else {
print '

An error occurred while we were attempting to'
.' send your message. Please try again later.

';
}
}
}
?>




// Replace you@example.com with your own e-mail address.define('YOUR_EMAIL', 'you@example.com');
// If the user does not fill in a subject, this will be used.define('DEFAULT_SUBJ', 'A message from your contact form');
// This is the maximum length of a subject, in characters.define('MAX_SUBJ_LEN', 1000);
if (isset($_POST['mail'])) { $errors = array(); // Sanitize the subject; if (preg_match('/(%0A|%0D|\\n+|\\r+)/i', $_POST['subj'])) { $errors[] = 'Your subject contains illegal characters.'; } else { if (!strlen($_POST['subj']) || is_null($_POST['subj'])) { $subj = DEFAULT_SUBJ; } else { $subj = substr($_POST['subj'], 0, MAX_SUBJ_LEN); } } // Validate their e-mail address. if (!preg_match('/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i', $_POST['email'])) { $errors[] = 'Invalid e-mail address.'; } // Validate the body. if (preg_match('/(%0A|%0D|\\n+|\\r+)(content-type:|to:|cc:|bcc:)/i', $_POST['body'])) { $errors[] = 'Your message body contains invalid characters.'; } if (!strlen($_POST['body'])) { $errors[] = 'The body of your message was blank.'; }
if (count($errors)) { for ($i = 0; $i < count($errors); $i++) { printf('

%s

', $errors[$i]); } } else { $headers = sprintf("From: %s\r\n", $_POST['email']); if (mail(YOUR_EMAIL, $subj, $_POST['body'], $headers)) { print '

Your message was sent.

'; } else { print '

An error occurred while we were attempting to' .' send your message. Please try again later.

'; } }}?>