1

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();
}
?>
  • Check the `mail()` function of PHP. – KIKO Software Oct 11 '14 at 10:40
  • add php mail() after your operations... – Jayakarthik Appasamy Oct 11 '14 at 10:45
  • what happens when you try this code? – benomatis Oct 11 '14 at 10:45
  • I updated your question and separated the new edit from the original one, to ensure that the answers already given make sense. Please do not do major edits to your question once it has already been answered, if you still need to do so, mark it as an edit and post it additionally to the original question, not replacing it... – benomatis Oct 11 '14 at 12:27
  • Shouldn't styling be done inline when using the `mail` function of php or am i wrong here? – Dorvalla Oct 11 '14 at 12:35
  • Your emailaddress validation is utterly broken https://emailtester.pieterhordijk.com/test-pattern/NjY. Click [here](http://stackoverflow.com/questions/12026842/how-to-validate-an-email-address-in-php/12026863#12026863) to fix it. – PeeHaa Oct 11 '14 at 17:44
  • I can send a mail to $myemail but not a extra mail to $email – Thom Knepper Oct 11 '14 at 17:55

2 Answers2

0

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.

benomatis
  • 5,536
  • 7
  • 36
  • 59
  • I have the most of this code from internet i only made a few changes my php skills really suck – Thom Knepper Oct 11 '14 at 11:55
  • review my answer, try to make the edits accordingly and see if it works... additionally, post your edits in your original question, not as an answer; try to also make them shorter and remain relevant and specific "doesn't work" is not an error message ;) – benomatis Oct 11 '14 at 11:59
  • OKe thanks for the tip its my firt post on this website so i dont know how all of this work. I have edit the code incl. The mail thats need to be created and send to the user email. I have tried it but i don't recive a email myself only the opgeven@kidsnthingspernis.nl account gets the email. See my code for changes. Thanks for the help – Thom Knepper Oct 11 '14 at 12:13
  • sorry, can't help you if you're not following what i suggest... also be sure to provide your edit as an extra if it has significant changes pointing out what you did, otherwise my answer becomes obsolete, then we go on and on... and it will never end... – benomatis Oct 11 '14 at 12:21
  • I have chanced my code can you plaese check it out again. it still isn't sending emails to the $email i've tried with my own mail – Thom Knepper Oct 11 '14 at 15:01
  • you're editing your original question again, please don't do that! please understand that if you do that, my answer becomes totally useless, as I answered that, not the new one... moreover, you haven't implemented my suggestions correctly, so obviously it will not work... – benomatis Oct 11 '14 at 15:10
0

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();
}
?>