?
Solved

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

Posted on 2006-05-15
3
Medium Priority
?
177 Views
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!

<?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\">";
}
?>
0
Comment
Question by:IShiva
2 Comments
 
LVL 5

Accepted Solution

by:
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=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
 

Author Comment

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

IShiva
0

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