making a form mail script and it will only display one form value

Discussion in 'PHP' started by SwiftKiwi, Sep 22, 2009.

  1. #1
    I have made a very basic form mail script and I am POSTing multiple fields to it.

    Here's what I have:
    
    <?
    //	Mail Settings
    $to		= 'XXXXXXXXXXXXXX';
    $subject	= 'Mail from: '.$_SERVER["SERVER_NAME"].'';
    $from_name= 'Mailer';
    $from_email	= 'mail@'.$_SERVER["SERVER_NAME"].'';
    $headers 	= 'From: ' . $from_name . ' <' . $from_email . ">\r\n" . 'Reply-To: ' . $email . "\r\n";
    
    foreach($_POST as $key => $value)
    $body 	= array($key.": ".$value);
    			
    // Send the Mail
    mail($to , $subject, $body, $headers) or die ('Something went a little wrong.');
    echo "Your form was sucessfully sent.";
    ?>
    
    PHP:
    It works but it only send one field on the form page to the email address specified...

    Probably something easy but I'm just beginning with PHP so don't shoot me :)
     
    SwiftKiwi, Sep 22, 2009 IP
  2. phprightnow

    phprightnow Peon

    Messages:
    296
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Try enclosing all the foreach commands in brackets, like this...

    
    foreach($_POST as $key => $value) {
     // Your email code here
    }
    PHP:

    Also, try doing a var_dump($_POST); to make sure your form is sending all the fields correctly.
     
    phprightnow, Sep 22, 2009 IP
  3. zeronese

    zeronese Peon

    Messages:
    83
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #3
    just a fast notice, but i think from your code, you will only receive the data from the last field.

    Maybe this will fix it:
    instead of:
    foreach($_POST as $key => $value)
    $body   = array($key.": ".$value);
    PHP:
    consider something like:
    $body = "";
    foreach($_POST as $key => $value)
    $body   .= $key.": ".$value."\n";
    PHP:
     
    zeronese, Sep 23, 2009 IP
  4. phprightnow

    phprightnow Peon

    Messages:
    296
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #4
    That's also a good sugestion, compiling all the post variables in one variable rather than sending each individually. Saves time and effort on your mailserver aswell.
     
    phprightnow, Sep 23, 2009 IP