Hey, just made this for one of my sites, so I thought I'd let you guys use it.
PHP Code:
<?php
if (isset($_POST['submit'])) {
// Just make all the info good to work with
$to = "admin@example.com";
$subject = "Example.com Contact Form";
$name = trim(stripslashes($_POST['name']));
$email = trim(stripslashes($_POST['email']));
$message = trim(stripslashes($_POST['message']));
$reason = ucfirst($_POST['reason']);
// Check all fields for values
$error = false;
if (trim($name) == "")
$error = true;
if (trim($email) == "")
$error = true;
if (trim($message) == "")
$error = true;
if ($error == true) {
echo "<span class=\"error\">One of the fields has been left blank. Please fill out all fields and try again.</span>\n";
} else {
// Make the email all pretty
$body = "Name: ".$name."\n";
$body .= "Email: ".$email."\n";
$body .= "Reason for Contact: ".$reason."\n";
$body .= "Message:\n".$message;
// Headers (thank you Patrick Paul)
$headers = "MIME-Version: 1.0\n";
$headers .= "Content-type: text/plain; charset=iso-8859-1\n";
$headers .= "X-Priority: 3\n";
$headers .= "X-MSMail-Priority: Normal\n";
$headers .= "X-Mailer: php\n";
$headers .= "From: ".$email."\n";
// Send Email
$sent = mail($to, $subject, $body, $headers);
// Print results
if ($sent) {
echo "<p>Your email has been sent. Please allow time for a reply.</p>\n";
} else {
echo "<span class=\"error\">There has been an error while trying to send your email. Manually send an email to the webmaster to have this problem corrected.</span>\n";
}
}
}
?>
Code:
<form action="<?php $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data">
<p>Name:</p>
<input type="text" name="name" />
<p>Email:</p>
<input type="text" name="email" />
<p>Reason for contact:</p>
<select name="reason">
<option name="1" value="support">Support</option>
<option name="2" value="feedback">Feedback</option>
<option name="3" value="feature request">Feature Request</option>
</select>
<p>Message:</p>
<textarea name="message" rows="6" cols="40"></textarea>
<p><input type="submit" name="submit" value="Submit Form" /></p>
</form>