Grow your CSS skills. Land your dream job.

Adding a variable to existing mail script help

  • # January 24, 2009 at 1:35 pm

    Hi all,
    I have this piece of code:

    <?php
    error_reporting(E_NOTICE);

    function valid_email($str)
    {
    return ( ! preg_match("/^([a-z0-9+_-]+)(.[a-z0-9+_-]+)*@([a-z0-9-]+.)+[a-z]{2,6}$/ix", $str)) ? FALSE : TRUE;
    }

    if($_POST!=” && $_POST!=” && valid_email($_POST)==TRUE && strlen($_POST)>1)
    {
    $to = "damian.herrington@yahoo.co.uk";
    $headers = ‘From: ‘.$_POST.”. "rn" .
    ‘Reply-To: ‘.$_POST.” . "rn" .
    ‘X-Mailer: PHP/’ . phpversion();
    $subject = "Contact Form";
    $message = htmlspecialchars($_POST);

    if(mail($to, $subject, $message, $headers))
    {
    echo 1; //SUCCESS
    }
    else {
    echo 2; //FAILURE – server failure
    }
    }
    else {
    echo 3; //FAILURE – not valid email
    }
    ?>

    As you can see it takes the information entered from the form and stores it before sending to my email address. But I have a form input called name, how would I add that variable, $_POST into $message either the persons name then the comment or vice versa?
    Thanks

    # January 24, 2009 at 1:51 pm

    You could modify the message variable like this:

    $message = htmlspecialchars($_POST) . "rnName: " . htmlspecialchars($_POST);

    The "." adds on to the string, and the "rnName: " section adds a new line and then the text "Name:" before the person’s name, just so your email is easy to understand.

    Make sense?

    # January 24, 2009 at 1:54 pm

    Thanks for the quick reply I will try that out later :-)

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

You must be logged in to reply to this topic.

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