├── README.markdown ├── index.html └── thankyou └── index.php /README.markdown: -------------------------------------------------------------------------------- 1 | #PHP Email Form 2 | 3 | This was designed to be my replacement for the antiquated CGIemail. 4 | 5 | * `$subject` is used for the subject line of the email 6 | * `$my_email` is the email box it's delivered to 7 | * `$site_name` is used for the page title in the Thank You page 8 | * `$site_url` is used for the meta refresh link 9 | * `$continue` is used for the homepage link in the Thank You body copy. 10 | 11 | The form submits to `thankyou/` (thankyou/index.php) for a cleaner URL. The Thank You page template is at the bottom of the PHP page. 12 | 13 | [Demo](http://rwbaker.com/demos/php-email-form/) 14 | 15 | 95% of the PHP script is based on [Form To Email](http://formtoemail.com) by Charles Sweeney. His pro version adds a lot of extra features if you're looking for something beefier. -------------------------------------------------------------------------------- /index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | Contact Us 4 | 17 | 18 | 19 |
20 |
21 |
22 | Contact Form 23 |
    24 |
  1. 25 | 26 | 27 |
  2. 28 |
  3. 29 | 30 | 31 |
  4. 32 |
  5. 33 | 34 | 35 |
  6. 36 |
  7. 37 | 38 |
  8. 39 |
40 |
41 |
42 |
43 | 44 | -------------------------------------------------------------------------------- /thankyou/index.php: -------------------------------------------------------------------------------- 1 | 15 | 16 | 17 | 18 | 19 | 20 |
Name
Email address
Comments
 
21 | 22 | 23 | Step 2: Enter your email address. 24 | 25 | Enter the email address below to send the contents of the form to. You can enter more than one email address separated by commas, like so: $my_email = "info@example.com"; or $my_email = "bob@example.com,sales@example.co.uk,jane@example.com"; 26 | */ 27 | 28 | $my_email = "admin@rwbaker.com"; 29 | 30 | /* 31 | Optional. Enter a From: email address. By default, the email you get from the script will show the visitor's email address as the From: address. In most cases this is desirable. On the majority of setups this won't be a problem but a minority of hosts insist that the From: address must be from a domain on the server. 32 | */ 33 | $from_email = ""; 34 | 35 | /* Subject line */ 36 | $subject = "Contact from RWBAKER.COM"; 37 | 38 | /* Site URL */ 39 | $site_url = "http://www.rwbaker.com"; 40 | 41 | /* Site Name */ 42 | $site_name = "RWBAKER.COM"; 43 | 44 | /* 45 | Optional. Enter the continue link to offer the user after the form is sent. If you do not change this, your visitor will be given a continue link to your homepage. 46 | If you do change it, remove the "/" symbol below and replace with the name of the page to link to, eg: "mypage.htm" or "http://www.elsewhere.com/page.htm" 47 | */ 48 | $continue = "/"; 49 | 50 | /* 51 | Step 3: Save this file (/thankyou/index.php) and upload it together with your webpage containing the form to your webspace. IMPORTANT - The file name is case sensitive! You must save it exactly as it is named above! 52 | */ 53 | 54 | $errors = array(); 55 | 56 | // Remove $_COOKIE elements from $_REQUEST. 57 | 58 | if(count($_COOKIE)){foreach(array_keys($_COOKIE) as $value){unset($_REQUEST[$value]);}} 59 | 60 | // Validate email field. 61 | 62 | if(isset($_REQUEST['email']) && !empty($_REQUEST['email'])) 63 | { 64 | 65 | $_REQUEST['email'] = trim($_REQUEST['email']); 66 | 67 | if(substr_count($_REQUEST['email'],"@") != 1 || stristr($_REQUEST['email']," ")){$errors[] = "Email address is invalid";}else{$exploded_email = explode("@",$_REQUEST['email']);if(empty($exploded_email[0]) || strlen($exploded_email[0]) > 64 || empty($exploded_email[1])){$errors[] = "Email address is invalid";}else{if(substr_count($exploded_email[1],".") == 0){$errors[] = "Email address is invalid";}else{$exploded_domain = explode(".",$exploded_email[1]);if(in_array("",$exploded_domain)){$errors[] = "Email address is invalid";}else{foreach($exploded_domain as $value){if(strlen($value) > 63 || !preg_match('/^[a-z0-9-]+$/i',$value)){$errors[] = "Email address is invalid"; break;}}}}}} 68 | 69 | } 70 | 71 | // Check referrer is from same site. 72 | 73 | if(!(isset($_SERVER['HTTP_REFERER']) && !empty($_SERVER['HTTP_REFERER']) && stristr($_SERVER['HTTP_REFERER'],$_SERVER['HTTP_HOST']))){$errors[] = "You must enable referrer logging to use the form";} 74 | 75 | // Check for a blank form. 76 | 77 | function recursive_array_check_blank($element_value) 78 | { 79 | 80 | global $set; 81 | 82 | if(!is_array($element_value)){if(!empty($element_value)){$set = 1;}} 83 | else 84 | { 85 | 86 | foreach($element_value as $value){if($set){break;} recursive_array_check_blank($value);} 87 | 88 | } 89 | 90 | } 91 | 92 | recursive_array_check_blank($_REQUEST); 93 | 94 | if(!$set){$errors[] = "You cannot send a blank form";} 95 | 96 | unset($set); 97 | 98 | // Display any errors and exit if errors exist. 99 | 100 | if(count($errors)){foreach($errors as $value){print "$value
";} exit;} 101 | 102 | if(!defined("PHP_EOL")){define("PHP_EOL", strtoupper(substr(PHP_OS,0,3) == "WIN") ? "\r\n" : "\n");} 103 | 104 | // Build message. 105 | 106 | function build_message($request_input){if(!isset($message_output)){$message_output ="";}if(!is_array($request_input)){$message_output = $request_input;}else{foreach($request_input as $key => $value){if(!empty($value)){if(!is_numeric($key)){$message_output .= str_replace("_"," ",ucfirst($key)).": ".build_message($value).PHP_EOL.PHP_EOL;}else{$message_output .= build_message($value).", ";}}}}return rtrim($message_output,", ");} 107 | 108 | $message = build_message($_REQUEST); 109 | 110 | $message = $message . PHP_EOL.PHP_EOL."-- ".PHP_EOL."Thank you for using FormToEmail from http://FormToEmail.com"; 111 | 112 | $message = stripslashes($message); 113 | 114 | $subject = stripslashes($subject); 115 | 116 | if($from_email) 117 | { 118 | 119 | $headers = "From: " . $from_email; 120 | $headers .= PHP_EOL; 121 | $headers .= "Reply-To: " . $_REQUEST['email']; 122 | 123 | } 124 | else 125 | { 126 | 127 | $from_name = ""; 128 | 129 | if(isset($_REQUEST['name']) && !empty($_REQUEST['name'])){$from_name = stripslashes($_REQUEST['name']);} 130 | 131 | $headers = "From: {$from_name} <{$_REQUEST['email']}>"; 132 | 133 | } 134 | 135 | mail($my_email,$subject,$message,$headers); 136 | 137 | /* 138 | Thank you 139 | */ 140 | 141 | ?> 142 | 143 | 144 | 145 | 146 | 147 | 148 | Thank you for contacting <?php print $site_name; ?> 149 | 160 | 161 | 162 |
163 |

Thank you for contacting us, your message has been sent.

164 |

If you're not redirected in 5 seconds, then click here to go to the homepage.

165 |
166 | 167 | 168 | --------------------------------------------------------------------------------