Web Hosting Talk







View Full Version : PHP Contact Form


Neoboffin
01-19-2006, 07:08 PM
Simple form. Works with register_globals on.

This little script works within your page so you don't need 2 pages to process data, and it won't ruin your layout. Simple, clean and you can add departments by changing the self explanatory array.

I didn't find one like this in this section, so I've posted it just to help others who may need one as I think every host needs a contact page.

All you need to do, is create a page, put the below code where you want the contact form to show, edit $YourEmail and $YourDepartments variables, save the page with a .php extension and your ready to go!

<?php
// Configuration Start \\
$YourEmail = "youremail@yourdomain.com";
$YourDepartments = array("Abuse","Domains","General","Other");
// Configuration End \\

if($Submit)
{
if(empty($email) || empty($name) || empty($message))
{
echo "You forgot to enter some required fields. Please go back and try again.";
}
elseif(!eregi('^([._a-z0-9-]+[._a-z0-9-]*)@(([a-z0-9-]+\.)*([a-z0-9-]+)(\.[a-z]{2,3})?)$', $email))
{
echo "The e-mail is not valid. Please go back and try again.";
}
else
{
$Message = "$name has contacted the $Department department. The message is below.\n\n***************\n$message\n***************\n\nIP of sender: $_SERVER[REMOTE_ADDR]\nE-mail of sender: $email";
$headers = 'From: ' . $email . "\r\n" .
'Reply-To: ' . $email . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($YourEmail, $Department, stripslashes($Message), $headers);
echo "The form has been sent and you shall receive a reply shortly.";
}
}
else
{
echo '<form name="cf" method="post" action="">
<table border="0" align="center" cellpadding="5" cellspacing="0">
<tr>
<td align="right"><strong>Your Name:</strong></td>
<td><input name="name" type="text" id="name"></td>
</tr>
<tr>
<td align="right"><strong>Your E-Mail:</strong></td>
<td><input name="email" type="text" id="email"></td>
</tr>
<tr>
<td align="right"><strong>Department:</strong></td>
<td><select name="Department">';
foreach($YourDepartments as $Department)
{
echo '<option>' . $Department . '</option>';
}
echo '</select></td>
</tr>
<tr>
<td align="right" valign="top"><strong>Message:</strong></td>
<td><textarea name="message" cols="30" rows="6" id="message"></textarea></td>
</tr>
</table>
<p align="center">
<input type="submit" name="Submit" value="Send">
<input type="reset" name="Reset" value="Reset">
</p>
</form>';
}
?>

boxxy
04-21-2006, 12:08 PM
Is there a demo anywhere?

Top Nurse
07-27-2006, 05:05 PM
First of all thanks to Neoboffin for posting the script. :lovewht:

I have been trying to get this to work on our website put it seems to not work properly and I don't know enough programming to see what is up. I think the problem is with this line = X-Mailer: PHP/' . phpversion();

BTW, is there any way to use this script to utilize SMTP?

Here is the code we used:


<?php
// Configuration Start \\
$YourEmail = "contactus@baycitiesna.com";
$YourDepartments = array("Activities","Area Chair","Area Secretary","Convention","External Vice-Chair","Hospitals & Institutions","Internal Vice-Chair","Literature Distribution","Men's Luncheon","Newsletter","Phonelines","Public Information","Punk Show","Rockfest","Treasurer","Website","Women's Luncheon","Women's Retreat","Other");
// Configuration End \\

if($Submit)
{
if(empty($email) || empty($name) || empty($message))
{
echo "You forgot to enter some required fields. Please go back and try again.";
}
elseif(!eregi('^([._a-z0-9-]+[._a-z0-9-]*)@(([a-z0-9-]+\.)*([a-z0-9-]+)(\.[a-z]{2,3})?)$', $email))
{
echo "The e-mail is not valid. Please go back and try again.";
}
else
{
$Message = "$name has contacted the $Department department. The message is below.\n\n***************\n$message\n***************\n\nIP of sender: $_SERVER[REMOTE_ADDR]\nE-mail of sender: $email";
$headers = 'From: ' . $email . "\r\n" .
'Reply-To: ' . $email . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($YourEmail, $Department, stripslashes($Message), $headers);
echo "The form has been sent and you shall receive a reply shortly.";
}
}
else
{
echo '<form name="cf" method="post" action="">
<table border="0" align="center" cellpadding="5" cellspacing="0">
<tr>
<td align="right"><strong>Your Name:</strong></td>
<td><input name="name" type="text" id="name"></td>
</tr>
<tr>
<td align="right"><strong>Your E-Mail:</strong></td>
<td><input name="email" type="text" id="email"></td>
</tr>
<tr>
<td align="right"><strong>Department:</strong></td>
<td><select name="Department">';
foreach($YourDepartments as $Department)
{
echo '<option>' . $Department . '</option>';
}
echo '</select></td>
</tr>
<tr>
<td align="right" valign="top"><strong>Message:</strong></td>
<td><textarea name="message" cols="60" rows="10" id="message"></textarea></td>
</tr>
</table>
<p align="center">
<input type="submit" name="Submit" value="Send">
<input type="reset" name="Reset" value="Reset">
</p>
</form>';
}
?>

Top Nurse
07-27-2006, 11:16 PM
It isn't going to work because the function I need will not be provided by my hosting company: register_globals :bawling:

arkin
07-28-2006, 09:12 AM
register_globals is defined in all versions of PHP, whether it is allowed or not.
If however it is not set to on, you can simply define the variables.

Attach to top:
$email=$_POST['email'];
$name=$_POST['name'];
$message=$_POST['message'];

Another factor i'd be scared of is injection, be careful.

horizon
07-28-2006, 11:23 PM
There's also this one for more efficient verification method:


$email = (isset($_POST['email'])) ? (stripslashes(trim($_POST['email']))) : "";
$name = (isset($_POST['name'])) ? (stripslashes(trim($_POST['name']))) : "";
$message = (isset($_POST['message'])) ? strip_tags(trim($_POST['message'])) : "";

brendandonhu
07-29-2006, 12:28 AM
You might want to do some validation on $Department as well, making it a <select> isn't stopping anyone from putting in any value they like.

Christian
07-29-2006, 01:21 AM
register_globals is defined in all versions of PHP, whether it is allowed or not.
That is until PHP 6 comes out... They finally get rid of them! :banana:
http://www.sitepoint.com/blogs/2006/03/10/what-wont-be-in-php-6/

Christian
07-29-2006, 01:23 AM
You might want to do some validation on $Department as well, making it a <select> isn't stopping anyone from putting in any value they like.
Exactly, never trust the user. Make use you are getting the expected input. :)

horizon
07-29-2006, 09:10 AM
Replace:


$email = (isset($_POST['email'])) ? (stripslashes(trim($_POST['email']))) : "";
$name = (isset($_POST['name'])) ? (stripslashes(trim($_POST['name']))) : "";
$message = (isset($_POST['message'])) ? strip_tags(trim($_POST['message'])) : "";


with:


$email = (isset($_POST['email'])) ? (stripslashes(trim($_POST['email']))) : "";
$name = (isset($_POST['name'])) ? (stripslashes(trim($_POST['name']))) : "";
$message = (isset($_POST['message'])) ? strip_tags(trim($_POST['message'])) : "";
$department = (isset($_POST['department'])) ? (stripslashes(trim($_POST['department']))) : NULL;


Then,

replace:


if(empty($email) || empty($name) || empty($message))


with:


if(empty($email) || empty($name) || empty($message) || $department == NULL)



Should be all set. ;)

brendandonhu
07-30-2006, 01:01 AM
They can still set the subject line to whatever they want...

horizon
07-30-2006, 08:56 AM
hey can still set the subject line to whatever they want...



Subject line ? Sorry there but I don't see any variables named anything like subject ...

brendandonhu
07-30-2006, 03:30 PM
Whatever the variable is called, the 2nd parameter to mail() is the subject. They can POST any value for $Department and it will go directly into mail().

BurakUeda
08-17-2006, 11:51 AM
Email header injection anyone?

brendandonhu
08-17-2006, 02:30 PM
Email header injection anyone?
See the post above yours...

horizon
08-17-2006, 02:34 PM
Ok, so assuming you do have a subject line, here's an example you could use:


$subject = (isset($_POST['subject'])) ? (stripslashes(trim($_POST['subject']))) : "";

if (!empty($subject) && strpos("your_restriction_word_here", $subject)) {
die ('Word not allowed.');
}

Neoboffin
08-23-2006, 06:59 AM
Thank you to horizon and brendandonhu. I have implimented your code samples and altered the script as well. The new one is here:

<?php
// Configuration Start \\
$YourEmail = "youremail@yourdomain.com";
$YourDepartments = array("Abuse","Domains","General","Other");
// Configuration End \\

if($_POST['Submit'] || $Submit)
{
// Check all variables sent through POST. Strip 'em!
$fEmail = (isset($_POST['email'])) ? (stripslashes(trim($_POST['email']))) : "";
$fName = (isset($_POST['name'])) ? (stripslashes(trim($_POST['name']))) : "";
$fMessage = (isset($_POST['message'])) ? strip_tags(trim($_POST['message'])) : "";
$fDepartment = (isset($_POST['department'])) ? (stripslashes(trim($_POST['department']))) : NULL;

// Quick variable checks
if(empty($fEmail) || empty($fName) || empty($fMessage) || $fDepartment == NULL)
echo "You forgot to enter some required fields. Please <a href=\"javascript: history.go(-1)\">go back</a> and try again.";
elseif(!eregi('^([._a-z0-9-]+[._a-z0-9-]*)@(([a-z0-9-]+\.)*([a-z0-9-]+)(\.[a-z]{2,3})?)$', $fEmail))
echo "The e-mail is not valid. Please <a href=\"javascript: history.go(-1)\">go back</a> and try again.";
else
{
// Set the message format
$fMessage = "$fName has contacted the $fDepartment department. The message is below.\n\n***************\n$fMessage\n***************\n\nIP of sender: $_SERVER[REMOTE_ADDR]\nE-mail of sender: $fEmail";
// Prepare e-mail headers for sending
$fHeaders = 'From: ' . $fName . ' <' . $fEmail . '>' . "\r\n" .
'Reply-To: ' . $fEmail . "\r\n" .
'X-Mailer: PHP/' . phpversion();
// Attmpt to e-mail
$tryEmail = mail($YourEmail, $fDepartment, $fMessage, $fHeaders);
if($tryEmail)
echo "The form has been sent and you shall receive a reply shortly.";
else
echo "We are sorry. The e-mail could not be sent. Please try again later.";
}
}
else
{
echo '<form name="cf" method="post" action="">
<table border="0" align="center" cellpadding="5" cellspacing="0">
<tr>
<td align="right"><strong>Your Name:</strong></td>
<td><input name="name" type="text" id="name"></td>
</tr>
<tr>
<td align="right"><strong>Your E-Mail:</strong></td>
<td><input name="email" type="text" id="email"></td>
</tr>
<tr>
<td align="right"><strong>Department:</strong></td>
<td><select name="department">';
foreach($YourDepartments as $Department)
{
echo '<option>' . $Department . '</option>';
}
echo '</select></td>
</tr>
<tr>
<td align="right" valign="top"><strong>Message:</strong></td>
<td><textarea name="message" cols="30" rows="6" id="message"></textarea></td>
</tr>
</table>
<p align="center">
<input type="submit" name="Submit" value="Send">
<input type="reset" name="Reset" value="Reset">
</p>
</form>';
}
?>

horizon
08-23-2006, 08:11 AM
Looking good.

However, this line:


if($_POST['Submit'] || $Submit)
{


should be changed to:


if(isset($_GET['Submit']) || isset($_POST['Submit'])) {
$submit = (isset($_POST['submit'])) ? trim($_POST['submit']) : "";
} else {
$submit = "";
}


Then, replace:


if(empty($fEmail) || empty($fName) || empty($fMessage) || $fDepartment == NULL)


with:


if(empty($fEmail) || empty($fName) || empty($fMessage) || $fDepartment == NULL || empty($submit))


And this:


!eregi('^([._a-z0-9-]+[._a-z0-9-]*)@(([a-z0-9-]+\.)*([a-z0-9-]+)(\.[a-z]{2,3})?)$', $fEmail)


I assume this is for email checking ? If so, you could also call it from a general function but it's alright. ;)

brendandonhu
08-23-2006, 08:38 AM
Why this?
if(isset($_GET['Submit']) || isset($_POST['Submit'])) {

Its the same as
if(isset($_REQUEST['Submit'])) {

horizon
08-23-2006, 08:59 AM
Why this?


Because it's what I use.

mythnick
01-03-2007, 06:20 PM
<?
if ($send=="yes") {
$to = "email@something.com";
$subject = "$subjectVar";
$body = "$adrVar \r\n $cnpVar \r\n $emailVar";
$tfrom = "From: $nameVar <$emailVar>";
mail($to,$subjectVar,$adrVar,$tfrom);
}
echo "&errormessage=Email Trimis&";
?>

I have all these variables in a flash
how can i sent them to a mail?

subjectVar="Inscriere";
nameVar="";
pnameVar="";
emailVar="";
cnpVar="";
seriaVar="";
nrVar="";
dataVar="";
polVar="";
anVar="";
anstudVar="";
seriastudVar="";
grupastudVar="";
cunivVar="";
adrVar="";

admin-globalvidia
02-13-2007, 10:38 PM
Hi,
Where can I find free scripts?

horizon
02-14-2007, 12:03 AM
www.hotscripts.com

cFlux
03-03-2007, 01:19 PM
Thank you for the great script. I am having only one problem:

What makes the Department go in front of the Subject when viewing all emails? It doesn't seem to appear when viewing the email itself, but rather when viewing the list of emails recieved...

How can this be changed so that the department name does not go in front of the actual subject?

<?php include('inc/header.php'); ?>

<div id="icons"><?php include('inc/flash6.php'); ?></div>

<div class="mainHolder2">

<?php
// Configuration Start \\
$YourEmail = "sergiod06@hotmail.com";
$YourDepartments = array("Yes&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;","No&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
// Configuration End \\

if($_POST['Submit'] || $Submit)
{
// Check all variables sent through POST. Strip 'em!
$fEmail = (isset($_POST['email'])) ? (stripslashes(trim($_POST['email']))) : "";
$fName = (isset($_POST['name'])) ? (stripslashes(trim($_POST['name']))) : "";
$fMessage = (isset($_POST['message'])) ? strip_tags(trim($_POST['message'])) : "";
$fSubject = (isset($_POST['subject'])) ? (stripslashes(trim($_POST['subject']))) : "";
$fDepartment = (isset($_POST['department'])) ? (stripslashes(trim($_POST['department']))) : NULL;


// Quick variable checks
if(empty($fEmail) || empty($fName) || empty($fSubject) || empty($fMessage) || $fDepartment == NULL)
echo "You forgot to enter some required fields. Please <a href=\"javascript: history.go(-1)\">go back</a> and try again.";
elseif(!eregi('^([._a-z0-9-]+[._a-z0-9-]*)@(([a-z0-9-]+\.)*([a-z0-9-]+)(\.[a-z]{2,3})?)$', $fEmail))
echo "The e-mail is not valid. Please <a href=\"javascript: history.go(-1)\">go back</a> and try again.";
else
{
// Set the message format
$fMessage = "\n\nName: $fName\n\nEmail: $fEmail\n\nJoin e-Newsletter: $fDepartment\n\nComments: $fMessage";
// Prepare e-mail headers for sending
$fHeaders = 'From: ' . $fName . ' <' . $fEmail . '>' . "\r\n" .
'Reply-To: ' . $fEmail . "\r\n" .
'Subject: [Web Inquiry] ' . $fSubject . "\r\n" .
'X-Mailer: PHP/' . phpversion();
// Attmpt to e-mail
$tryEmail = mail($YourEmail, $fDepartment, $fMessage, $fHeaders);
if($tryEmail)
echo "The form has been sent and you shall receive a reply shortly.";
else
echo "We are sorry. The e-mail could not be sent. Please try again later.";
}
}
else
{
echo '

<br /><br />
<form name="cf" method="post">
<table border="0" cellpadding="1" cellspacing="0">
<tr>
<td>Name:</td>
</tr>
<tr>
<td><input name="name" size="55" type="text" id="name"><br /><br /></td>
</tr>
</table>
<table border="0" cellpadding="1" cellspacing="0">
<tr>
<td>
<table border="0" cellpadding="1" cellspacing="0">
<tr>
<td>E-Mail Address:</td>
</tr>
<tr>
<td><input name="email" size="31" type="text" id="email"><br /><br /></td>
</tr>
</table>
</td>
<td>
<table border="0" style="margin-left: 20px;" cellpadding="1" cellspacing="0">
<tr>
<td>Join e-Newsletter:</td>
</tr>
<tr>
<td><select name="department">';
foreach($YourDepartments as $Department)
{
echo '<option>' . $Department . '</option>';
}
echo '</select><br /><br /></td>
</tr>
</table>
</td>
</tr>
</table>
<table border="0" cellpadding="1" cellspacing="0">
<tr>
<td>Subject:</td>
</tr>
<tr>
<td><input name="subject" size="55"type="text" id="subject"><br /><br /></td>
</tr>
<tr>
<td>Comments:</td>
</tr>
<tr>
<td><textarea name="message" cols="52" rows="6" id="message"></textarea></td>
</tr>
</table>
<div style="margin-left: 110px; margin-top: 10px;">
<a onclick="document.cf.reset();return false;" href="#"><img alt="Clear" src="images/reset.jpg" border="0" /></a>
<img src="images/spacer.gif" width="5" alt="" />
<input type="image" value="Submit" style="border: 0; padding: 0; width: 58ox; height: 25px;" name="Submit" src="images/send.jpg" />
</div>
</form>';
}
?>

</div>

<?php include('inc/footer.php'); ?>


I only want to include [Web Inquiry] before the subject in all emails sent.

orongo
03-27-2007, 10:02 AM
Is these contact script form is spam/hack proof?

horizon
03-27-2007, 09:09 PM
Is these contact script form is spam/hack proof?

No, it only validates the fields to see if they match accordingly as expected.

Energizer Bunny
03-28-2007, 01:08 AM
Is these contact script form is spam/hack proof?

You can use captcha protection, u can use any existing captcha codes and use on ur form page. Like i did on http://www.h-log.com/contact.php . Its both audio and visual captcha. :D

Mr-Max
04-27-2007, 10:29 AM
Thanks Alot >>>>>