Simple PHP feedback mailer form

Posted under » PHP on 13 March 2009

One of the first use of PHP that I've learnt is to create a feedback form. Initially, it was done using PERL but when PERL is gradually phased out, we use PHP which is much easier to use.

First you need to have a form where the user is given the ability to fill up the info. to be given to us.
Then we create a PHP script to process the form and then mail the data to us.

Sometimes instead of mailing the data over, we keep it in some storage like database for example. We can also do both mailing and storing.

You can pass the form variables to another page via either the post or get action.

Lets get started. The form itself can be HTML or it could be PHP so that you can get the user's IP number for example. Example is below.

<¿php $ipi = getenv("REMOTE_ADDR"); $httprefi = getenv ("HTTP_REFERER"); $httpagenti = getenv ("HTTP_USER_AGENT"); ?>

Your Name:

Your Email:

Attention:

Subject:

Mail Message:

Notice that the action goes to script.php. Therefore create a file "script.php" with the contents below.




Send mail Script

<¿php
$ip = $_POST['ip'];
$httpref = $_POST['httpref'];
$httpagent = $_POST['httpagent'];
$visitor = $_POST['visitor'];
$visitormail = $_POST['visitormail'];
$notes = $_POST['notes'];
$subject = $_POST['subject'];
$attn = $_POST['attn'];
$to = youremail@gmail.com;
// spam killer
if (eregi('http:', $notes)) {
die ("Do NOT try that! ! ");
}
// email validator
if(!$visitormail == "" && (!strstr($visitormail,"@") || !strstr($visitormail,".")))
{
echo "

Use Back - Enter valid e-mail

\n"; $badinput = "

Feedback was NOT submitted

\n"; echo $badinput; die ("Go back! ! "); } // null contents checker if(empty($visitor) || empty($visitormail) || empty($notes )) { echo "

Use Back - fill in all fields

\n"; die ("Use back! ! "); } $todayis = date("l, F j, Y, g:i a") ; // you could think of some usage in attn like adding to subject line $subject = $attn.":".$subject; $notes = stripcslashes($notes); $message = "$notes"; $from = "From: $visitormail\r\n"; mail($to, $subject, $message, $from); ?>

Date: <¿php echo $todayis ?>
Thank You : <¿php echo $visitor ?> ( <¿php echo $visitormail ?> )
Attention: <¿php echo $attn ?>
Message:
<¿php $notesout = str_replace("\r", "
", $notes); echo $notesout; ?>
<¿php echo $ip ?>

Next Page

If your server can send out email, the feedback will reach youremail@gmail.com shortly after.

Due to spam prevention, php mail() method can be unreliable if your server is blacklisted. A better or effective but slightly complex althernative is the phpmailer using SMTP.

If the code formatting is screwy, please download the source code here.


web security linux ubuntu python django git Raspberry apache mysql php drupal cake javascript css AWS data