Here is one of mine. If you have any questions, let me know.
Fill in the CONFIGURATION part. Save it to a file and put it in your public web directory.
Feedback appreciated. If someone can improve this, please post your improvements so I can use them too.
PHP Code:
<?php
if ( isset($_POST['contact_submitted']) ) {
// CONFIGURATION
$yourName = ''; // Your Name
$sendTo = ''; // Where to send email
$subject = 'Your Contact Form'; // Subject of the email you will receive
$from = 'From: Your Website <webmaster@yourwebsite.com>' . "\n"; // Enter your website name and website email
$possibleReferers = array('http://www.yourwebsite.com/contactform.php'); // Enter the full URL of this script
// END CONFIGURATION
$errors = array();
if ( preg_match('/^[A-Za-z0-9](([_\.\-]?[a-zA-Z0-9]+)*)@([A-Za-z0-9]+)(([\.\-]?[a-zA-Z0-9]+)*)\.([A-Za-z]{2,})$/', $_POST['email']) ) {
$email = trim(substr($_POST['email'],0,60));
} else {
$errors[] = '<p>Please enter a valid email address.</p>';
}
$name = !empty($_POST['name']) ? strip_tags(ucwords(trim(substr($_POST['name'],0,56)))) : 'Not Provided';
$rawMessage = !empty($_POST['message']) ? strip_tags(trim(substr($_POST['message'],0,1000))) : 'Not Provided';
$validReferer = in_array($_SERVER['HTTP_REFERER'], $possibleReferers ) ? true : false;
if (!$validReferer) {
mail($to, 'Invalid Referer', 'INVALID ATTEMPT from: ' . $_SERVER['HTTP_REFERER'] , "From: $yourName <$sendTo>\n");
exit;
}
if ( empty($errors) ) {
$message = "From: $name\n\n";
$message .= "Email: $email\n\n";
$message .= "Message:\n\n";
$message .= wordwrap($rawMessage, 60, "\n");
if ( mail($sendTo, $subject, $message, $from) ) {
$success = true;
} else {
$errors[] = '<p>Sorry, there is a problem sending your email. Please try again.</p>';
}
}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" lang="en-US">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta http-equiv="content-language" content="en" />
<meta name="description" content="" />
<meta name="keywords" content="" />
<meta name="author" content="" />
<title>Contact Form</title>
<link href="" rel="stylesheet" type="text/css" media="screen" />
<style type="text/css">
label { display: block; }
.success { padding: .5em; background: #BFF8BF; border: 1px solid #008C00; padding: .5em; }
.error { border: 1px solid #f00; }
</style>
</head>
<body class="">
<?php if ( isset($success) ) { ?>
<div class="success">
<p>Thanks for contacting me.</p>
</div>
<?php } else { ?>
<form id="contact" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<fieldset>
<?php if ( isset($errors) ) { ?>
<div class="error">
<?php foreach ($errors as $error) {
echo "$error";
} ?>
</div>
<?php } ?>
<label id="email_label" for="email">Email: (required)</label>
<input id="email" class="text" type="text" name="email" maxlength="60" />
<label for="name">Name:</label>
<input id="name" class="text" type="text" name="name" maxlength="26" />
<label for="message">Message:</label>
<textarea id="message" name="message" rows="12" cols="50"></textarea>
<input type="hidden" name="contact_submitted" value="1" />
<input class="submit" type="submit" name="submit" value="Send" />
</fieldset>
</form> <!-- end contact -->
<?php } ?>
</body>
</html>
Good Luck!
Bookmarks