简体   繁体   中英

PHP auto responder

I made a php form but i can't figgure out how I make it auto respond to a email that is filled in. I want to send a confurmation to a person who just used the form that we have it but i dont know how to make it.

Here a example of my code. I want to send a custom mail to the $email how do i do that?!

I made some changes but i dont recive emails jet. Maby i did understand you whrong but can you check it for me again please.

    <?php
/* Set e-mail recipient */
$myemail  = "opgeven@kidsnthingspernis.nl";

/* Check all form inputs using check_input function */
$subject  = check_input($_POST['subject']);

$Voornaam = check_input($_POST['Voornaam'], "Vul A.U.B. uw Voornaam in.");
$Achternaam = check_input($_POST['Achternaam'], "Vul A.U.B. uw Achternaam in.");

$VoornaamKind = check_input($_POST['VoornaamKind'], "Vul A.U.B. de Voornaam van uw in.");
$AchternaamKind = check_input($_POST['AchternaamKind'], "Vul A.U.B. de Achternaam van uw in.");

$email    = check_input($_POST['email'], "Vul A.U.B. uw Email adres in.");

$Leeftijd = check_input($_POST['Leeftijd'], "Vul A.U.B. de leeftijd van uw kind in.");

$Groep = check_input($_POST['Groep'], "Vul A.U.B. de basisschoolgroep van uw in.");

$Opmerking= ($_POST['Opmerking']);

/* If e-mail is not valid show error message */
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email))
{
    show_error("E-mail adres klopt niet");
}


/* Let's prepare the message for the e-mail */
$message = "Hallo!

Je contact formulier is ingevuld door:

Ouder/Verzorger
Voornaam: $Voornaam
Achternaam: $Achternaam

Kind:
Voornaam Kind: $VoornaamKind
Achternaam Kind: $AchternaamKind

E-mail: $email

Groep: $Groep

Leeftijd: $Leeftijd

Opmerking:
$Opmerking

Einde bericht.
"
;

/* Send the message using mail() function */
mail($myemail, $subject, $message);

/* Redirect visitor to the thank you page */
header('Location: bedankt.html');
exit();

/* Functions we used */
function check_input($data)
{
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    if (strlen($data) == 0)
    {
        return false;
    }
    else
    {
        return $data;
    }
}

function show_error($myError)
{
?>



    <html>
    <body>

    <b>Please correct the following error:</b><br />
    <?php echo $myError; ?>

    </body>
    </html>



<?php
$email      = 'email';
$subject = 'subject';
$message = "Hallo!

U/jij bent nu opgegeven voor Kids N Theatre met De Vliegende Speeldoos.
Dit is wat wij aan gegevens hebben gekregen:

Ouder/Verzorger
Voornaam: $Voornaam
Achternaam: $Achternaam

Kind:
Voornaam Kind: $VoornaamKind
Achternaam Kind: $AchternaamKind

E-mail: $email

Groep: $Groep

Leeftijd: $Leeftijd

Opmerking:
$Opmerking

Einde bericht.
";
$headers = 'From: opgeven@kidsnthingspernis.nl' . "\r\n" .
    'Reply-To: opgeven@kidsnthingspernis.nl' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

mail($email, $subject, $message, $headers);
?>

<?php
exit();
}
?>

There are multiple errors in the way you've setup this script, let me try and point them all out to you.

First of all, you're using header('Location... , irrespective of if any of the show_error functions are actually being echoed / printed above it, which will result in an error of...

Warning: Cannot modify header information - headers already sent by

This is because you are not allowed to output anything before header (read more about this in the PHP documentation of header ).

So to avoid this, you'll have to tell your script somehow that there is an error, and check against it, then only allow header to be executed if there are no errors.

In your case, I'd remove the $problem variable from your check_input function, and would turn it into an array of error messages instead. This way you to be able to output multiple error messages, if there are multiple form inputs that have not been filled in correctly:

$problem = array();

I'd modify the check_input function to return false in case the validation fails:

function check_input($data)
{
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    if (strlen($data) == 0)
    {
        return false;
    }
    else
    {
        return $data;
    }
}

Further up, according to the changes, I'd use this function the following way:

$Leeftijd = check_input($_POST['Leeftijd']);

if (!$Leeftijd)
{
    $problem[] = "Vul A.U.B. uw Leeftijd in.";
    // Square brackets ('[]') are one of the ways to add an element to an array
}

I'd also avoid using your show_error this way, as it could cause the same error with header because you're, plain and simple, outputting html code using that function just above it.

Consequently I would only use it when checking against the $problem array, and only if there are errors, otherwise allow to send the email. For it to work properly, you'd also have to modify it a little:

function show_error($myError)
{ ?>
    <html>
        <body>

            <b>Please correct the following error(s):</b><br />
            <?php
            foreach($myError as $error)
            // the `foreach` function takes care of looping through the array
            {
               echo $myError."<br />";
               // adding a line break to be able to show multiple errors
            }
            ?>

        </body>
    </html>
<?php } ?>

Then I'd use the final check and the above modified function the following way:

if (count($problem) > 0)
{
    show_error($problem);
}
else
{
    mail($myemail, $subject, $message);
}

I know there are many other ways to do this, but I found this was the one that modified your existing script the least by providing one of the most ideal results.

Note: I know that curly brackets ( { and } ) are not always necessary, but it's probably best for a newby to start with.

This is a Working script!! Actually really easy. Thanks for the help annyway!

<?php
/* Set e-mail recipient */
$myemail  = 'opgeven@kidsnthingspernis.nl' . ', '; // note the comma
$email .= check_input($_POST['email']);

/* Check all form inputs using check_input function */
$subject  = check_input($_POST['subject']);

$Voornaam = check_input($_POST['Voornaam'], "Vul A.U.B. uw Voornaam in.");
$Achternaam = check_input($_POST['Achternaam'], "Vul A.U.B. uw Achternaam in.");

$VoornaamKind = check_input($_POST['VoornaamKind'], "Vul A.U.B. de Voornaam van uw in.");
$AchternaamKind = check_input($_POST['AchternaamKind'], "Vul A.U.B. de Achternaam van uw in.");

$email    = check_input($_POST['email'], "Vul A.U.B. uw Email adres in.");

$Leeftijd = check_input($_POST['Leeftijd'], "Vul A.U.B. de leeftijd van uw kind in.");

$Groep = check_input($_POST['Groep'], "Vul A.U.B. de basisschoolgroep van uw in.");

$Opmerking= ($_POST['Opmerking']);

/* If e-mail is not valid show error message */
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email))
{
    show_error("E-mail adres klopt niet");
}


/* Let's prepare the message for the e-mail */
$message = "Hallo!

Yes er is weer iemand opgegeven voor Kids 'N Things met De Vliegende Speeldoos.
Dit zijn de gegevens:

Ouder/Verzorger
Voornaam: $Voornaam
Achternaam: $Achternaam

Kind:
Voornaam Kind: $VoornaamKind
Achternaam Kind: $AchternaamKind

E-mail: $email

Groep: $Groep

Leeftijd: $Leeftijd

Opmerking:
$Opmerking

Einde bericht.
"
;

/* Send the message using mail() function */
mail($myemail, $subject, $message);
mail($email, $subject, $message);

/* Redirect visitor to the thank you page */
header('Location: bedankt.html');
exit();

/* Functions we used */
function check_input($data)
{
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    if (strlen($data) == 0)
    {
        return false;
    }
    else
    {
        return $data;
    }
}

function show_error($myError)
{
?>



    <html>
    <body>

    <b>Please correct the following error:</b><br />
    <?php echo $myError; ?>

    </body>
    </html>

<?php
exit();
}
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM