Here you go. It's PHP code for an email form that I got from somewhere and added code to stop email injection attack and make it validate. It posts to itself so you don't need thank you pages, all fields are required, it validates XHTML Strict and is tableless. Just put it into your page, change the $email_address value, add the CSS and you should be good to go.
PHP Code:
<?php //Start of the contact form
$email_address = "who@yourbusiness.com";
$sender_name = @$_POST[sender_name];
$sender_email = @$_POST[sender_email];
$subject = @$_POST[subject];
$message = @$_POST[message];
$op = @$_POST[op];
$form_block = "
<form method=\"post\" action=\"$_SERVER[PHP_SELF]\">
<fieldset>
<legend>All fields are required</legend>
<label for=\"sender_name\">Name:</label>
<input id=\"sender_name\" type=\"text\" name=\"sender_name\" value=\"$sender_name\" size=\"30\" /><br />
<label for=\"sender_email\">Your E-Mail:</label>
<input id=\"sender_email\" type=\"text\" name=\"sender_email\" value=\"$sender_email\" size=\"30\" /><br />
<label for=\"subject\">Subject</label>
<input id=\"subject\" type=\"text\" name=\"subject\" value=\"$subject\" size=\"30\" /><br />
<label for=\"message\">Message</label>
<textarea id=\"message\" name=\"message\" cols=\"30\" rows=\"5\">$message</textarea><br />
<input class=\"hidden\" type=\"hidden\" name=\"op\" value=\"ds\" /><br />
<input class=\"submitbutton\" type=\"submit\" name=\"submit\" value=\"Send E-Mail\" />
</fieldset>
</form>";
if ($op != "ds") {
// they need to see the form
echo "$form_block";
} else if ($op == "ds") {
//This is the line of code stopping the email injection attack
if(eregi("MIME-Version: ",$_POST['sender_name'].$_POST['sender_email'].$_POST['subject'].$_POST['message'])){die('Connection problem, try later.');
//end of code
}
if ($sender_name == "") {
// check value of $_POST[sender_name]
$name_err = "<p class=\"error\"> Please enter your name!</p>";
$send = "no";
}
if ($sender_email == "") {
// check value of $_POST[sender_email]
$email_err = "<p class=\"error\">Please enter your e-mail address!</p>";
$send = "no";
}
if ($subject == "") {
// check value of $_POST[sender_email]
$subject_err = "<p class=\"error\">Please enter a subject!</p>";
$send = "no";
}
if ($message == "") {
// check value of $_POST[message]
$message_err = "<p class=\"error\">Please enter a message!</p>";
$send = "no";
}
if (@$send != "no") {
// it's ok to send so build the mail
$to = "$email_address";
$subject = "$subject";
$mailheaders = "From: Your Business <$to> \n";
$mailheaders .= "Reply-To: $sender_email\n";
$msg = "E-MAIL SENT FROM THE PAGE\n";
$msg .= "Sender's Name: $sender_name\n";
$msg .= "Sender's E-Mail: $sender_email\n";
$msg .= "Message: $message\n";
mail($to, $subject, $msg, $mailheaders);
echo "<p>Thank you, $sender_name, your message has been sent. I will contact you as soon as possible.</p>";
} else if ($send == "no") {
echo "$name_err";
echo "$email_err";
echo "$subject_err";
echo "$message_err";
echo "$form_block";
}
}
?>
Here is the CSS to match it:
Code:
/* Form style inspired by Nadia P's http://www.dreamweaverresources.com/tutorials/styled_form.htm*/
form { width: 95%;
margin: 0 0 0 10px; }
label {
display: block;
width: 150px;
float: left;
margin-bottom: 10px;
text-align: right;
padding-right: 20px;
color:#003;
}
br {
clear: left;
}
fieldset{
padding:10px;
border:1px solid #036;
margin-bottom:15px;
}
input, textarea{
color: #000;
border: 1px solid #069;
background: url(images/formbg.jpg);
margin-top: 2px;
margin-bottom: 2px;
}
legend{
padding:10px;
color:#069;
font-size: 95%;
font-weight: bold;
}
.submitbutton {
border: 1px solid #036;
background:#CCF;
}
.hidden {
border:none;
}
Bookmarks