Grow your CSS skills. Land your dream job.

Sending An email from a form Using PHP

  • # March 18, 2009 at 7:52 am

    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;
    $LastName = $_POST;
    $Email = $_POST;
    $Contact = $_POST;
    $Comments = $_POST;

    $message = "Customer First Name: $FirstNamenn";
    $message .= "Customer Last Name: $LastNamenn";
    $message .= "Customer E-mail Address: $Emailnn";
    $message .= "Customer Contact: $Contactnn";
    $message .= "Customer Comments: $Commentsnn";

    $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

    # March 18, 2009 at 11:55 am
    Code:
    $message = “Customer First Name: $FirstNamenn”;
    $message .= “Customer Last Name: $LastNamenn”;
    $message .= “Customer E-mail Address: $Emailnn”;
    $message .= “Customer Contact: $Contactnn”;
    $message .= “Customer Comments: $Commentsnn”;

    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:

    Code:
    $message = ”
    First Name: $FirstNamen
    Last Name: $LasttNamen
    E-Mail: $Emailn
    Customer Contact: $Contactaten
    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:

    Code:
    Surname:
    # March 18, 2009 at 12:21 pm

    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

    # March 18, 2009 at 12:42 pm

    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.

    # March 20, 2009 at 3:35 am

    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

    # March 20, 2009 at 12:55 pm

    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.

    Code:
    < ?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 "“;
    exit;
    }

    /*
    ******************* prepare email body text *******************
    */
    $message = “Customer First Name: $FirstNamenn”;
    $message .= “Customer Last Name: $LastNamenn”;
    $message .= “Customer E-mail Address: $Emailnn”;
    $message .= “Customer Contact: $Contactnn”;
    $message .= “Customer Comments: $Commentsnn”;

    /*
    ******************* 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, “$namennThank 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 ““;
    }
    else
    {
    //show the error page for an un-successful email
    print ““;
    }
    //end script
    ?>

Viewing 6 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic.

*May or may not contain any actual "CSS" or "Tricks".