11-15-2007, 12:42 AM
|
#1 (permalink)
|
|
The Contributor
Join Date: Oct 2007
Location: Argentina
Posts: 72
Thanks: 18
|
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"> </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 →"></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
Last edited by Matt83 : 12-05-2007 at 08:07 PM.
Reason: CSS support in emails / Quotes in Html / Added Attachment / Better Styling
|
|
|
|