Form to email but also with 'thank you' email to user

Posted on 2006-05-15
Last Modified: 2008-03-06
Hey guys and gals

I have very little php experience so your help is greatly appreciated. I have a php script that sends an email to us when someone fills out our online form.

I would like to also send a 'Thank You' email to the user that just filled out the form. Can you add some code to what Ive got to accomplish this?

Thanks so much!

$EmailFrom = Trim(stripslashes($_POST['EmailFrom']));
$EmailTo = "";
$Subject = "Tryout Form Submission";
$name = Trim(stripslashes($_POST['name']));
$comments = Trim(stripslashes($_POST['comments']));

// validation
if (Trim($EmailFrom)=="") $validationOK=false;
if (!$validationOK) {
  print "<meta http-equiv=\"refresh\" content=\"0;URL=error.html\">";

// prepare email body text
$Body = "";
$Body .= "Name: ";
$Body .= $name;
$Body .= "\n";
$Body .= "Email: ";
$Body .= $EmailFrom;
$Body .= "\n";
$Body .= "Comments: ";
$Body .= $comments;
$Body .= "\n";

// send email
$success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");

// redirect to success page
if ($success){
  print "<meta http-equiv=\"refresh\" content=\"0;URL=ThankYou.html\">";
  print "<meta http-equiv=\"refresh\" content=\"0;URL=error.html\">";
Question by:IShiva
    LVL 5

    Accepted Solution

    Oke a little help on your hand :D

    First lets think out the logic.

    If someone loads the page, we want them to see the form. Then if the form was send but we missed values we show the form again with notifications on the fields not filled out or not passing the a validation of another kind. If all validations succeed we then process the from and send a "thank you" mail.

    if(isset($_POST['submit']) ){
              // Validate fields  do this with all the named fields in the form (especially the email addr)//
              if(isset($_POST['name']) ){
                     if(!empty($_POST['name']) ){
                           if(strlen($_POST['name']) >= 3){
                                   $error_msg .= "Name to short<br>\n";
                          $error = true;
                          $error_msg .= "No name given<br>\n";
                      $error = true;
                      $error_msg .= "No Name Set<br>\n";

               if($error == false){     // Validation using equal to is done by the dubble equal to sign " == "
                       $to = $_POST['user_email'];
                       $subject = "thank you";
                       // Setting email headers
                       $headers  = 'MIME-Version: 1.0' . "\r\n";
                       $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
                       $headers = "From:\r\n";
                       $headers .= "Reply-To:\r\n";
                       $headers .= "Return-Path:\r\n";
                       // Create a nice message "html"//
                       $body = '<table width="80%" cellspacing="0" cellpadding="0">\n
                                                 <td>Thank you for your application</td>\n
                                                 <td><div>Some nice message to '.$_POST['name'].' and somehow
                      if(mail($to, $subject, $body, $headers) ){

    // Assign all filled out fields if any.
    $name = (isset($_POST['name']) ) ? $_POST['name'] : "";
    $error = ($error == true) ? "Error occured" : "&nbsp;";

    // Then show the form.

    <form action="'.$_SERVER['PHP_SELF'].'" method="post">
          <td><input type="text" name="name" value="'.$name.'"></td>
          <td><input type="submit" name="submit"></td>

    Please look at it and question the code if you dont get it... ;) good luck!

    Author Comment

    Sorry for the delay in accepting dutchclan's answer. It worked perfectly!


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    This is a general how to create your own custom plugin system for your PHP application that you designed (or wish to extend a third party program to have plugin functionality that doesn't have it yet).  This is not how to make plugins for existing s…
    Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
    The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
    The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now