PHP - Email Form

From Global Programming Syntax

Jump to: navigation, search

As an owner of a website, one of the things that you really need is feedback. To find out what your visitors think and what needs to change to suite your visitor/customer needs. The easiest way to receive this feedback without the feedback entering public domain is having an email form. An email form as shown below is just a regular html form which then submits the data to a server where it is processed. In this case it is being processed by a php script which then gets reprocessed by the servers email software. Then the servers email software submits the compiled data to its destination where the user (you in this case) can receive the email through the web browser or a specialized email program. Below is a picture of the example email form and to the right of the picture is the source code used. Of course it can be altered to suite your needs and styling. Also this email form does allow you to input html code into the message field turning it into a styled email when receiving it.

Image:phpemailform.jpg

<?
if (isset($_POST['subject']) && $_POST['message']!=='')
{
$address = "your_real_email@domain.com"; //change this to the receiving address.
$subject = "Website Email: ".$_POST['subject'];
if ($_POST['name']!=='')
{
$body = "<table border=0 cellpadding=5 cellspacing=0 width=200><tr><td>".$_POST['message']."<br>
<br>
Yours Sincerely<br>
"
.$_POST['name']."</td></tr></table>";
} else {
$body = "<table border=0 cellpadding=5 cellspacing=0 width=200><tr><td>".$_POST['message'].
"</td></tr></table>";
}
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: do_not_reply@your_website_form' . "\r\n";
 
mail($address,$subject,$body,$headers);
}
//below displays the form while above processes it.
echo "<form method='post'>Subject: <input type=text value='' maxlength=100 name='subject' size=30><br>
<textarea cols=30 rows=20 name='message'></textarea><br>Name: <input type='text' value='' name='name'>
<input type='submit' value='submit'></form>"
;
?>
Personal tools
languages
page stats
Toolbox