php mail form tutorial

Php e-mail form is very important for website because a contact form or feed back form is one of the basic requirements of a website.In this tutorial you'll learn how to create php email form for contact page or feed back page .

First things first is making of HTML form that we will be used to take input information of user.Then we will make php script page for sending mail.

For more understading of form you may see tutorial Php Form Example.

Here is our form :
<form name="form1" action="send_mail.php" method="post">
<table>
<tr><td>Name</td><td><input type="text" name="name" id="name" /> </td></tr>
<tr><td>Email</td><td><input type="text" name="email" id="email" /> </td></tr>
<tr><td>Phone</td><td><input type="text" name="phone" id="phone" /> </td></tr>
<tr><td>Message </td><td>
<textarea name="message" id="message" rows="5" cols="36"></textarea>
</td></tr>
<tr><td></td><td><input type="submit" name="btn" id="btn" value="Submit" /> </td></tr>
</table>
</form>

As we see our HTML form is ready, save this html page with name contact.html. In our form, action attribute value send_mail.php is

set that means, we want to send user on send_mail.php page when user press submit button.We will use this page for sending mail.

In php mail() function is used to send emails. Syntax of mail function is as given below.


mail(to,subject,message,headers);


In mail function parameter "to" is for email of receiver.The parameter "subject" is used for email subject. The parameter "message" is for

the message to be sent. The paramter "headers" is optional and is used for bcc, cc etc. Now we have learnt about mail function, so we are

ready to write script for sending mail in send_mail.php.

<?php 
$name = $_POST['name'];
$email = $_POST['email'];
$message ="Name ".$name."<br/>";
$message. ="Email ".$email."<br/>";
$message. ="Message ".$_POST['message'];
$to = "receiveremailaddress@yourdomain.com";
$subject = "Contact Form";
$header = "From: $email \r\n";
mail($to, $subject, $message, $header) or die("Error!");
echo "Thank you for contacting us!"; ?>
Now our example is complete.You can customize script with our own style.