home *** CD-ROM | disk | FTP | other *** search
/ H4CK3R 4 / hacker04 / 04_HACK04.ISO / src / PHP / form2mail.php3.txt < prev    next >
Encoding:
Text File  |  2002-05-06  |  3.5 KB  |  128 lines

  1. Form To Mail 
  2.  
  3. Easy to use script to send mail from a form. Allows use of an e-mail template file, so you can customize the appearance and wording of the e-mail. HTML e-mail option. 
  4.  
  5.  
  6. <?PHP
  7.  
  8.     // Form To Mail
  9.     // This program will process information send 
  10.     // to it via POST from a form on a web site
  11.     // The form should be set up like this:
  12.     // <FORM METHOD="post" ACTION="form_to_mail.php3">
  13.     // <INPUT TYPE="hidden" NAME="to" VALUE="your@email.com">
  14.     // <INPUT TYPE="hidden" NAME="subject" VALUE="The subject can be whatever you want">
  15.     // <INPUT TYPE="hidden" NAME="template" VALUE="form.frm"> (optional)
  16.     // <INPUT TYPE="hidden" NAME="required" VALUE="name,email,comments"> (optional)
  17.     // <INPUT TYPE="hidden" NAME="redirect" VALUE="thankyou.html"> (optional
  18.     // <INPUT TYPE="hidden" NAME="html_mail" VALUE="1"> values are 0 or 1.
  19.  
  20.     // if the html_mail is set to 1, then your template can be an HTML page.
  21.  
  22.     // when using a e-mail template, simply set it up like this:
  23.     // 
  24.     // This is a cutomized e-mail.  Sender's Name: <name>.  
  25.     // The Sender's E-mail address is <email>
  26.     // etc.
  27.     //
  28.     //  Where the email template is a text file with the field names contained in <>.
  29.  
  30.  
  31.     // set default send to address
  32.     if (empty($to))
  33.         $to = "your@email.com";
  34.         
  35.     // set default sender's e-mail address
  36.     if (empty($email))
  37.         $from = "FROM: your@email.com";
  38.     else
  39.         $from = "FROM: $email";
  40.  
  41.     if ($html_mail)
  42.         $from .= "\nContent-type: text/html";
  43.  
  44.  
  45.     function PrintErrorPage($error_msg) {
  46.         // change the HTML below to customize your default Error Page
  47.         ?>
  48.         <HTML>
  49.         <HEAD><TITLE>Error</TITLE></HEAD>
  50.         <BODY>
  51.         <DIV ALIGN="center">
  52.         <TABLE CELLPADDING=8><TR>
  53.             <TD BGCOLOR="#8A8A8A"><FONT SIZE="+2">Error Processing Form</FONT></TD>
  54.         </TR><TR>
  55.             <TD BGCOLOR="#C0C0C0">
  56.             <B>Please Use The Back Button On Your Browser And Correct These Mistakes</B>
  57.             <BR><BR>
  58.             <?PHP print $error_msg; ?>
  59.             </TD>
  60.         </TR></TABLE>        
  61.         </DIV>        
  62.         </BODY></HTML>
  63.         <?PHP
  64.     }
  65.     
  66.     function PrintSuccessPage() {
  67.         // change the HTML below to customize your default Success Page
  68.         ?>
  69.         <HTML>
  70.         <HEAD><TITLE>Success</TITLE></HEAD>
  71.         <BODY>
  72.         <DIV ALIGN="center">
  73.         <FONT SIZE="+1"><B>Thank You.  Form Has Been Sent</B></FONT>
  74.         </DIV>        
  75.         </BODY></HTML>
  76.         <?PHP    
  77.     }
  78.  
  79.         
  80.     // load required variables into $required_array
  81.     $required = ereg_replace(" ", "", $required);
  82.     $required_array = array();
  83.     if ($required)
  84.         $required_array = split(",", $required);
  85.     
  86.     // check for required fields
  87.     $loop = count($required_array);
  88.     $error_msg = "";
  89.     for ($i = 0; $i < $loop; $i++) {
  90.         if (!$HTTP_POST_VARS[$required_array[$i]])
  91.             $error_msg .= "Required Field, ".$required_array[$i].", was left blank<BR>\n";
  92.     }
  93.     
  94.     if (!$error_msg) {
  95.         // send the mail and redirect to thank you page
  96.         $message = "";
  97.         if (empty($template) || !file_exists($template)) {
  98.             while ( list( $key, $val ) = each( $HTTP_POST_VARS ) ) {
  99.                 $message .= $key.": ".$val."\n\n";
  100.             }                        
  101.         }
  102.         else {
  103.             $lines = array();
  104.             $lines = file($template);
  105.             $loop = count($lines);
  106.             for ($i = 0; $i < $loop; $i++) {
  107.                 $message .= $lines[$i];
  108.             }
  109.             
  110.             while ( list( $key, $val ) = each( $HTTP_POST_VARS ) ) {
  111.                $message = ereg_replace("<".$key.">", $val, $message);
  112.             }            
  113.             // at this point, $message is ready to go.
  114.         }
  115.         mail($to, $subject, $message, $from);
  116.         if ($redirect)
  117.             HEADER("Location: $redirect");
  118.         else
  119.             PrintSuccessPage();
  120.     }
  121.     else {
  122.     // send error page
  123.         PrintErrorPage($error_msg);
  124.     }
  125.  
  126. ?>
  127.  
  128.