Help with PHP contact form

Discussion in 'PHP' started by lodgey, Apr 10, 2008.

  1. #1
    Hi Forum users,

    I have recently tried to put together a php contact form but when it sends it doesn't have the visitors email address. I receive the email with the subject and message addressed to myself from myself. I would also like to add the visitors name on the contact from, but have also failed at attempting that. Adding input type="text" name="Visitor" to the form and
    
     $visitor = Trim(stripslashes($_POST['visitor'])); 
    Code (markup):
    to the php form data and adding the $vistor in the create and send didn't appear to work.
    Could someone please be of assistance?


    
    <?php
    require_once "Mail.php";
    
    //Store form data in local variables
    $EmailFrom = Trim(stripslashes($_POST['EmailFrom'])); 
    $EmailTo = "myemail@gmail.com";
    $Subject = Trim(stripslashes($_POST['Subject'])); 
    $Message = Trim(stripslashes($_POST['Message']));
    
    
    // Require fields
    $validationOK=true;
    if (Trim($EmailFrom)=="") $validationOK=false;
    if (!$validationOK) {
      print "<meta http-equiv=\"refresh\" content=\"0;URL=error.html\">";
      exit;
    }
    
    $validationOK=true;
    if (Trim($Subject)=="") $validationOK=false;
    if (!$validationOK) {
      print "<meta http-equiv=\"refresh\" content=\"0;URL=error.html\">";
      exit;
    }
    
    $validationOK=true;
    if (Trim($Message)=="") $validationOK=false;
    if (!$validationOK) {
      print "<meta http-equiv=\"refresh\" content=\"0;URL=error.html\">";
    }
    
    
    //smtp variables
    $host = "smtp.gmail.com";
    $port = "587";
    $username = "myemail@gmail.com";
    $password = "xxxxx";
    
    
    //reCaptcha validation
    require_once('recaptchalib.php');
    $privatekey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
    $resp = recaptcha_check_answer ($privatekey,
                                    $_SERVER["REMOTE_ADDR"],
                                    $_POST["recaptcha_challenge_field"],
                                    $_POST["recaptcha_response_field"]);
    
    if (!$resp->is_valid) {
      print "<meta http-equiv=\"refresh\" content=\"0;URL=captcha.html\">";
      exit;
    }
    
    
    //create and send
    $headers = array ('From' => $EmailFrom,
    'To' => $EmailTo,
    'Subject' => "Web contact - $Subject");
    $smtp = Mail::factory('smtp',
    array ('host' => $host,
       'auth' => true,
       'port' => $port,
       'username' => $username,
       'password' => $password));
    
    $mail = $smtp->send($EmailTo, $headers, $Message);
    
    
    //show form submission result
    if (PEAR::isError($mail)) {
    print "<meta http-equiv=\"refresh\" content=\"0;URL=error.html\">";
    exit;
    } else {
    print "<meta http-equiv=\"refresh\" content=\"0;URL=ok.html\">";
    }
    ?> <?php
    require_once "Mail.php";
    
    //Store form data in local variables
    $EmailFrom = Trim(stripslashes($_POST['EmailFrom'])); 
    $EmailTo = "myemail@gmail.com";
    $Subject = Trim(stripslashes($_POST['Subject'])); 
    $Message = Trim(stripslashes($_POST['Message']));
    
    
    // Require fields
    $validationOK=true;
    if (Trim($EmailFrom)=="") $validationOK=false;
    if (!$validationOK) {
      print "<meta http-equiv=\"refresh\" content=\"0;URL=error.html\">";
      exit;
    }
    
    $validationOK=true;
    if (Trim($Subject)=="") $validationOK=false;
    if (!$validationOK) {
      print "<meta http-equiv=\"refresh\" content=\"0;URL=error.html\">";
      exit;
    }
    
    $validationOK=true;
    if (Trim($Message)=="") $validationOK=false;
    if (!$validationOK) {
      print "<meta http-equiv=\"refresh\" content=\"0;URL=error.html\">";
    }
    
    
    //smtp variables
    $host = "smtp.gmail.com";
    $port = "587";
    $username = "myemail@gmail.com";
    $password = "xxxxx";
    
    
    //reCaptcha validation
    require_once('recaptchalib.php');
    $privatekey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
    $resp = recaptcha_check_answer ($privatekey,
                                    $_SERVER["REMOTE_ADDR"],
                                    $_POST["recaptcha_challenge_field"],
                                    $_POST["recaptcha_response_field"]);
    
    if (!$resp->is_valid) {
      print "<meta http-equiv=\"refresh\" content=\"0;URL=captcha.html\">";
      exit;
    }
    
    
    //create and send
    $headers = array ('From' => $EmailFrom,
    'To' => $EmailTo,
    'Subject' => "Web contact - $Subject");
    $smtp = Mail::factory('smtp',
    array ('host' => $host,
       'auth' => true,
       'port' => $port,
       'username' => $username,
       'password' => $password));
    
    $mail = $smtp->send($EmailTo, $headers, $Message);
    
    
    //show form submission result
    if (PEAR::isError($mail)) {
    print "<meta http-equiv=\"refresh\" content=\"0;URL=error.html\">";
    exit;
    } else {
    print "<meta http-equiv=\"refresh\" content=\"0;URL=ok.html\">";
    }
    ?> 
    
    Code (markup):
    Form


    
     <div id="eform">
    <center><form method="POST" action="contact.php">
    Fields marked (*) are required.
    
    <p>Email Address: *<br>
    <input type="text" name="EmailFrom"></p>
    <p>Subject: <br>
    <input type="text" name="Subject"></p>
    <p>Message:<br>
    <textarea name="Message" rows="10" cols="30"></textarea>
    <p><script type="text/javascript"
       src="http://api.recaptcha.net/challenge?k=6Lc4eAEAAAAAALC7mtowaUZ2z0sVF1pX_txsraLK">
    </script></p>
    <noscript>
       <iframe src="http://api.recaptcha.net/noscript?k=6Lc4eAEAAAAAALC7mtowaUZ2z0sVF1pX_txsraLK"
           height="300" width="500" frameborder="0"></iframe><br>
       <textarea name="recaptcha_challenge_field" rows="3" cols="40">
       </textarea>
       <input type="hidden" name="recaptcha_response_field" 
           value="manual_challenge">
    </noscript>
    <p><input type="submit" name="submit" value="Submit"></p>
    </form></center>
       </div> <div id="eform">
    <center><form method="POST" action="contact.php">
    Fields marked (*) are required.
    
    <p>Email Address: *<br>
    <input type="text" name="EmailFrom"></p>
    <p>Subject: <br>
    <input type="text" name="Subject"></p>
    <p>Message:<br>
    <textarea name="Message" rows="10" cols="30"></textarea>
    <p><script type="text/javascript"
       src="http://api.recaptcha.net/challenge?k=6Lc4eAEAAAAAALC7mtowaUZ2z0sVF1pX_txsraLK">
    </script></p>
    <noscript>
       <iframe src="http://api.recaptcha.net/noscript?k=6Lc4eAEAAAAAALC7mtowaUZ2z0sVF1pX_txsraLK"
           height="300" width="500" frameborder="0"></iframe><br>
       <textarea name="recaptcha_challenge_field" rows="3" cols="40">
       </textarea>
       <input type="hidden" name="recaptcha_response_field" 
           value="manual_challenge">
    </noscript>
    <p><input type="submit" name="submit" value="Submit"></p>
    </form></center>
       </div>
    
    Code (markup):

     
    lodgey, Apr 10, 2008 IP
  2. lodgey

    lodgey Guest

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Sorry about the double code for both form and php as i can't seem to edit this post.
     
    lodgey, Apr 10, 2008 IP
  3. lodgey

    lodgey Guest

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Sorry about the double code for both form and php as i can't seem to edit this post.
    Has any one a good tutorial for mail.php web forms as i followed this one and added recaptcha.


    "www.websitesnetwork.com/forums/index.php?showtopic=623&pid=1711&mode=threaded&start=#entry1711[/url]"
     
    lodgey, Apr 10, 2008 IP
  4. c4st

    c4st Peon

    Messages:
    41
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    http://green-beast.com/blog/?p=128
     
    c4st, Apr 10, 2008 IP