Using PHP To Create E-Mail Feedback Forms
If you are writing your site in php, you can still use a form to allow your
visitors to send email to you. Below is an example on how you could accomplish
this. Items which are highlighted are items which
you will need to change to match your site.
Let's start the code with the small line below to allow browsers to know you
are using php.
<?php
Now, we have to test to see if a form was filled out or not.
if (isset($HTTP_POST_VARS["action"])) {
First let's read the form data into variables.
$name = $HTTP_POST_VARS['visitorname'];
$email = $HTTP_POST_VARS['email'];
$subject = $HTTP_POST_VARS['subject'];
$message = $HTTP_POST_VARS['message'];
We need to be very sure that the email is sent to you. Please never
give a visitor an option to fill in an email address to send to! People can
abuse this privilege and spam from your domain. If this happens, your domain
could be taken offline. Always make sure the To: field in your email
forms is fixed.
Replace the highlighted area with your own email address.
$your_mail = "postmaster@yourdomain.com";
Now let's set up the different headers that must go into the email. Note that we are using variables named $name and $email. These correspond with the name="" value in the input tags in the form we'll create in a moment. Do be careful when typing these lines in!
$mail_headers = "MIME-Version: 1.0\n";
$mail_headers .= "Content-type: text/html; charset=iso-8859-1\n";
$mail_headers .= "From: ".$name." <".$email.">\n";
Sometimes it's nice to send a copy email to the sender so they know it was sent. If you want to do this, add this line to the script here:
$mail_headers .= "Reply-To: ".$name." <".$email.">\n";
Next we can set the rest of the headers. It's best to leave these as shown below.
$mail_headers .= "X-Priority: 3\n";
$mail_headers .= "X-MSMail-Priority: Normal\n";
$mail_headers .= "X-Mailer: ".$_SERVER['SERVER_NAME'];
Now that we're done with the mail headers, we'll set one more item so that the web page parts will display correctly.
if (!headers_sent()) {
header("Pragma: no-cache");
}
The next few lines is what actually sends the email or lets the visitor know how things turned out. It's always best to put a link to the main page so they can return there after the message.
if(mail($your_mail, stripslashes($subject), stripslashes($message), $mail_headers)){
echo "Thank you for your email. I'll read it as soon as I can.<br><br>";
echo '<a href="http://yourdomain.com/">Back to Home Page</a>';
}else{
echo "Sorry, your email could not be sent.<br><br>";
echo 'Try again <a href="http://cgi.yourdomain.com/contact.php">here</a>';
}
Now we can write the code that will produce a form for visitors to fill in. This portion of the script will execute if the form had not already been filled out. This is where visitors go when they first encounter this script.
} else {
if (!headers_sent()) {
header("Pragma: no-cache");
}
?>
<div style="text-align:center;margin:auto">
<form method="post" action="<?php echo $_SERVER['PHP_SELF']?>">
<input type="hidden" name="action" value="send_mail">
<table style="border:none;text-align:center">
<tr>
<td style="text-align:right" valign="top">Name:</td>
<td><input type="text" name="visitorname" size="50"></td>
</tr><tr>
<td style="text-align:right" valign="top">E-Mail Address:</td>
<td><input type="text" name="email" size="50"></td>
</tr><tr>
<td style="text-align:right" valign="top">Subject:</td>
<td><input type="text" name="subject" size="50"></td>
</tr><tr>
<td style="text-align:right" valign="top">Comments:</td>
<td><textarea wrap="soft" name="message" cols="50" rows="8"></textarea></td>
</tr><tr>
<td align="center" colspan="2">
<input type="submit" value="Submit">
<input type="reset">
</td>
</tr>
</table>
</form>
</div>
<?php
}
?>
If you want to cut and paste the entire code to edit for your own use, select all the text in the box below, then copy and paste it into your own php file. Be sure to edit as needed.