Unstoppable Domains

PHP FeedBack Mail Form

Spaceship Spaceship
Watch

YaSHaRz

Established Member
Impact
1
:hi:
This PHP Mail tutorial will tell you how to use the mail() function to send emails to people and make feedback form for your own website. PHP has built in capabilities for your email needs. Let's get started by making a form:


Code:
  <form action="feed.php" method="post">
  <div align="center">

Now add here your PHP block to write:

Code:
<?php
 echo $error; 
?>

Which will be our variable if there is any errors something will be displayed!


Let's continue:

Code:
  </div>
  Name: <input type="text" name="name" /><br />
  Email: <input type="text" name="email" /><br />
  Comments: <textarea name="comments"></textarea><br />
  <input type="submit" name="submit" value="Send Feedback" />
  </form>

Well that is our feedback.php file. Save this file and upload it.
Now we will work on our feed.php file which has the actual script in it.

Let's begin and i'll explain along the way:

Code:
<?php

  //Start your PHP tags
  foreach($_POST as $key => $val){
    $$key = trim($val);
 }
 function em($ema){
 if(!eregi('[a-z0-9]+[_a-z0-9-]*(\.[_a-z0-9-]+)*@[a-z?G0-9]+(-[a-z?G0-9]+)*(\.[a-z?G0-9-]+)*(\.[a-z]{2,4})$', 
$ema, $ema_er)){
       return 0;
    } else {
        return 1;
    }
 }
 if(em($email) != 1){
   $error = "<span class='error'>Error<br />The 
email you entered is an invalid email address, Please input the correct 
email!</span>";
   include("feedback.php");

   exit();
 }
 if((!$name) || (!$email) || (!$comments)) {
   $error = "<span class='error'>Error<br />Please fill in all the fields.</span>";
   include("feedback.php");
   exit();
 } else {
 $ip =  $_SERVER['REMOTE_ADDR'];
 $ref = $_SERVER['HTTP_REFERER'];
 $to = "[email protected]";
 $date = date("m/d/Y");
 $time = date("h:i:s A");
 $subject = "Feedback from YourWebsite!";
 $msg = "Feedback from $username.
 @ $email!!!
 His comments are:
 $comments \n\n

 Came from: $ref
 At: $ip|  $date  |  $time";
 $mailheaders = "From: FEEDBACK.PHP<[email protected]> \n\r";
 $mailheaders .= "X-Mailer: DP-Mail X-2 \n\r\n\r";
 mail($to, $subject, $msg, $mailheaders);
 $error = "<span class='true'>We have successfully sent your 
feedback form! We will reply to your input as soon as possible. Thank 
you!</span>";
 include("feedback.php");
 exit();
 } // Close PHP tags.

?>

Ok, first we take all the POST variables from the $_POST superglobal array, which includes all the form variables we just received via POST. Then we take all the $key's and their values and make simple variables out of them such as: $comments, $name, $email instead of $_POST['comments']. So that's a neat little loop there to do that. We also trim any extra spaces.

We then have a function called "em" which checks for incorrect email formats. All emails should be in the form of [email protected] or .net or whatever. It returns 1 or 0 in whichever case. We later check for it, and if it returned 0, then we give an error and stop the script by exitting.

Then we check if any of our variables are empty. If they are we throw an error and include the form. If everything is fine, we check the server for HTTP_REFERER and REMOTE_ADDR, which is the HTTP referrer to know where they came from, and the REMOTE_ADDR is the IP address of the user, incase of spammers. The variable $to is YOUR email address, the recipient of this feedback. We also set the Date and Time in human readable format. And then we set the subject and $msg variables to send our email, in a nice format. Containing all the information. This time we set $error as a success message, and we show it to the user.

This is a great feedback form for all your clients and your website. Emailing is great thing to control through PHP. Also do not forget the \r\n's at the end of the $mailheaders function. You should change the info there too. This is to prevent hackers and injections into your header that robots use your form to spam people with your email address.

:)
 
1
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
AfternicAfternic
Maybe this should go in the code section, im not sure, thanks though, rep added.
 
0
•••
Dynadot โ€” .com Registration $8.99Dynadot โ€” .com Registration $8.99
Unstoppable Domains
Domain Recover
DomainEasy โ€” Zero Commission
  • The sidebar remains visible by scrolling at a speed relative to the pageโ€™s height.
Back