Current location: Hot Scripts Forums » Programming Languages » PHP » Tute: Create Email based Contact Form


Tute: Create Email based Contact Form

Reply
  #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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 05-30-06, 01:27 PM
astrkalj's Avatar
astrkalj astrkalj is offline
Newbie Coder
 
Join Date: May 2006
Location: Saint Louis, Missouri
Posts: 66
Thanks: 0
Thanked 0 Times in 0 Posts
Woah! Talk about bad code!

This is the way I would have done it:

HTML (email.html)
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Send Us An E-Mail!</title>
</head>
<body>
<form name="send_email" id="send_email" method="post" action="email.php">
<div><strong>Full Name:</strong> <input type="text" name="full_name" id="full_name" maxlength="100" /></div>
<div><strong>E-Mail Address:</strong> <input type="text" name="email_address" id="email_address" maxlength="100" /></div>
<div><strong>Message:</strong> <textarea name="message" id="message"></textarea></div>
<div><input type="submit" name="submit" id="submit" value="Submit E-Mail" /> <input type="reset" name="reset" id="reset" value="Reset Form" /></div>
</form>
</body>
</html>

PHP (email.php);
PHP Code:

<?php

if(!isset($_POST['submit'])) {
        include 
"email.html";
        exit();
} else {
        
$errors = array();
        
$full_name trim($_POST['full_name']);
        
$email_address trim($_POST['email_address']);
        
$message trim($_POST['message']);
        
$full_name htmlentities(strip_tags(preg_replace("/(\!|\@|\#|\$|\%|\^|\&|\*|\(|\))/"""$full_name)));
        if(!
preg_match("/^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/"$email_address)) {
                
$errors['email_address'] = "Invalid E-Mail Address!";
        }
        if(
count($errors) != 0) {
                echo 
$errors;
                exit();
        } else {
                
// ...
        
}
}
?>
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #3 (permalink)  
Old 05-30-06, 03:26 PM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,074
Thanks: 11
Thanked 88 Times in 83 Posts
Well, if everybody does... lol

I wrote this a while back

PHP Code:

<?php


$recipient 
'you@domain.com';            // Your email address.
$subject 'Contact form submission';    // Subject form email.
$bgcolor '#FFCC00';                     // Background color for empty fields.

// Message for user when message sent.
$message '<p>Thank you, '$_POST['name'] .'. Your message has been sent!</p>';
$message .= '<p><a href="javascript:history.go(-1);">Click here to return</a></p>';

/* 
    Fields:
    Do not use spaces or special chars. 
    Use underscores instead, they will be replaced with spaces.
*/
$fields = array(
            
"name"
            
"mail"
            
"domains"
            
"FTPs"
            
"database"
            
"email_addresses"
            
"email_lists"
            
"cron_jobs"
            
"IP_addresses"
            
"extra_demands",
                );

################################ DO NOT EDIT BELOW ###################################

if (isset($_POST["Submit"]))
{
    
// Checks for empty fields and prepares error variables for the colored background
    
foreach ($fields as $key)
    {
        if (
trim($_POST[$key]) == '')
        {
            
$error[$key] = 1;
        }
    }
    
    if (!isset(
$error))
    {    
        
// Prepare string
        
$string "--------------------------------------\n";
        
$string .= "Date: "date('Y/m/d H:i:s') ."\n";
        
        
// Prepare string for each submission
        
foreach ($_POST as $submission => $value)
        {
            if (
$value == 'Submit'// Skips the submit button
            
{
                continue;
            }
            
            
$string .= str_replace('_'' 'ucfirst($submission)) .': 'stripslashes($value) ."\n";
        }
        
        
$string .= "--------------------------------------\n";
        
$header 'From: "'$recipient .'" <'$recipient .'>';
        
        
mail($recipient$subject$string$header);
                
        
// Show message to user when message sent
        
die($message);
    }
}

    
// HTML Output start
    
echo '<form action="'basename($PHP_SELF) .'" method="post">' ."\n";
    echo 
'<table cellspacing="2" align="center">' ."\n";

    
// Prints all fields
    
foreach ($fields as $field)
    {
        
// Check if an error is set for this field and set background color if necessary
        
if (isset($error[$field]))
        {
            
$style[$field] = ' style="background-color:'$bgcolor .'"';
        }
        
        echo 
'<tr>'"\n";
        echo 
'<td><input type="text" name="'$field .'" value="'stripslashes($_POST[$field]) .'"'$style[$field]  .'/></td>'"\n";
        echo 
'<td> 'str_replace('_'' 'ucfirst($field)) .'</td>';
        echo 
'</tr>'"\n";
    }
    
    echo 
'<td colspan="2" align="center"><input type="submit" name="Submit" value="Submit" /></td>';
    echo 
'</table>';
    echo 
'</form>';
    
?>
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #4 (permalink)  
Old 05-31-06, 04:11 AM
Grabber's Avatar
Grabber Grabber is offline
Newbie Coder
 
Join Date: Apr 2006
Posts: 87
Thanks: 0
Thanked 0 Times in 0 Posts
After reading replies I think i have started code competition and not a tutorial to help beginners .. guys I know every trick in the book to write a standard code but my main purpose was to give a quick start to beginners in writing email based contact forms...

anyway, I like it in fact this thread is an awesome one now for beginners to get started with Email Forms.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
need help to create a attachment on my email frankkk PHP 1 10-25-04 03:10 PM
Declared Functions skipper23 PHP 4 12-17-03 11:06 AM
index page not showing up skipper23 PHP 3 12-15-03 02:10 PM


All times are GMT -5. The time now is 09:23 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.