http://pastebin.org/249014
process_form sends the email. The redirect is easily findable.
Thanks.
<Edit: Added the code here. External references tend to be unreliable. -- Atli>
Expand|Select|Wrap|Line Numbers
- <?php
- //start session
- session_start();
- // prints form
- function print_form(){
- ?>
- <form method="post" action="<?php echo $_SERVER[’PHP_SELF’];?>" id="uploadform" enctype="multipart/form-data">
- <p><label for="namefrom">Name <span class="required">*</span></label>
- <input name="namefrom" id="namefrom" type="text" class="field" value="<?= $_SESSION['myForm']['namefrom']; ?>" tabindex="1"/></p>
- <p><label for="emailfrom">Email <span class="required">*</span></label>
- <input name="emailfrom" id="emailfrom" type="text" class="field" value="<?= $_SESSION['myForm']['emailfrom']; ?>" tabindex="2"/></p>
- <p><label for="address">Address</label>
- <input name="address" id="address" type="text" class="field" value="<?= $_SESSION['myForm']['address']; ?>" tabindex="3"/><br/>
- <input name="address2" id="address2" type="text" class="field" value="<?= $_SESSION['myForm']['address2']; ?>" tabindex="4"/></p>
- <p><label for="phone">City</label>
- <input name="city" id="city" type="text" class="field" value="<?= $_SESSION['myForm']['city']; ?>" tabindex="5"/></p>
- <p><label for="state">State</label>
- <select id="state" name="state" class="field" value="<?= $_SESSION['myForm']['state']; ?>" tabindex="6">
- <option value="none">Please Select State</option><br />
- <option value="AL">Alabama</option><br />
- <option value="AK">Alaska</option><br />
- <option value="AZ">Arizona</option><br />
- <option value="AR">Arkansas</option><br />
- <option value="CA">California</option><br />
- <option value="CO">Colorado</option><br />
- <option value="CT">Connecticut</option><br />
- <option value="DE">Delaware</option><br />
- <option value="DC">Dist of Columbia</option><br />
- <option value="FL">Florida</option><br />
- <option value="GA">Georgia</option><br />
- <option value="HI">Hawaii</option><br />
- <option value="ID">Idaho</option><br />
- <option value="IL">Illinois</option><br />
- <option value="IN">Indiana</option><br />
- <option value="IA">Iowa</option><br />
- <option value="KS">Kansas</option><br />
- <option value="KY">Kentucky</option><br />
- <option value="LA">Louisiana</option><br />
- <option value="ME">Maine</option><br />
- <option value="MD">Maryland</option><br />
- <option value="MA">Massachusetts</option><br />
- <option value="MI">Michigan</option><br />
- <option value="MN">Minnesota</option><br />
- <option value="MS">Mississippi</option><br />
- <option value="MO">Missouri</option><br />
- <option value="MT">Montana</option><br />
- <option value="NE">Nebraska</option><br />
- <option value="NV">Nevada</option><br />
- <option value="NH">New Hampshire</option><br />
- <option value="NJ">New Jersey</option><br />
- <option value="NM">New Mexico</option><br />
- <option value="NY">New York</option><br />
- <option value="NC">North Carolina</option><br />
- <option value="ND">North Dakota</option><br />
- <option value="OH">Ohio</option><br />
- <option value="OK">Oklahoma</option><br />
- <option value="OR">Oregon</option><br />
- <option value="PA">Pennsylvania</option><br />
- <option value="RI">Rhode Island</option><br />
- <option value="SC">South Carolina</option><br />
- <option value="SD">South Dakota</option><br />
- <option value="TN">Tennessee</option><br />
- <option value="TX">Texas</option><br />
- <option value="UT">Utah</option><br />
- <option value="VT">Vermont</option><br />
- <option value="VA">Virginia</option><br />
- <option value="WA">Washington</option><br />
- <option value="WV">West Virginia</option><br />
- <option value="WI">Wisconsin</option><br />
- <option value="WY">Wyoming</option><br />
- </select>
- </p>
- <p><label for="phone">Zip</label>
- <input name="zip" id="zip" type="text" class="field" value="<?= $_SESSION['myForm']['zip']; ?>" tabindex="7"/></p>
- <p><label for="phone">Phone</label>
- <input name="phone" id="phone" type="text" class="field" value="<?= $_SESSION['myForm']['phone']; ?>" tabindex="8"/></p>
- <p><label for="comments">General Description</label>
- <textarea name="comments" id="comments" rows="7" cols="10" class="field" tabindex="9"><?= $_SESSION['myForm']['comments']; ?></textarea></p>
- <span class="copysmhead">Attach your own specs here<br /></span>
- <p class="copy" style="font-weight: normal;">(1 file only, max file size 10mb. Allowed file formats are .doc .pdf .xls .docx .txt .rtf .wpd .wps)<br/>Thank You!</p>
- <input name="attachment" id="attachment" type="file" tabindex="10">
- <p><input type="submit" name="submit" id="submit" value="Submit" tabindex="11"/></p>
- <p><input type="hidden" name="submitted" value="true" /></p>
- </form>
- <?php
- }
- // enquiry form validation
- function process_form() {
- // Read POST request params into global vars
- // FILL IN YOUR EMAIL
- $to = "sspock@andrew.cmu.edu";
- $subject = "[ KP ] " . "Quote Request ::" . " " . $_POST['emailfrom'];
- $namefrom = trim($_POST['namefrom']);
- $address = trim($_POST['address']);
- $address2 = trim($_POST['address2']);
- $phone = trim($_POST['phone']);
- $emailfrom = trim($_POST['emailfrom']);
- $comments = trim($_POST['comments']);
- $state = trim($_POST['state']);
- $city = trim($_POST['city']);
- $zip = trim($_POST['zip']);
- // Headers
- $headers = 'From: webmaster@knepperpress.com' . "\r\n" .
- 'Reply-To:' . " " . $_POST['emailfrom'];
- // Allowed file types. add file extensions WITHOUT the dot.
- $allowtypes=array("doc", "pdf", "xls", "txt", "docx", "rtf", "wpd", "wps");
- // Require a file to be attached: false = Do not allow attachments true = allow only 1 file to be attached
- $requirefile="false";
- // Maximum file size for attachments in KB NOT Bytes for simplicity. MAKE SURE your php.ini can handel it,
- // post_max_size, upload_max_filesize, file_uploads, max_execution_time!
- // 2048kb = 2MB, 1024kb = 1MB, 512kb = 1/2MB etc..
- $max_file_size="10240";
- // Thank you message
- $thanksmessage='Your email has been sent, we will respond shortly.<p> </p>If you have more than one document to attach, submit the first document and start again.';
- $errors = array(); //Initialize error array
- //checks for a name
- if (empty($_POST['namefrom']) ) {
- $errors[]='You forgot to enter your name';
- }
- //checks for an email
- if (empty($_POST['emailfrom']) ) {
- $errors[]='You forgot to enter your email';
- } else {
- if (!eregi ('^[[:alnum:]][a-z0-9_\.\-]*@[a-z0-9\.\-]+\.[a-z]{2,4}$', stripslashes(trim($_POST['emailfrom'])))) {
- $errors[]='Please enter a valid email address';
- } // if eregi
- } // if empty email
- // checks for required file
- // http://amiworks.co.in/talk/handling-file-uploads-in-php/
- if($requirefile=="true") {
- if($_FILES['attachment']['error']==4) {
- $errors[]='You forgot to attach a file';
- }
- }
- //checks attachment file
- // checks that we have a file
- if((!empty($_FILES["attachment"])) && ($_FILES['attachment']['error'] == 0)) {
- // basename -- Returns filename component of path
- $filename = basename($_FILES['attachment']['name']);
- $ext = substr($filename, strrpos($filename, '.') + 1);
- $filesize=$_FILES['attachment']['size'];
- $max_bytes=$max_file_size*1024;
- //Check if the file type uploaded is a valid file type.
- if (!in_array($ext, $allowtypes)) {
- $errors[]="Invalid extension for your file: <strong>".$filename."</strong>";
- // check the size of each file
- } elseif($filesize > $max_bytes) {
- $errors[]= "Your file: <strong>".$filename."</strong> is to big. Max file size is ".$max_file_size."kb.";
- }
- } // if !empty FILES
- if (empty($errors)) { //If everything is OK
- echo'<META HTTP-EQUIV="refresh" content="0;URL=thank_you.htm">';
- function process_form(); // processes form.
- // send an email
- // Obtain file upload vars
- $fileatt = $_FILES['attachment']['tmp_name'];
- $fileatt_type = $_FILES['attachment']['type'];
- $fileatt_name = $_FILES['attachment']['name'];
- // create a boundary string. It must be unique
- $semi_rand = md5(time());
- $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
- // Add the headers for a file attachment
- $headers .= "\nMIME-Version: 1.0\n" .
- "Content-Type: multipart/mixed;\n" .
- " boundary=\"{$mime_boundary}\"";
- // Add a multipart boundary above the plain message
- $message ="This is a multi-part message in MIME format.\n\n";
- $message.="--{$mime_boundary}\n";
- $message.="Content-Type: text/plain; charset=\"iso-8859-1\"\n";
- $message.="Content-Transfer-Encoding: 7bit\n\n";
- $message.="Name: ".$namefrom."\n";
- $message.="Email: ".$emailfrom."\n";
- $message.="Address:\n\t" . $address . "\n\t" . $address2 . "\n\t" . $city . ", " . $state . " " . $zip . "\n";
- $message.="Phone: ".$phone."\n";
- $message.="General Description: ".$comments."\n\n";
- $message.="\n";
- $message.="[ Email Generated from KnepperPress.com ]";
- $message.="\n\n\n";
- if (is_uploaded_file($fileatt)) {
- // Read the file to be attached ('rb' = read binary)
- $file = fopen($fileatt,'rb');
- $data = fread($file,filesize($fileatt));
- fclose($file);
- // Base64 encode the file data
- $data = chunk_split(base64_encode($data));
- // Add file attachment to the message
- $message .= "--{$mime_boundary}\n" .
- "Content-Type: {$fileatt_type};\n" .
- " name=\"{$fileatt_name}\"\n" .
- //"Content-Disposition: attachment;\n" .
- //" filename=\"{$fileatt_name}\"\n" .
- "Content-Transfer-Encoding: base64\n\n" .
- $data . "\n\n" .
- "--{$mime_boundary}--\n";
- }
- // Send the completed message
- $envs = array("HTTP_USER_AGENT", "REMOTE_ADDR", "REMOTE_HOST");
- foreach ($envs as $env)
- $message .= "$env: $_SERVER[$env]\n";
- if(!mail($to,$subject,$message,$headers)) {
- exit("Mail could not be sent. Sorry! An error has occurred, please report this to the website administrator.\n");
- } else {
- // echo '<div id="formfeedback"><h3>Thank You!</h3><p>'. $thanksmessage .'</p></div>';
- // unset($_SESSION['myForm']);
- // print_form();
- } // end of if !mail
- } else { //report the errors
- echo '<div id="formfeedback"><h3>Error!</h3><p>The following error(s) has occurred:<br />';
- foreach ($errors as $msg) { //prints each error
- echo " - $msg<br />\n";
- } // end of foreach
- echo '</p><p>Please try again</p></div>';
- print_form();
- } //end of if(empty($errors))
- } // end of process_form()
- ?>