Code
// Initialize the $status variable which contains any information to be displayed to the user.
$status = "<p>Please fill out all fields.</p>\n";
// Detect if the form has been submitted.
if(count($_POST) > 0){
// Create an empty array to store a list of errors
$error_list = array();
// Store the supplied information from the form into their own variables.
$name = $_POST['name'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$message_body = $_POST['message_body'];
// Check for empty values in the supplied data.
// If they left any fields blank, store a message in the $error_list array.
if(empty($name)){
$error_list[] = 'Name';
}
if(empty($phone)){
$error_list[] = 'Phone Number';
}
if(empty($email)){
$error_list[] = 'Email';
}
else{
// Check for properly formatted email.
if(!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email)){
list($username, $domain) = split ("@",$email);
// If email is formatted correctly, check for valid host.
if(getmxrr($domain, $MXHost)){
$ConnectAddress = $MXHost[0];
$connect = fsockopen($ConnectAddress, 25);
// If host is valid, check for email address on server.
if($connect){
if(ereg("^220", $out = fgets($connect, 1024))){
fputs($connect, "HELO $HTTP_HOST\r\n");
$out = fgets( $connect, 1024 );
fputs($connect, "MAIL FROM: <{$email}>\r\n");
$from = fgets( $connect, 1024 );
fputs($connect, "RCPT TO: <{$email}>\r\n");
$to = fgets($connect, 1024);
fputs($connect, "QUIT\r\n");
fclose($connect);
if(!ereg ("^250", $from) || !ereg ( "^250", $to )){
$error_list[] = 'Valid Email Address';
}
}
else{
$error_list[] = 'Valid Email Address';
}
}
else{
$error_list[] = 'Valid Email Address';
}
}
else{
$error_list[] = 'Valid Email Address';
}
}
}
if(empty($message_body)){
$error_list[] = 'Message';
}
// Count the number of elements in the $error_list array.
// If there are more than zero, reset the status message to an XHTML unordered list
// containing one list item for every element (error message) in the array
if(count($error_list) > 0) {
$status = "<p>You did not provide your:</p>\n<ul>\n";
foreach($error_list as $error){
$status .= "\t<li><strong>$error</strong></li>\n";
}
$status .= "</ul>\n";
}
// If no errors were found (no fields were blank), then send mail.
else{
$name = $_POST['name'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$message_body = $_POST['message_body'];
$header = 'from:contact';
$message = "$name\n";
$message .= "$email\n";
$message .= "$phone\n";
$message .= "\n";
$message .= "\n";
$message .= "$message_body\n";
if(mail('example@example.com','Email Subject Here',$message,"from:$email")){
$status = "<p><strong>Your message has been sent!</strong></p>\n ";
}
else{
$status = "<p><strong>An error occurred, please try again.</strong></p>";
}
}
}
<?php echo $status; ?>
<form name="contact" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<dl>
<dt><label for="name"><strong>Your Name:</strong></label></dt>
<dt><input class="field" type="text" name="name" id="name" size="30" maxlength="30" value="<?php echo $name; ?>" /></dt>
<dt><label for="phone"><strong>Your Phone Number:</strong></label></dt>
<dt><input class="field" type="text" name="phone" id="phone" size="30" value="<?php echo $phone; ?>" /></dt>
<dt><label for="email"><strong>Your Email:</strong></label></dt>
<dt><input class="field" type="text" name="email" id="email" size="30" value="<?php echo $email; ?>" /></dt>
<dt><label for="message_body"><strong>Your Message:</strong></label></dt>
<dt><textarea class="field" rows="5" name="message_body" id="message_body" cols="50" dir="ltr"><?php echo $message_body; ?></textarea></dt>
<dt><input class="button" type="submit" value="Send" name="submit" /></dt>
</dl>
</form>
