// // // // ... // // // // // // // // In mailform.php: // include( 'class.mailform.inc' ); // $mf = new MailForm(); // $mf -> ReadForm(); // $mf -> Send( true ); // Or better, create a script for your particular mail form and leave AT LEAST // your email address off the form. // In mymailform.php: // include( 'class.mailform.inc' ); // $mf = new MailForm( 'yourname@here.com', 'From My Mail Form' ); // $mf -> ReadForm(); // $mf -> Send( true ); // Changes: // March 10, 2006 - Add retain_args tag. List the keys of any values that you // want to send to your ok_url or bad_url. You can reference them as // $_GET['email'] or wherever the value key is. // Thanks for the suggestion, Kjell Jarmlinger! class MailForm { // These are the input tag variable names used in the form. Change them if you must. var $valueVar = 'value'; var $descVar = 'desc'; var $addrVar = 'to'; var $subjectVar = 'subject'; var $statsVar = 'stats'; var $okVar = 'ok_url'; var $badVar = 'bad_url'; var $retainArgsVar = 'retain_args'; // Consider all the rest of the variables private to the class, OK? var $formMethod = 'POST'; // Can be 'GET', but 'POST' is much safer. var $subject = 'From the Data Helper Mail Form'; // Can be overridden below. var $stats; // Want to add sender's IP address and domain to the email? var $okUrl; // Where do we go on success? var $badUrl; // Where do we go on failure? var $retainArgs; // Which input arguments do we want to send to the after-email redirects? var $addr; // Your email address. var $value; // Array containing the things your customer typed on the form. var $desc; // Array of descriptions for each item in $value. var $args; // The build-up argument string for the ok or bad url redirect. // Constructor. function MailForm( $addr = false, $subject = false, $stats = false ) { // Save your email address, the message subject, and the flag that requests // the IP address and domain to be quietly added to the email. $this -> addr = $addr; $this -> stats = $stats; // Only override the subject if we have one on the constructor call. Otherwise, // use the default. We NEED some subject. if( $subject ) $this -> subject = $subject; // This isn't necessary, but I like to be explicit. $this -> okUrl = false; $this -> badUrl = false; $this -> value = false; $this -> desc = false; $this -> okUrl = false; $this -> badUrl = false; $this -> retainArgs = false; $this -> args = ''; } // Scrape all the data from the form. function ReadForm() { $varsName = 'HTTP_' . $this -> formMethod . '_VARS'; global $$varsName; $vars = & $$varsName; // Only store values that we don't already have. if( ! $this -> addr ) $this -> addr = $vars[ $this -> addrVar ]; if( ! $this -> subject ) $this -> subject = $vars[ $this -> subjectVar ]; if( ! $this -> okUrl ) $this -> okUrl = $vars[ $this -> okVar ]; if( ! $this -> badUrl ) $this -> badUrl = $vars[ $this -> badVar ]; if( ! $this -> stats ) $this -> stats = isset( $vars[ $this -> statsVar ] ); if( ! $this -> value ) $this -> value = $vars[ $this -> valueVar ]; if( ! $this -> desc ) $this -> desc = $vars[ $this -> descVar ]; if( ! $this -> retainArgs ) $this -> retainArgs = $vars[ $this -> retainArgsVar ]; } // Send yourself an email. If $redirect is true (default), a page will be sent // to the browser that sends the visitor to the OK or BAD page. If you call // Send( false ), you will have to determine what to do next. function Send( $redirect = true ) { define( NL, "\n" ); global $HTTP_SERVER_VARS; // Prepare the retainArgs variable. Give it surrounding commas to make the strstr work. if( ! $this -> retainArgs ) $this -> retainArgs = ''; $this -> retainArgs = str_replace( array( ' ', ), array( '', ), $this -> retainArgs ); $this -> retainArgs = "," . $this -> retainArgs . ","; if( strstr( $this -> retainArgs, 'subject' )) { $this -> args = "subject=" . urlencode( $this -> subject ); } // Set up the email headers. $headers = 'Date: ' . date( 'r' ) . NL; $headers .= 'From: Mail Form <' . $this -> addr . '>' . NL; // Set up the email. $msg = ''; $subjectLine = ''; if( $this -> subject ) $subjectLine = $this -> subject . NL . NL; // For each of the boxes that the visitor filled in, put the description // of the box and the visitor's text into the message. foreach( $this -> value as $key => $value ) { $value = str_replace( "\r", ' ', trim( $value )); if( strlen( $value ) > 0 ) { // The description is optional. if( isset( $this -> desc[$key] )) $msg .= $this -> desc[$key] . ':' . NL; $msg .= $value . NL . NL; if( strstr( $this -> retainArgs, $key )) { if( strlen( $this -> args ) > 0 ) $this -> args .= "&"; $this -> args .= urlencode( $key ) . "=" . urlencode( $value ); } } } // Do you want to capture the visitor's IP address and domain? if( $this -> stats ) { $ip = $HTTP_SERVER_VARS['REMOTE_ADDR']; $sender = 'From: ' . gethostbyaddr( $ip ) . ' (' . $ip . ')' . NL; } $result = false; // If there's anything in the message, send it. if( (! empty( $msg )) && ( $this -> addr )) $result = mail ( $this -> addr, $this -> subject, $subjectLine . $msg . $sender, $headers ); // If desired, send the visitor to the OK or BAD page. if( $redirect ) { $url = $result ? $this -> okUrl : $this -> badUrl; if( $url ) $this -> _redirect( $url ); } return $result; } // Set the hash of values. This can be used to add some fields to the email. function SetValues( $value ) { $this -> value = $value; } // Set the hash of descriptions of values. // This can be used to add some fields to the email. function SetDesc( $desc ) { $this -> desc = $desc; } // Set YOUR email address. This is the TO: address on the email. function SetAddr( $addr ) { $this -> addr = $addr; } // Set the email subject. function SetSubject( $subject ) { $this -> subject = $subject; } // Set the page that the visitor goes to on success. function SetOkUrl( $okUrl ) { $this -> okUrl = $okUrl; } // Set the page that the visitor goes to on failure. function SetBadUrl( $badUrl ) { $this -> badUrl = $badUrl; } // Turn on the switch so the visitor's IP address and domain are added to the email. function GetStats( $stats = true ) { $this -> stats = $stats; } // The form uses GET rather than POST. (Not recommended!) function MethodGET( $get = true ) { $this -> formMethod = $get ? 'GET' : 'POST'; } // This puts out a whole page. If you are redirecting, don't output anything else. function _redirect( $url ) { $args = $this -> args; ?> Form Sent ">

I am trying to send you back after your response form.

If you don't get back in a a few seconds, press the "Continue" button below.

" method="post">