View Single Post
Old 11-15-2007, 12:42 AM   #1 (permalink)
Matt83
The Contributor
Upcoming Programmer 
 
Matt83's Avatar
 
Join Date: Oct 2007
Location: Argentina
Posts: 72
Thanks: 18
Matt83 is on a distinguished road
Default Contact Form Script (update 3)

Hi guys, this is a simple script to manage contact forms, just thougth of giving it away in case someone needs one. Any critics or comments are welcome.

sendmail.php: (update 3) // Thanks Wildhoney

PHP Code:
<?php
# ------------------------------------------------------------
// General Configuration
# ------------------------------------------------------------

// Name of the website. 
$szFrom "My Website";

// Your email, (the recipient email address).
$szRecipient "youremail@somedomain.com"

// Sender's email address.
$szFromEmail "noreply@somedomain.com"

/* Title comes from the form. this gives the possibility  
 to set up more than one form and still use this same script.*/
$szTitle $_POST['title']; 

// This sets up the subject.
$szSubject $szFrom.": ".$szTitle

/* All input fields coming from the form should go here. 
The first value is the name attribute we used on the form 
and the second value on the right the name we want to display 
on the final email.*/

$aPosted = array( 
    
"name" => "Name",
    
"telephone" => "Telephone Number",
    
"email" => "Email Address",
    
"comments" => "Comments",
    
// add here your fields
);

# ------------------------------------------------------------
// Email Content
# ------------------------------------------------------------

$szEmailContent ='<table border="0" width="100%" border="0">';
$szEmailContent.='<tr><td colspan="2" align="center" style="color:#fff; background-color:#000"><b>".$szTitle."</b></td></tr>';
$szEmailContent.= '</tr><tr><td colspan="2">&nbsp;</td>';

$last "";

foreach(
$aPosted as $value => $szReal)
{
    if(isset(
$_POST['value']))
    {
        
$szEmailContent.= '<tr><td width="50%" align="right"><b>';
        
        if(
$last != $szReal)
        {
            
$szEmailContent.= $szReal.":";
            
$last $szReal;
        }
        
        
$szEmailContent.= "</b></td><td>".$_POST['value']."</td></tr>\n";
    }
}

$szEmailContent.= "</table>";

# ------------------------------------------------------------
// Declare Email headers
# ------------------------------------------------------------

$szHeaders  "MIME-Version: 1.0\n";
$szHeaders .= "Content-type: text/html; charset=iso-8859-1\n";
$szHeaders .= "From: \"$szFrom\" <$szFrom>\n";    
$szHeaders .= "Return-Path: <$szFrom>\n";    
$szHeaders .= "X-Sender: <$szFrom>\n";
$szHeaders .= "X-Mailer: PHP\n"
$szHeaders .= "X-Priority: 3\n"

# ------------------------------------------------------------
// Output 
# ------------------------------------------------------------

if(mail($szRecipient$szSubject$szEmailContent$szHeaders))
    
// go to previous page ($lasturl) with action=1 (could be a succes message).
    
header'Location: '.html_entity_decode($_POST['lasturl']).'?action=1');  
else
    
// go to previous page ($lasturl) with action=2 (could be a failure message).
    
header'Location: '.html_entity_decode($_POST['lasturl']).'?action=2');      
        
?>
contact_form.php (example):


PHP Code:

<form action="sendmail.php" method="post">
<input type="hidden" name="title" value="My Contact Form Title"/>
<input type="hidden" name="lasturl" value="<?php echo $_SERVER['REQUEST_URI']; // this is important ?>"/> 

    <p><label>Name:</label><br/>
    <input type="text" name="name" value=""/>    
    </p>
    
    <p><label>Telephone Number:</label><br/>
    <input type="text" name="telephone" value=""/>    
    </p>
    
    <p><label>Email:</label><br/>
    <input type="text" name="email" value=""/>    
    </p>

    <p><label>Comments:</label><br/>
    <textarea name="comments" rows="8" cols="40"></textarea>
    </p>

    <p><input type="submit" value="Continue &rarr;"></p>
</form>
I think that the overall concept is pretty simple but let me know if you have any questions.
ps: sorry for bad english.

Matt
Attached Files
File Type: php sendmail.php (3.0 KB, 238 views)
File Type: php contact_form.php (690 Bytes, 212 views)
__________________
http://www.mattvarone.com

Last edited by Matt83 : 12-05-2007 at 08:07 PM. Reason: CSS support in emails / Quotes in Html / Added Attachment / Better Styling
Matt83 is offline  
Reply With Quote
The Following User Says Thank You to Matt83 For This Useful Post:
Rendair (12-10-2007)