Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Noob php help

Options
  • 30-06-2009 9:01pm
    #1
    Registered Users Posts: 18,272 ✭✭✭✭


    I am trying to use php for the first time, have no experience in it at all

    I have created a simple form on a website and am trying to use php to get it sent to an email address.

    However when I try to process the form I get the following error message
    error wrote:
    Warning: mail() [function.mail]: SMTP server response: 501 5.5.4 Invalid Address in D:\Domains\rmbuckets.com\wwwroot\do_sendform.php on line 14

    Here is the php code

    [PHP]<?php

    $msg = "Sender Name:\t$sender_name\n";
    $msg .= "Sender E-Mail:\t$sender_email\n";
    $msg .= "Message:\t$message\n\n";

    $recipient = "donal@rmbuckets.com";
    $subject = "Web Site Feedback";


    $mailheaders = "From: My Web Site <> \n";
    $mailheaders .= "Reply-To: $sender_email\n\n";

    mail($recipient, $subject, $msg, $mailheaders);

    echo "<HTML><HEAD><TITLE>Form Sent!</TITLE></HEAD><BODY>";
    echo "<H1 align=center>Thank You, $sender_name</H1>";
    echo "<P align=center>Your feedback has been sent.</P>";
    echo "</BODY></HTML>";

    ?> [/PHP]

    As I said I have no php experience so I'm just working off a tutorial with this code, can anyone spot what is wrong?

    The form sent page displays with the Thank You, Your feedback has been sent. being displayed however the error message appears above it and the email is not sent.


Comments

  • Registered Users Posts: 379 ✭✭TheWaterboy


    Im assuming that you are testing this form on your local development setup. Usually PHP / Apache is installed with the mail functions turned off and hence wont send emails from a local environment.

    You can setup the smtp stuff within your php.ini file - Do a google search on sorting this out

    Also to get rid of the error for testing purposes put @ infront of the mail() function.

    Finally you should put in some code like this to display the correct message

    if (mail($recipient, $subject, $msg, $mailheaders))
    {
    $message = "Thank You";
    } else {
    $message = "Your message has not sent";
    }

    echo "<HTML><HEAD><TITLE>Form Sent!</TITLE></HEAD><BODY>";
    echo "<H1 align=center>".$message."</H1>";
    echo "</BODY></HTML>";


  • Registered Users Posts: 18,272 ✭✭✭✭Atomic Pineapple


    No i've done it online on my web hosting, its running php5 but I dont know how to find the php.ini file?


  • Registered Users Posts: 8,070 ✭✭✭Placebo


    draffodx wrote: »
    No i've done it online on my web hosting, its running php5 but I dont know how to find the php.ini file?

    you dont/cant change the ini on your hosting,
    the errors you quoted is local

    'D:\Domains\rmbuckets.com\wwwroot\do_sendform.php on line 14'

    also use trim

    $recipient = trim("donal@rmbuckets.com");


  • Registered Users Posts: 6,509 ✭✭✭daymobrew


    You can setup the smtp stuff within your php.ini file - Do a google search on sorting this out
    You can use the ini_set() function to achieve this. [PHP]ini_set( 'SMTP', 'smtp.myhost.net' );[/PHP]
    Of course, this is probably already set correctly in the main php.ini file. A call to phpinfo() should tell you the system wide SMTP setting.

    I recommend you try the first example on the mail() page: [PHP]<?php
    $message = "Line 1\nLine 2\nLine 3";

    // Send
    mail('donal@rmbuckets.com', 'My Subject', $message);
    ?>[/PHP]


  • Registered Users Posts: 18,272 ✭✭✭✭Atomic Pineapple


    Thats the file path on my web hosting,

    See the picture below, I dont have much experience with web hosting either so I may be doing that completely wrong too

    84019.jpg


  • Advertisement
  • Registered Users Posts: 18,272 ✭✭✭✭Atomic Pineapple


    daymobrew wrote: »

    I recommend you try the first example on the mail() page

    I changed the code to the following

    [PHP]<?php
    $message = "Line 1\nLine 2\nLine 3";

    // Send
    mail('donal@rmbuckets.com', 'My Subject', $message);


    echo "<HTML><HEAD><TITLE>Form Sent!</TITLE></HEAD><BODY>";
    echo "<H1 align=center>Thank You, $sender_name</H1>";
    echo "<P align=center>Please note the email is not yet working please email r&mbuckets directly.</P>";
    echo "</BODY></HTML>";

    ?> [/PHP]

    And now I get the following error
    Error wrote:
    Warning: mail() [function.mail]: "sendmail_from" not set in php.ini or custom "From:" header missing in D:\Domains\rmbuckets.com\wwwroot\do_sendform.php on line 5


  • Registered Users Posts: 6,509 ✭✭✭daymobrew


    Try setting the sendmail_from with: [PHP]ini_set( 'sendmail_from', '' );[/PHP]
    Or, now that I look at the original code again, maybe PHP is complaining about the invalid address (in this case missing address) in: [PHP]$mailheaders = "From: My Web Site <> \n";[/PHP]
    Change this to [PHP]$mailheaders = "From: My Web Site <$sender_email>\n";[/PHP]


  • Registered Users Posts: 18,272 ✭✭✭✭Atomic Pineapple


    daymobrew wrote: »
    Try setting the sendmail_from with:

    Thanks daymobrew,

    I have the email sending when the form is sent now but when I receive the email the only parts it has are the headers, so I receive this as an email

    Sender Name:
    Sender E-Mail:
    Message:

    So the parts the user filled out are empty.

    heres the form:

    [HTML]<FORM method="POST" action="do_sendform.php">

    <P>Your Name:<br>
    <INPUT type="text" name="sender_name" size=30>
    </p>

    <P>Your E-Mail Address:<br>
    <INPUT type="text" name="sender_email" size=30>
    </p>

    <P>Message:<br>
    <textarea name="message" cols=30 rows=5></textarea>
    </p>

    <INPUT type="submit" value="Send This Form">

    </FORM> [/HTML]

    I have the variable names the same I think but what am I leaving out so that the info doesn't get passed?


  • Registered Users Posts: 1,045 ✭✭✭Bluefrog


    You have to explicitly set the variables like $sender_name to their values posted through the form so something like
    $sender_name = $_POST['sender_name']// each $_POST value is accessed using the name of the input in the form
    

    For cosmetics you could do
    $sender_name = ucwords(trim($_POST['sender_name']));
    

    The trim() function strips any whitespace (spaces) from both ends of a string passed to it while the ucwords() one will capitalise the first letter of each word so if someone put in 'john smith' you would get 'John Smith'.

    (yes, I know, ucwords won't take care of names like O'Brien very well but that's for another day)


  • Registered Users Posts: 6,509 ✭✭✭daymobrew


    draffodx wrote: »
    Thanks daymobrew,

    I have the email sending when the form is sent now but when I receive the email the only parts it has are the headers, so I receive this as an email
    Which change worked? Using ini_set or adding the email address to the 'From:' header line?


  • Advertisement
  • Registered Users Posts: 18,272 ✭✭✭✭Atomic Pineapple


    Thanks Bluefrog, I have it working when I use this now

    [PHP]$msg = trim($_POST)[/PHP]

    However I want to break up the response in the email and so have tried to do that this way

    [PHP]$msg = "Sender Name:\trim($_POST)\n"[/PHP]

    however when I do this I get the following error
    Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in D:\Domains\rmbuckets.com\wwwroot\do_sendform.php on line 6

    I've tried the Syntax several different ways but always get the same message


    daymobrew wrote: »
    Which change worked? Using ini_set or adding the email address to the 'From:' header line?

    I tried both ways and both worked, thanks


  • Registered Users Posts: 6,509 ✭✭✭daymobrew


    [PHP]$msg = "Sender Name:\trim($_POST)\n" [/PHP] Try breaking it up so that the PHP engine knows that trim is a function: [PHP]$msg = "Sender Name: " . trim($_POST) . "\n";[/PHP]


  • Registered Users Posts: 18,272 ✭✭✭✭Atomic Pineapple


    daymobrew wrote: »
    [PHP]$msg = "Sender Name:\trim($_POST)\n" [/PHP] Try breaking it up so that the PHP engine knows that trim is a function: [PHP]$msg = "Sender Name: " . trim($_POST) . "\n";[/PHP]

    cheers that sorted it out, thank you very much


  • Registered Users Posts: 1,045 ✭✭✭Bluefrog


    I'd get into the habit now of using apostrophes to delimit strings rather than quotes unless you are formatting strings. So for instance:

    $mystring = 'This is my string'; instead of $mystring = "This is my string";

    Apostrophes are a little faster to process because if you use quotes, as you found out, PHP will check to see if there are formatting characters and apply them if it finds them. In your case PHP saw "Field Name: \trim($_POST)" and that's why it got confused.

    Normally I would do something like:
    $body .= 'You have a message, details follow:';
    $body .= "\n\nField Name:\t\t".trim($_POST);

    where the "\n" makes a line break and "\t" a tab space. While you can put PHP variables in quoted strings, apart from avoiding mixups by using apostrophes instead, it is also a hell of a lot easier to read when syntax highlighted.


  • Registered Users Posts: 6,509 ✭✭✭daymobrew


    Bluefrog wrote: »
    I'd get into the habit now of using apostrophes to delimit strings rather than quotes unless you are formatting strings. So for instance:

    $mystring = 'This is my string'; instead of $mystring = "This is my string";

    Apostrophes are a little faster to process because if you use quotes, as you found out, PHP will check to see if there are formatting characters and apply them if it finds them.
    I've seen a few pages that have benchmarks that show that there isn't much of a differences.
    See the bottom of The PHP Bench where it concludes "In today's versions of PHP it looks like this argument has been satisfied on both sides of the line. Lets all join together in harmony in this one!".
    Similar results were found here.

    Having said that, I agree that it is good practice as it helps to avoid unnecessary conversion of variables (when put inside double quotes unnecessarily).


  • Registered Users Posts: 1,045 ✭✭✭Bluefrog


    Well, whatever about the interpreter, my main issue (being half blind) is the readability in syntax highlighted code and the way quoted variables inevitably get the same colour as the surrounding string.

    I do think putting coding conventions in place for yourself right from the start will save a lot of head scratching down the line. Also I'd add that commenting is a great habit to get into - was reviewing some 3 year old code today and was glad I had done them.


Advertisement