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

Posted on 2006-05-15
Medium Priority
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 = "me@mydomain.com";
$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

Accepted Solution

dutchclan earned 2000 total points
ID: 16685492
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: 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
                                             <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

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


Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
This holiday season, we’re giving away the gift of knowledge—tech knowledge, that is. Keep reading to see what hacks, tips, and trends we have wrapped and waiting for you under the tree.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.
Suggested Courses
Course of the Month14 days, 5 hours left to enroll

807 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