View Single Post
  #1 (permalink)  
Old 05-30-06, 09:18 AM
Grabber's Avatar
Grabber Grabber is offline
Newbie Coder
 
Join Date: Apr 2006
Posts: 87
Thanks: 0
Thanked 0 Times in 0 Posts
Tute: Create Email based Contact Form

Preface:

If you are a beginner in php and if you ever wanted to make a contact page that sends email to admin on submission then this post is for you.

This is a very simple Email Form in php that will help you to understand the process and would help you to make more complex forms.

Introduction:

There are two pages, Contact.html and SendMail.php,

Contact.html is a HTML form where the visitor will submit his name, email and message. and

SendMail.php will get the variables and would displatch email to admin containing all the information.

We will use PHP MAIL function for sending out email.

Following are the code for HTML Form called Contact.html

Contact.html
------------

Code:
<html> 
  <title>Sample Contact Page</title> 
    <body> 
       <form action=SendMail.php method=post> 
         Name :: <input type=text name=uname> 
          Email :: <input type=text name=uemail> 
         Message :: <br> <textarea name=message rows=10 cols=20>Write your message here </textarea> 
      <input type=submit value="Send Email to Admin"> <input type=reset value="Clear"> 
    </form> 
   </body> 
 </html>
Following is the code for SendMail.php that will accept form variables and will send Email.

SendMail.php
------------

PHP Code:

 <?php 

// define the email that you want to send the form 

$to "admin@yoursite.com"

// Get form variables 

  
$name $_POST[uname]; 
   
$email $_POST[uemail]; 
  
$msg $_POST[message]; 

// make the body of email 

$mailbody "Name: $name <br /> Email: $email <br /> Message: $msg"

// use mail function of php to send email 

 
if (mail($to,"Contact Form Submitted from your site",$mailbody,"\r\n From: $email") ) { 
  
     echo 
"Email Sent successfully"
   } 
  else 
     echo 
"There was some problem in sending email please refer to manual"

?>

I hope this will help you to get into more complex email based forms development.

Please feel free to send me your feedback about this tutorial and any other tutorial you may want to see here, I would love to add more to this forum.

Thanks
Reply With Quote