Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Other Adding a variable to existing mail script help

  • This topic is empty.
Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #24039

    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 = "[email protected]";
    $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

    #53511

    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?

    #53513

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

Viewing 3 posts - 1 through 3 (of 3 total)
  • The forum ‘Other’ is closed to new topics and replies.