treehouse : what would you like to learn today?
Web Design Web Development iOS Development

Sending An email from a form Using PHP

  • Hi, I am now leaerning php and kinda getting the hang of the language but I am stuck... :S I am trying to send an email from a form using php and I keep getting the error undefined variable

    here is my form code...
    <form method="post" action="" onsubmit="return validation(this)">

    First Name:<br/>
    <input type="text" name="CustomerFname" size="30" />
    <br/>
    Surname:<br/>
    <input type="text" name="CustomerLname" size="30" />
    <br/>
    Email:<br/>
    <input type="text" name="CustomerMail" size="30" />
    <br/>
    Contact:<br/>
    <input type="text" name="CustomerNumber" size="30" />
    <br/>
    Comments:<br/>
    <textarea id="CustomerComments"></textarea>
    <br/>
    <input type="submit" name="submit" value="Send!"/>

    </form>

    my php script is....

    <?php

    if(array_key_exists('send',$_POST)) {

    $to = 'kevon.garcia@gmail.com';
    $subject = 'Customer Request';

    $FirstName = $_POST['CustomerFname'];
    $LastName = $_POST['CustomerLname'];
    $Email = $_POST['CustomerMail'];
    $Contact = $_POST['CustomerNumber'];
    $Comments = $_POST['CustomerComments'];


    $message = "Customer First Name: $FirstName\n\n";
    $message .= "Customer Last Name: $LastName\n\n";
    $message .= "Customer E-mail Address: $Email\n\n";
    $message .= "Customer Contact: $Contact\n\n";
    $message .= "Customer Comments: $Comments\n\n";

    $mail = mail($to, $subject, $message);

    }

    ?>

    and my php output code when it is successful and when it fails is

    <?php

    if ($_POST && !$mail) {
    ?>
    <h1 class="error">Sorry</h1>
    <p>There was a problem sending the message. Please try again later.</p>

    <?php
    } elseif ($_POST && $mail) {
    ?>
    <h1 class="good">Success!</h1>
    <p>Your message has been sent. I will get back to you as soon as possible.</p>

    <?php } ?>

    Kindly can anyone tell me where I went wrong :( Please please
  • $message = \"Customer First Name: $FirstName\n\n\";
    $message .= \"Customer Last Name: $LastName\n\n\";
    $message .= \"Customer E-mail Address: $Email\n\n\";
    $message .= \"Customer Contact: $Contact\n\n\";
    $message .= \"Customer Comments: $Comments\n\n\";

    I'm no PHP whiz, but this looks wrong off the bat. I don't think you can declare a $variable more than once?

    Try changing that to:
    $message = \"
    First Name: $FirstName\n
    Last Name: $LasttName\n
    E-Mail: $Email\n
    Customer Contact: $Contactate\n
    Message: $Comments\" ;


    EDIT:

    Also, try adding ID's to your form, as I'm not 100% where PHP grabs the name from, it might be the ID field:
    Surname:<br/>
    <input type=\"text\" id=\"CustomerLname\" name=\"CustomerLname\" size=\"30\" />
  • Thanks, but i get the same error message it says undefined variable : mail and that line in my code is this

    if ($_POST && !$mail) {

    I am baffled at this point...

    Really i do appreciate your help the tips were very informative
  • Have a look through this tutorial:

    http://scriptplayground.com/tutorials/p ... -with-PHP/

    It was my saviour in the past when I was first starting out with PHP.
  • Doc,

    he was appending more to that variable, so it was only one variable....

    dude I will have a look at this after work and iron out any problems later
  • I amended your PHP code it should now work, however it is not a safe script, no captchas not redirect if nothing is filled in...... but I just made what you already had work.....

    but I am getting your script to redirect if not successful where you can just add in the basic h2 if you want.

    <?php
    /*
    ******************* get posted data into local variables *******************
    */
    $EmailTo = \"kevon.garcia@gmail.com\";
    $Subject = \"Customer Request\";
    $FirstName = $_REQUEST['CustomerFname'];
    $LastName = $_REQUEST['CustomerSname'];
    $Contact = $_REQUEST['CustomerNumber'];
    $Email = $_REQUEST['CustomerMail'];
    $Comments = $_REQUEST['CustomerComments'];

    /*
    ******************* validation *******************
    */
    $validationOK=true;
    if (!$validationOK)
    {
    print \"<meta http-equiv=\\"refresh\\" content=\\"0;URL=http://yoursite/error.php\\">\";
    exit;
    }

    /*
    ******************* prepare email body text *******************
    */
    $message = \"Customer First Name: $FirstName\n\n\";
    $message .= \"Customer Last Name: $LastName\n\n\";
    $message .= \"Customer E-mail Address: $Email\n\n\";
    $message .= \"Customer Contact: $Contact\n\n\";
    $message .= \"Customer Comments: $Comments\n\n\";

    /*
    ******************* Send mail if all successful *******************
    */
    $success = mail($EmailTo, $Subject, $Body, \"From: <$email>\", \"-froot@youremail\");

    /*
    ******************* Send mail to recipient to thank them if all successful & redirect *******************
    */
    // redirect to success page
    if ($success)
    {
    //sending mail to the person who filled in the form if they filled in the email
    mail($email, $Subject, \"$name\n\nThank you for contacting me.\nYour message will be passed on to the relevant person who will contact you as soon as possible regarding your query.\", \"From: <$EmailTo>\", \"-froot@youremail\");
    //show the thanks page for a successful email
    print \"<meta http-equiv=\\"refresh\\" content=\\"0;URL=http://your site/thanks.php\\">\";
    }
    else
    {
    //show the error page for an un-successful email
    print \"<meta http-equiv=\\"refresh\\" content=\\"0;URL=http://your site/error.php\\">\";
    }
    //end script
    ?>