11 Replies Latest reply on Mar 21, 2012 10:19 AM by James 1001

    Contact Form - Who from?

    James 1001

      I've designed a contact form and the information is sent to me via a php script. I ask for the senders email address and this is returned. However the from address in my email program is blank. It would be useful if this could advise the senders address in case they inputed it wrong on the form. I could get them to input it twice as a check but I'd prefer not to do that.

       

      I guess it's something to do with this code in the php file which is:

       

      EOD;

       

       

                $headers = "From: $email\r\n";

                $headers .= "Content-type: text/html\r\n";

                $success = mail($webMaster, $emailSubject, $body, $headers);

       

      /* Results Rendered as HTML */

       

       

                $theResults = <<<EOD

       

      Can anyone help?

       

      Thanks.

        • 1. Re: Contact Form - Who from?
          osgood_ Level 8

          What does the rest of the php code look like as there would be a php variable which gets the senders email address from the form field.

           

          Make sure your form field names are that same as the variables in the php script.

          • 2. Re: Contact Form - Who from?
            James 1001 Level 1

            I get the email address OK in the body of the email which is sent to me so my variables are OK, but the email which lands in my inbox has no sender information. I want this as a check that I have the correct email address of the sender. Hope this makes sense!

            • 3. Re: Contact Form - Who from?
              osgood_ Level 8

              Add the below to the php where $email is the variable that you have used to retrieve the email information from the form field.

               

              $headers .= "From:".$email;

               

              For example if your email form field name is 'email':

               

              $email = $_GET['email'];

              • 4. Re: Contact Form - Who from?
                James 1001 Level 1

                Thanks for your help. I'm struggling though to get what you suggest working. Here's my full code - if you wouldn't mind advising how to change it.

                 

                <?php

                 

                 

                while (list($key, $val) = @each($_GET)) $GLOBALS[$key] = $val;

                while (list($key, $val) = @each($_POST)) $GLOBALS[$key] = $val;

                while (list($key, $val) = @each($_COOKIE)) $GLOBALS[$key] = $val;

                while (list($key, $val) = @each($_FILES)) $GLOBALS[$key] = $val;

                while (list($key, $val) = @each($_SESSION)) $GLOBALS[$key] = $val;

                 

                 

                /* Subject and Email Variables */

                 

                 

                          $emailSubject = 'Contact Form';

                          $webMaster = 'james@jamesrussellphotography.co.uk';

                 

                /* Gathering Data Variables */

                 

                 

                          $nameField = $_POST['sendersName'];

                          $emailField = $_POST['Email'];

                          $messageField = $_POST['Message'];

                          $phoneField = $_POST['phone'];

                 

                          $body = <<<EOD

                <br><hr><br>

                Name: $nameField <br>

                Email: $emailField <br>

                Message: $messageField <br>

                Phone: $phoneField <br>

                EOD;

                 

                 

                 

                          $headers .= "From:".$Email;

                          $headers .= "Content-type: text/html\r\n";

                          $success = mail($webMaster, $emailSubject, $body, $headers);

                 

                /* Results Rendered as HTML */

                 

                 

                          $theResults = <<<EOD

                <html>

                <head>

                <title></title>

                <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"><br />

                <meta HTTP-EQUIV="REFRESH" content="0; url=http://www.jrphoto.co.uk/index.html">

                <style type="text/css">

                <!--

                body {

                          background-color: #fffff;

                          font-family: Arial, Helvetica, sans-serif;

                          font-size: 14px;

                          font-style: normal;

                          line-height: normal;

                          font-weight: normal;

                          color: #000000;

                          text-decoration: none;

                }

                -->

                </style>

                </head>

                <div align="left">

                Redirecting you...</div>

                </body>

                </html>

                EOD;

                echo "$theResults";

                 

                 

                ?>

                • 5. Re: Contact Form - Who from?
                  osgood_ Level 8

                  What happens if you change the php to the below: (please note there is no peirod (.) infornt of the first $headers =

                   

                  $headers = "Content-type: text/html\r\n";

                  $headers .= "From:".$Email;

                  $success = mail($webMaster, $emailSubject, $body, $headers);

                  • 6. Re: Contact Form - Who from?
                    osgood_ Level 8

                    Sorry try as below: (using YOUR $emailField variable instead of $Email)

                     

                    $headers = "Content-type: text/html\r\n";

                    $headers .= "From:".$emailField;

                    $success = mail($webMaster, $emailSubject, $body, $headers);

                    • 7. Re: Contact Form - Who from?
                      James 1001 Level 1

                      Thank you. I see what you've done and it works. It dispays as I would like in my email program. What is happening is that the variable is copied into the header. So unfortunately it doesn't work as a check if the user typed in their address wrongly. Was hoping their real email address, irrespective of what they typed in would come up in the header. I may be asking for something that's not possible.

                      • 8. Re: Contact Form - Who from?
                        osgood_ Level 8

                        James 1001 wrote:

                         

                        Was hoping their real email address, irrespective of what they typed in would come up in the header. I may be asking for something that's not possible.

                        If someone chooses to provide an incorrect email address then theres nothing you can do. You can ask the user to fill out the email formfield twice as a sort of 'validation' check but if they wish to send a false email address they can.

                        • 9. Re: Contact Form - Who from?
                          James 1001 Level 1

                          I wondered whether that was the case. I've never been a fan of filling in my email address twice so would prefer to avoid that route.

                           

                          Anyway thanks for your help - much appreciated.

                          • 10. Re: Contact Form - Who from?
                            bregent Most Valuable Participant

                            There is no way for your form/script to know the users real email address, other than what they enter. If there was, they wouldn't need to enter it

                            • 11. Re: Contact Form - Who from?
                              James 1001 Level 1

                              That would make sense! Thank you.