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

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!

<?php
$EmailFrom = Trim(stripslashes($_POST['EmailFrom']));
$EmailTo = "me@mydomain.com";
$Subject = "Tryout Form Submission";
$name = Trim(stripslashes($_POST['name']));
$comments = Trim(stripslashes($_POST['comments']));

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

// 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\">";
}
else{
  print "<meta http-equiv=\"refresh\" content=\"0;URL=error.html\">";
}
?>
IShivaAsked:
Who is Participating?
 
dutchclanConnect With a Mentor Commented:
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=false;
                       }else{
                               $error=true;
                               $error_msg .= "Name to short<br>\n";
                       }
                 }else{
                      $error = true;
                      $error_msg .= "No name given<br>\n";
                 }
           }else{
                  $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: mymail@here.com\r\n";
                   $headers .= "Reply-To: somemail@here.com\r\n";
                   $headers .= "Return-Path: somemail@here.com\r\n";
                   
                   // Create a nice message "html"//
                   $body = '<table width="80%" cellspacing="0" cellpadding="0">\n
                                      <tr>\n
                                             <td>Thank you for your application</td>\n
                                      </tr>\n
                                      <tr>\n
                                             <td><div>Some nice message to '.$_POST['name'].' and somehow
                                                             Personalized</div></td>\n
                                       </tr>\n
                                   </table>\n';
                  if(mail($to, $subject, $body, $headers) ){
                              header('location:succes.html');
                  }
            }
       
}

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

// Then show the form.
print_r('

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

Please look at it and question the code if you dont get it... ;) good luck!
0
 
IShivaAuthor Commented:
Sorry for the delay in accepting dutchclan's answer. It worked perfectly!

IShiva
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.