PHP code won't work properly... need help figuring out what I've done wrong.

Discussion in 'PHP' started by NScarpinato, Jun 29, 2008.

  1. #1
    I have a bit of PHP that doesn't seem to like me much at all. I copied it from a PHP script site, but apparently either I've done something strange, or the script needs to be tweaked. Here's the code and the error I'm getting:

    <?php
    $to = $_POST['sendto'];
    $from = $_POST['Email'];
    $name = $_POST['Name'];
    $headers = "From: $from";
    $subject = "Web Contact Data";
    
    $fields = array();
    $fields{"Name"} = "Name";
    
    $body = "We have received the following information:\n\n"; foreach($fields as $a => $b){ $body .= sprintf("%20s: 
    
    %s\n",$b,$_REQUEST[$a]); }
    
    $headers2 = "From: $to";
    $subject2 = "Thank you for contacting us.";
    $autoreply = "Thank you for contacting us. Somebody will get back to you as soon as possible, usually within 48 hours. If you have 
    
    any more questions, please consult our website at www.bbsstoneworks.com.";
    
    if($from == '') {print "You have not entered an email, please go back and try again";}
    else {
    if($name == '') {print "You have not entered a name, please go back and try again";}
    else {
    $send = mail($to,$subject,$body,$headers);
    $send2 = mail($from,$subject2,$autoreply,$headers2);
    if($send)
    ~~~This line was removed for debugging purposes~~~
    else
    {print "We encountered an error sending your mail, please notify scorpious@project17productions.com"; }
    }
    }
    ?>
    PHP:
    The error I'm getting is this:

    Warning: mail() [function.mail]: SMTP server response: 451 See http://pobox.com/~djb/docs/smtplf.html. in D:\Hosting\3049459\html\contact.php on line 21


    The code submits two emails, one to the person who's sending the mail from the form (the autoreply mail that tells them someone will contact them, etc.), and one to whoever is selected in a <select> box from contact.html. The problem is, that second mail never gets sent, and I think it's because the $fields array isn't working properly. But the first email, the one that goes to the person filling in the form, sends just fine. I don't have a way to debug the code to figure out where my problem is. Online PHP validators say that the syntax is all kosher, but the second email doesn't send until I change the $body line to something other than the array.


    Can anyone help this poor misguided soul who's losing his mind trying to figure this out without the proper tools? :D
     
    NScarpinato, Jun 29, 2008 IP
  2. php-lover

    php-lover Active Member

    Messages:
    261
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    58
    #2
    $headers2 = "From: $to";

    Replace this line above with this

    $headers2 = "From: $to \r\n" ;
     
    php-lover, Jun 29, 2008 IP
  3. David Pankhurst

    David Pankhurst Member

    Messages:
    39
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    43
    #3
    The reference page gives you the clue - you have a bare linefeed (in PHP, just \r without \n), and the email program complained.

    Since that's not in the code, maybe it's an edit on your system doing it - take the lines that are split:

    $body = "We have received the following information:\n\n"; foreach($fields as $a => $b){ $body .= sprintf("%20s: 
    
    %s\n",$b,$_REQUEST[$a]); }
    Code (markup):
    and

    $autoreply = "Thank you for contacting us. Somebody will get back to you as soon as possible, usually within 48 hours. If you have 
    
    any more questions, please consult our website at www.bbsstoneworks.com.";
    Code (markup):
    and either make them all one line, or add surrounding quotes like this:



    $body = "We have received the following information:\n\n"; foreach($fields as $a => $b){ $body .= sprintf("%20s: %s\n",$b,$_REQUEST[$a]); }
    Code (markup):
    and

    $autoreply = "Thank you for contacting us. Somebody will get back to you as soon as possible, usually within 48 hours. If you have ".
    " ".
    "any more questions, please consult our website at www.bbsstoneworks.com.";
    Code (markup):
     
    David Pankhurst, Jun 29, 2008 IP
  4. NScarpinato

    NScarpinato Peon

    Messages:
    9
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    I read that bare linefeed information, but I couldn't figure out which line in my code was causing it. But as soon as I change the "$body" line to something that doesn't use the $fields array, everything works fine.

    Could it be the "\n\n" causing this?
     
    NScarpinato, Jun 30, 2008 IP
  5. NScarpinato

    NScarpinato Peon

    Messages:
    9
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Ok, I changed the $body tag to replace [sprintf("%20s: %s\n",] with [sprintf("%20s: %s\r\n",]. That did the trick, but now I only see the last piece of the array in my email message.
     
    NScarpinato, Jun 30, 2008 IP
  6. David Pankhurst

    David Pankhurst Member

    Messages:
    39
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    43
    #6
    You only have one entry - just before the output line you do this:

    $fields = array();
    $fields{"Name"} = "Name";
    Code (markup):
    then you print out every entry in $fields - which will always be this one.

    perhaps you want all the request or post fields:

    $fields = $_POST;
    Code (markup):
    or

    $fields = $_REQUEST;
    Code (markup):
    or maybe specific fields

    $fields = array($_REQUEST['name'],$_REQUEST['email'],$_REQUEST['field1']);
    Code (markup):
     
    David Pankhurst, Jun 30, 2008 IP
  7. NScarpinato

    NScarpinato Peon

    Messages:
    9
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    I know that in the code I have posted, I only had one $fields line. This is what I have now:

    $fields = array();
    $fields{"Name"} = "Name";
    $fields{"Email"} = "Email";
    $fields{"Phone"} = "Phone";
    $fields{"Company"} = "Company";
    $fields{"Message"} = "Message";
    $fields{"list"} = "Mailing List";
    PHP:
    In my email that I receive that the address selected from the form, I only see:

    Mailing List: Yes

    Yes is the default for Mailing List. I think I will attempt this with your "$fields = array($_REQUEST['name'],$_REQUEST['email'],$_REQUEST['field1']);" solution and see if that fixes it... it seems like whoever wrote the PHP script I've been using wrote it using faulty or outdated PHP syntax.
     
    NScarpinato, Jun 30, 2008 IP
  8. NScarpinato

    NScarpinato Peon

    Messages:
    9
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    No dice. In fact, now I only get the "Yes" part. Something is wrong with the function that calls the array in the $body line, it's overwriting the $body variable rather than appending to it.
     
    NScarpinato, Jun 30, 2008 IP
  9. David Pankhurst

    David Pankhurst Member

    Messages:
    39
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    43
    #9
    Can you check this line:

    $body = "We have received the following information:\n\n"; foreach($fields as $a => $b){ $body [u][COLOR="DarkRed"].=[/COLOR][/u] sprintf("%20s: %s\n",$b,$_REQUEST[$a]); }
    Code (markup):
    If you've forgotten the period, then '$body=sprintf' instead of '$body.=sprintf' would do what you're saying.
     
    David Pankhurst, Jul 1, 2008 IP
  10. egoldseller

    egoldseller Guest

    Messages:
    213
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Hello !

    Dear Friend ,

    if you use this script from local web server like EasyPHP , apache ....

    mail function can't work , because smtp is not configured ,

    you most use this script in the real web server,

    I advice you my friend to use this script to send email , and don't use you classic script :

    
    
    <?php
    // destination email
    $to = "user@name.com";
    // you email ex : support@hostname.com
    $from = "you@name.com";
    // you subject
    $subject = "Write here you subject !";
    // message in text or in html
    $HTML = "<h3><img src='http://www.csphennaya.com/images/done.jpg' width='117' height='133' style='border: 1px solid Gray;'>\n\n<p>Dear $user</p>\n<p>\n<p>Thank you for registring in our website</p>\n</h3>";
    // call function to send email
    sendHTMLemail($HTML,$from,$to,$subject);
    ?>
    
    <?php
    function sendHTMLemail($HTML,$from,$to,$subject)
    {
    // First we have to build our email headers
    // Set out "from" address
    
        $headers = "From: $from\r\n"; 
    
    // Now we specify our MIME version
    
        $headers .= "MIME-Version: 1.0\r\n"; 
    
    // Create a boundary so we know where to look for
    // the start of the data
    
        $boundary = uniqid("HTMLEMAIL"); 
        
    // First we be nice and send a non-html version of our email
        
        $headers .= "Content-Type: multipart/alternative;".
                    "boundary = $boundary\r\n\r\n"; 
    
        $headers .= "This is a MIME encoded message.\r\n\r\n"; 
    
        $headers .= "--$boundary\r\n".
                    "Content-Type: text/html; charset=windows-1256\r\n".
                    "Content-Transfer-Encoding: base64\r\n\r\n"; 
                    
        $headers .= chunk_split(base64_encode($HTML)); 
    
    // Now we attach the HTML version
    
        $headers .= "--$boundary\r\n".
                    "Content-Type: text/html; charset=ISO-8859-1\r\n".
                    "Content-Transfer-Encoding: base64\r\n\r\n"; 
                    
        $headers .= chunk_split(base64_encode($HTML)); 
    
    // And then send the email ....
    
        mail($to,$subject,"",$headers);
        
    }
    ?>
    
    
    PHP:
    Enjoy

    If you need more help contact me ;

    Best Regards

    CHEMOURI , Electronic Engineer & Webmaster.
     
    egoldseller, Jul 1, 2008 IP
  11. NScarpinato

    NScarpinato Peon

    Messages:
    9
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11
    That did the trick. Thank you very much for all your help.

    I'm learning this on the fly, so I'm sure I'll be on here quite often looking for answers to my problems. :)
     
    NScarpinato, Jul 1, 2008 IP