www.webdeveloper.com
Results 1 to 9 of 9

Thread: Form Data not displaying in Email, using send mail

  1. #1
    Join Date
    Oct 2008
    Posts
    5

    Question Form Data not displaying in Email, using send mail

    Hi Everyone,

    I am new to PHP and my first asignment was to try and send form data from an html page to an email id using a php script. The form reached the email id, however, the contents of the fields do not display. The filed description though shows up. What could be the problem? I have attached the script below. The same script worked on another linux server before. Any help is appreciated here.

    Regards,

    Saktiyantra.

    <?

    ini_set("SMTP","11.11.11.11");

    //if($AttachDoc_size>0)
    // {
    // $AttachDoc_data = fread(fopen($AttachDoc, "rb"), filesize($AttachDoc));
    // $str = chunk_split(base64_encode($AttachDoc_data));
    // $AttachDoc_data = addslashes($AttachDoc_data);
    // }

    $Body="<table width=100% border=1 cellspacing=5 cellpadding=5 bgcolor=#E4D8F5>
    <tr>
    <td width=200 align=right><font face=\"Arial, Helvetica, sans-serif\" size=\"2\">Category</font></td>
    <td bgcolor=#FFFFFF><font face=\"Arial, Helvetica, sans-serif\" size=\"2\">$category</font></font></td>
    </tr>
    <tr>
    <td align=right><font face=\"Arial, Helvetica, sans-serif\" size=\"2\">Name</font></td>
    <td bgcolor=#FFFFFF><font face=\"Arial, Helvetica, sans-serif\" size=\"2\">$name</font> </td>
    </tr>
    <tr>
    <td align=right><font face=\"Arial, Helvetica, sans-serif\" size=\"2\">Company</font></td>
    <td bgcolor=#FFFFFF><font face=\"Arial, Helvetica, sans-serif\" size=\"2\">$company</font></td>
    </tr>
    <tr>
    <td align=right><font face=\"Arial, Helvetica, sans-serif\" size=\"2\">Address</font></td>
    <td bgcolor=#FFFFFF><font face=\"Arial, Helvetica, sans-serif\" size=\"2\">$address</font></td>
    </tr>
    <tr>
    <td align=right><font face=\"Arial, Helvetica, sans-serif\" size=\"2\">City</font></td>
    <td bgcolor=#FFFFFF><font face=\"Arial, Helvetica, sans-serif\" size=\"2\">$city </font></td>
    </tr>
    <tr>
    <td align=right><font face=\"Arial, Helvetica, sans-serif\" size=\"2\">State</font></td>
    <td bgcolor=#FFFFFF><font face=\"Arial, Helvetica, sans-serif\" size=\"2\">$state</font> </td>
    </tr>
    <tr>

    <td align=right><font face=\"Arial, Helvetica, sans-serif\" size=\"2\">Zip</font></td>
    <td bgcolor=#FFFFFF><font face=\"Arial, Helvetica, sans-serif\" size=\"2\">$zip</font> </td>
    </tr>
    <tr>

    <td align=right><font face=\"Arial, Helvetica, sans-serif\" size=\"2\">Country</font></td>
    <td bgcolor=#FFFFFF><font face=\"Arial, Helvetica, sans-serif\" size=\"2\">$country</font></td>
    </tr>

    <tr>
    <td align=right><font face=\"Arial, Helvetica, sans-serif\" size=\"2\">Mobile</font></td>
    <td bgcolor=#FFFFFF><font face=\"Arial, Helvetica, sans-serif\" size=\"2\">$mobile</font> </td>
    </tr>
    <tr>

    <td align=right><font face=\"Arial, Helvetica, sans-serif\" size=\"2\">Telephone
    </font></td>
    <td bgcolor=#FFFFFF><font face=\"Arial, Helvetica, sans-serif\" size=\"2\">$telephone</font>
    </td>
    </tr>
    <tr>

    <td align=right><font face=\"Arial, Helvetica, sans-serif\" size=\"2\">Fax
    </font></td>
    <td bgcolor=#FFFFFF><font face=\"Arial, Helvetica, sans-serif\" size=\"2\">$Fax</font>
    </td>
    </tr>
    <tr>
    <td align=right><font face=\"Arial, Helvetica, sans-serif\" size=\"2\">Email</font></td>
    <td bgcolor=#FFFFFF><font face=\"Arial, Helvetica, sans-serif\" size=\"2\">$email </font></td>
    </tr> <tr>
    <td align=right><font face=\"Arial, Helvetica, sans-serif\" size=\"2\">Your Requirement</font></td>
    <td bgcolor=#FFFFFF><font face=\"Arial, Helvetica, sans-serif\" size=\"2\">$yourrequirement </font></td>
    </tr>
    </table>";

    $to = "test@yahoo.com";
    $too = "test@yahoo.com";
    $subject = "Contact: test";
    $from = $email;
    $headers = "From:$from\n";
    $headers .= "Reply-To: $from\n";
    $headers .= "MIME-Version: 1.0\n";
    $headers .= "Content-Type: multipart/mixed; boundary=\"MIME_BOUNDRY\"\n";
    $headers .= "X-Sender: $from\n";
    $headers .= "X-Mailer: PHP4\n";
    $headers .= "X-Priority: 3\n";
    $headers .= "Return-Path: $from\n";
    $headers .= "This is a multi-part message in MIME format.\n";

    $message = "--MIME_BOUNDRY\n";
    $message .= "Content-Type: text/plain; charset=\"iso-8859-1\"\n";
    $message .= "Content-Transfer-Encoding: quoted-printable\n";
    $message .= "\n";
    $message .= "\n";
    $message .= "--MIME_BOUNDRY\n";

    $message = "--MIME_BOUNDRY\n";
    $message .= "MIME-Version: 1.0\r\n";
    $message .= "Content-Type: text/html; charset=\"iso-8859-1\"\n";
    $message .= "\n";
    $message .= "$Body\n";
    $message .= "\n";

    //if($AttachDoc_size >0){
    // $message .= "--MIME_BOUNDRY\n";
    // $message .= "Content-Type: $AttachDoc_type; name=\"$AttachDoc_name\"\n";
    // $message .= "Content-disposition: attachment\n";
    // $message .= "Content-Transfer-Encoding: base64\n";
    // $message .= "\n";
    // $message .= "$str\n";
    // $message .= "\n";
    //message ends
    //}

    mail($to, $subject, $message, $headers);
    include("AutoResponder.php");

    header("location:../htmls/thank_you.htm");
    ?>

  2. #2
    Join Date
    Nov 2007
    Posts
    140
    It appears that the values of your variables ($name, $company, $address, etc.) are not being set. The script may have worked on another server that had "register globals" enabled. This setting is now disabled by default as it poses certain security risks.

    You can set the values to those posted to the script by using the $_POST array in the following manner.

    PHP Code:
    $name htmlspecialchars$_POST['name'] );
    $company htmlspecialchars$_POST['company'] );
    etc
    This assumes your form contains input tags with names that match the variables you're using. eg.

    <input type="text" name="name" />
    <input type="text" name="company" />
    etc.

    While debugging similar issues it helps to add a few debugging function calls to your code so you can see what's going on without having to check any emails or even wait for the entire script to execute.

    var_dump($body);
    die;

    placing this before the mail is sent would display the contents of the body variable and terminate the script allowing you to see that the values were missing without having to send an email.

    var_dump($company);
    die;

    would show the type and data, if any of the $company variable to confirm that it indeed has not been assigned a value.

    http://ca.php.net/manual/en/reserved.variables.post.php

  3. #3
    Join Date
    Sep 2008
    Location
    Tel Aviv, Berlin, L.A.
    Posts
    165
    Just GLANCED at your script - if it WORKED on a different machine, I'd first of all put the message and header terminations into a proper "look", by using "\r\n", which is mandatory to make sure that email() can even HANDLE it (depending on your environment). I'm not kidding, try this first.

    Best from the south.

    P.S. And add "wordwrap($message,70);", for most readers otherwise truncate the content...
    ariell
    programming is understanding

    my most recent baby The Beatles - Fab Four beatlescomplete.com (now with content!)
    if u need help with utf, watch this first Hebrew/American Chars from same table (not very nice, but a working real life example)

  4. #4
    Join Date
    Oct 2008
    Posts
    5
    Hi,

    Thanks for your reply. I tried what you said and placed your suggested code above "$to = "test@yahoo.com";
    $too = "test@yahoo.com";
    $subject = "Contact: test";
    $from = $email;"......and so on..... in my code (hope that is the correct position to place). But it still did not work. Can you tell me where I should place correctly so that it works. As I mentioned earlier , I am absolutely new to PHP and borrowed this code from someone else.

    Thanks,

    Saktiyantra.

    Quote Originally Posted by tfk11 View Post
    It appears that the values of your variables ($name, $company, $address, etc.) are not being set. The script may have worked on another server that had "register globals" enabled. This setting is now disabled by default as it poses certain security risks.

    You can set the values to those posted to the script by using the $_POST array in the following manner.

    PHP Code:
    $name htmlspecialchars$_POST['name'] );
    $company htmlspecialchars$_POST['company'] );
    etc
    This assumes your form contains input tags with names that match the variables you're using. eg.

    <input type="text" name="name" />
    <input type="text" name="company" />
    etc.

    While debugging similar issues it helps to add a few debugging function calls to your code so you can see what's going on without having to check any emails or even wait for the entire script to execute.

    var_dump($body);
    die;

    placing this before the mail is sent would display the contents of the body variable and terminate the script allowing you to see that the values were missing without having to send an email.

    var_dump($company);
    die;

    would show the type and data, if any of the $company variable to confirm that it indeed has not been assigned a value.

    http://ca.php.net/manual/en/reserved.variables.post.php

  5. #5
    Join Date
    Oct 2008
    Posts
    5
    Hi,

    Thanks for your reply. Since I am new to PHP, I could not follow your suggestions. Is it possible for you to correct the code and give me please?

    Regards,

    Saktiyantra

    Quote Originally Posted by ariell View Post
    Just GLANCED at your script - if it WORKED on a different machine, I'd first of all put the message and header terminations into a proper "look", by using "\r\n", which is mandatory to make sure that email() can even HANDLE it (depending on your environment). I'm not kidding, try this first.

    Best from the south.

    P.S. And add "wordwrap($message,70);", for most readers otherwise truncate the content...

  6. #6
    Join Date
    Apr 2007
    Posts
    133
    just took a quick look and you need to set the headers correctly
    PHP Code:
    $message .= "MIME-Version: 1.0\r\n";
    $message .= "Content-Type: text/html; charset=\"iso-8859-1\"\n";

    //---------should be

    $headers .= "MIME-Version: 1.0\r\n";
    $headers .= "Content-type: text/html; charset=iso-8859-1\r\n"

  7. #7
    Join Date
    Apr 2007
    Posts
    1,664
    If it worked on another server and doesn't work on the new server then I would look at server configurations to give you a clue. I'm guessing this is old code from pre 'register globals off' days. If the new server has them turned off (as it should) then you need to refer to POSTed variable in the superglobal $_POST['posted_var']

    For testing purposes ONLY add this to the top of your scripts

    PHP Code:
    ini_set('register_globals'1); 
    If the form now works, rewrite the vars to use the correct $_POST way of calling on them. It's a mighty bad idea to leave this hack/testcode in place.

    To debug, remove the entire mail code and simply print_r($_POST) until you are sure you're getting the variables to the script.
    Last edited by SyCo; 10-23-2008 at 10:41 AM.
    Anti Linux rants are usually the result of a lack of Linux experience, while anti Windows rants are usually a result of a lot of Windows experience.

  8. #8
    Join Date
    Oct 2008
    Posts
    5
    Hi,

    The form worked on putting
    PHP Code:
    ini_set('register_globals'1); 
    as you suggested and I modified my code to:

    <?

    ini_set("SMTP","11.11.11.11.");

    //if($AttachDoc_size>0)
    // {
    // $AttachDoc_data = fread(fopen($AttachDoc, "rb"), filesize($AttachDoc));
    // $str = chunk_split(base64_encode($AttachDoc_data));
    // $AttachDoc_data = addslashes($AttachDoc_data);
    // }

    $Body="<table width=100&#37; border=1 cellspacing=5 cellpadding=5 bgcolor=#E4D8F5>
    <tr>
    <td width=200 align=right><font face=\"Arial, Helvetica, sans-serif\" size=\"2\">Category</font></td>
    <td bgcolor=#FFFFFF><font face=\"Arial, Helvetica, sans-serif\" size=\"2\">$category</font></font></td>
    </tr>
    <tr>
    <td align=right><font face=\"Arial, Helvetica, sans-serif\" size=\"2\">Name</font></td>
    <td bgcolor=#FFFFFF><font face=\"Arial, Helvetica, sans-serif\" size=\"2\">$name</font> </td>
    </tr>
    <tr>
    <td align=right><font face=\"Arial, Helvetica, sans-serif\" size=\"2\">Company</font></td>
    <td bgcolor=#FFFFFF><font face=\"Arial, Helvetica, sans-serif\" size=\"2\">$company</font></td>
    </tr>
    <tr>
    <td align=right><font face=\"Arial, Helvetica, sans-serif\" size=\"2\">Address</font></td>
    <td bgcolor=#FFFFFF><font face=\"Arial, Helvetica, sans-serif\" size=\"2\">$address</font></td>
    </tr>
    <tr>
    <td align=right><font face=\"Arial, Helvetica, sans-serif\" size=\"2\">City</font></td>
    <td bgcolor=#FFFFFF><font face=\"Arial, Helvetica, sans-serif\" size=\"2\">$city</font></td>
    </tr>
    <tr>
    <td align=right><font face=\"Arial, Helvetica, sans-serif\" size=\"2\">State</font></td>
    <td bgcolor=#FFFFFF><font face=\"Arial, Helvetica, sans-serif\" size=\"2\">$state</font> </td>
    </tr>
    <tr>

    <td align=right><font face=\"Arial, Helvetica, sans-serif\" size=\"2\">Zip</font></td>
    <td bgcolor=#FFFFFF><font face=\"Arial, Helvetica, sans-serif\" size=\"2\">$zip</font> </td>
    </tr>
    <tr>

    <td align=right><font face=\"Arial, Helvetica, sans-serif\" size=\"2\">Country</font></td>
    <td bgcolor=#FFFFFF><font face=\"Arial, Helvetica, sans-serif\" size=\"2\">$country</font></td>
    </tr>

    <tr>
    <td align=right><font face=\"Arial, Helvetica, sans-serif\" size=\"2\">Mobile</font></td>
    <td bgcolor=#FFFFFF><font face=\"Arial, Helvetica, sans-serif\" size=\"2\">$mobile</font> </td>
    </tr>
    <tr>

    <td align=right><font face=\"Arial, Helvetica, sans-serif\" size=\"2\">Telephone
    </font></td>
    <td bgcolor=#FFFFFF><font face=\"Arial, Helvetica, sans-serif\" size=\"2\">$telephone</font>
    </td>
    </tr>
    <tr>

    <td align=right><font face=\"Arial, Helvetica, sans-serif\" size=\"2\">Fax
    </font></td>
    <td bgcolor=#FFFFFF><font face=\"Arial, Helvetica, sans-serif\" size=\"2\">$Fax</font>
    </td>
    </tr>
    <tr>
    <td align=right><font face=\"Arial, Helvetica, sans-serif\" size=\"2\">Email</font></td>
    <td bgcolor=#FFFFFF><font face=\"Arial, Helvetica, sans-serif\" size=\"2\">$email</font></td>
    </tr> <tr>
    <td align=right><font face=\"Arial, Helvetica, sans-serif\" size=\"2\">Your Requirement</font></td>
    <td bgcolor=#FFFFFF><font face=\"Arial, Helvetica, sans-serif\" size=\"2\">$yourrequirement</font></td>
    </tr>
    </table>";


    $to = "test@yahoo.com";
    $too = "test@yahoo.com";
    $subject = "Contact: Test";
    $from = $email;
    $headers = "From:$from\n";
    $headers .= "Reply-To: $from\n";
    $headers .= "MIME-Version: 1.0\n";
    $headers .= "Content-Type: multipart/mixed; boundary=\"MIME_BOUNDRY\"\n";
    $headers .= "X-Sender: $from\n";
    $headers .= "X-Mailer: PHP4\n";
    $headers .= "X-Priority: 3\n";
    $headers .= "Return-Path: $from\n";
    $headers .= "This is a multi-part message in MIME format.\n";

    $message = "--MIME_BOUNDRY\n";
    $message .= "Content-Type: text/plain; charset=\"iso-8859-1\"\n";
    $message .= "Content-Transfer-Encoding: quoted-printable\n";
    $message .= "\n";
    $message .= "\n";
    $message .= "--MIME_BOUNDRY\n";

    $message = "--MIME_BOUNDRY\n";
    $message .= "MIME-Version: 1.0\r\n";
    $message .= "Content-Type: text/html; charset=\"iso-8859-1\"\n";
    $message .= "\n";
    $message .= "$Body\n";
    $message .= "\n";

    $category = ( $_POST['category'] );
    $name = ( $_POST['name'] );
    $company = ( $_POST['company'] );
    $address = ( $_POST['address'] );
    $city = ( $_POST['city'] );
    $state = ( $_POST['state'] );
    $zip = ( $_POST['zip'] );
    $country = ( $_POST['country'] );
    $mobile = ( $_POST['mobile'] );
    $telephone = ( $_POST['telephone'] );
    $fax = ( $_POST['fax'] );
    $email = ( $_POST['email'] );
    $yourrequirement = ( $_POST['yourrequirement'] );

    //if($AttachDoc_size >0){
    // $message .= "--MIME_BOUNDRY\n";
    // $message .= "Content-Type: $AttachDoc_type; name=\"$AttachDoc_name\"\n";
    // $message .= "Content-disposition: attachment\n";
    // $message .= "Content-Transfer-Encoding: base64\n";
    // $message .= "\n";
    // $message .= "$str\n";
    // $message .= "\n";
    //message ends
    //}

    mail($to, $subject, $message, $headers);
    include("AutoResponder.php");

    header("location:../htmls/thank_you.htm");
    ?>

    I hope this was correct. Also I still did not receive the form data in my email. Earlier the autoresponder was not working, but with the change in code as above, autoresponder started working.

    Can you advice here please? Also please note that I am a novice to PHP and you are explaining to an absolute beginner who has borrowed this code from elsewhere .

    Thank You,

    Saktiyantra.

    Quote Originally Posted by SyCo View Post
    If it worked on another server and doesn't work on the new server then I would look at server configurations to give you a clue. I'm guessing this is old code from pre 'register globals off' days. If the new server has them turned off (as it should) then you need to refer to POSTed variable in the superglobal $_POST['posted_var']

    For testing purposes ONLY add this to the top of your scripts

    PHP Code:
    ini_set('register_globals'1); 
    If the form now works, rewrite the vars to use the correct $_POST way of calling on them. It's a mighty bad idea to leave this hack/testcode in place.

    To debug, remove the entire mail code and simply print_r($_POST) until you are sure you're getting the variables to the script.
    Last edited by saktiyantra; 10-24-2008 at 04:43 AM.

  9. #9
    Join Date
    Oct 2008
    Posts
    5
    Hi,

    This did not work.

    Saktiyantra.

    Quote Originally Posted by roscor View Post
    just took a quick look and you need to set the headers correctly
    PHP Code:
    $message .= "MIME-Version: 1.0\r\n";
    $message .= "Content-Type: text/html; charset=\"iso-8859-1\"\n";

    //---------should be

    $headers .= "MIME-Version: 1.0\r\n";
    $headers .= "Content-type: text/html; charset=iso-8859-1\r\n"

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles