Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- //////////////////////////////////////////////////////////////////////
- // MAILER CONTACT FORM PAGE
- //////////////////////////////////////////////////////////////////////
- // Prepare data fields
- $from = $name = $subject = $msg = null;
- // Error set 440 : user screw up
- define("ERROR_440_BASE_CODE", 440); // base code = no error
- define("ERROR_440_ADDRESS_MISSING", 1); // bit 0 = sender address missing
- define("ERROR_440_NAME_MISSING", 2); // bit 1 = sender name missing
- define("ERROR_440_SUBJECT_MISSING", 4); // bit 2 = e-mail subject missing
- define("ERROR_440_BODY_MISSING", 8); // bit 3 = e-mail body missing
- define("ERROR_440_ADDRESS_INCORRECT", 16); // bit 4 = e-mail address is incorrect format
- // Error set 550 : server screw up
- define("ERROR_550_BASE_CODE", 550); // base code = no error
- define("ERROR_550_SENDING_FAILURE", 1); // bit 0 = e-mail sending failed
- define("ERROR_550_MEMORY_FAILURE", 2); // bit 1 = memory failed
- define("ERROR_550_SPAM_DETECTED", 4); // bit 2 = spam was detected, e-mail was dropped
- // Explanation: I decided to add spam detection as a server error for a simple reason:
- // it may be false positive, so the user may know that something went wrong.
- // Spambots won't actually perceive the returned page with additional informations,
- // so they won't ever notice their spam went dropped.
- // Set basic error codes -> no bits = no error
- $user_error = ERROR_440_BASE_CODE;
- $server_error = ERROR_550_BASE_CODE;
- // Testing errors preparation
- $test_error_440_address_missing = false;
- $test_error_440_name_missing = false;
- $test_error_440_subject_missing = false;
- $test_error_440_body_missing = false;
- $test_error_440_address_incorrect = false;
- //----------------------------------------
- $test_error_550_sending_failure = false;
- $test_error_550_memory_failure = false;
- $test_error_550_spam_detected = false;
- //----------------------------------------
- $test_error_no_error_success = false;
- // Get the informations: testing errors (method get)
- if(isset($_GET['test_address_missing'])) {
- $test_error_440_address_missing = true;
- }
- if(isset($_GET['test_name_missing'])) {
- $test_error_440_name_missing = true;
- }
- if(isset($_GET['test_subject_missing'])) {
- $test_error_440_subject_missing = true;
- }
- if(isset($_GET['test_body_missing'])) {
- $test_error_440_body_missing = true;
- }
- if(isset($_GET['test_address_incorrect'])) {
- $test_error_440_address_incorrect = true;
- }
- if(isset($_GET['test_sending_failure'])) {
- $test_error_550_sending_failure = true;
- }
- if(isset($_GET['test_memory_failure'])) {
- $test_error_550_memory_failure = true;
- }
- if(isset($_GET['test_spam_detected'])) {
- $test_error_550_spam_detected = true;
- }
- if(isset($_GET['test_success'])) {
- $test_error_no_error_success = true;
- }
- // Set general test
- $test_general = $test_error_440_address_missing || $test_error_440_name_missing || $test_error_440_subject_missing || $test_error_440_body_missing || $test_error_440_address_incorrect || $test_error_550_sending_failure || $test_error_550_memory_failure || $test_error_550_spam_detected || $test_error_no_error_success;
- // Get the informations: data fields (method post)
- if(isset($_POST['from'])) {
- $from = $_POST['from'];
- }
- if(isset($_POST['name'])) {
- $name = $_POST['name'];
- }
- if(isset($_POST['subject'])) {
- $subject = $_POST['subject'];
- }
- if(isset($_POST['msg'])) {
- $msg = $_POST['msg'];
- }
- // Set default values
- $default = is_null($from) && is_null($name) && is_null($subject) && is_null($msg);
- if(!$default) {
- // Errors: user screw up (440 set)
- if(empty($from)) {
- $user_error += ERROR_440_ADDRESS_MISSING;
- }
- if(empty($name)) {
- $user_error += ERROR_440_NAME_MISSING;
- }
- if(empty($subject)) {
- $user_error += ERROR_440_SUBJECT_MISSING;
- }
- if(empty($msg)) {
- $user_error += ERROR_440_BODY_MISSING;
- }
- if(!empty($from)) {
- preg_match("/^([a-zA-Z0-9\.\-]*)\@([a-zA-Z0-9\-]*)\.([a-zA-Z0-9]*)$/", $from, $from_array);
- if(is_null($from_array)) {
- $server_error += ERROR_550_MEMORY_FAILURE;
- } elseif(empty($from_array)) {
- $user_error += ERROR_440_ADDRESS_INCORRECT;
- }
- }
- // Check user or server error
- if($user_error != ERROR_440_BASE_CODE || $server_error != ERROR_550_BASE_CODE) {
- goto error;
- }
- // Setup spam prevention
- $regex = "/(from\:|to\:|bcc\:|cc\:|content\-type\:|mime\-version\:|subject\:|x\-mailer\:|reply\-to\:|\%0a|\%0b)/i";
- if(preg_match($regex, $from) || preg_match($regex, $name) || preg_match($regex, $subject) || preg_match($regex, $msg)) {
- $server_error += ERROR_550_SPAM_DETECTED; // if something in all four fields matches some of the headers,
- goto error; // it detects spam and drops the e-mail. Spambots won't ever notice.
- }
- // Prepare mail send
- $send_to = 'Marek Poláček <marpolda@gmail.com>'; // e-mail address of receipt - change to yours!
- $send_from = filter_var($from, FILTER_SANITIZE_EMAIL); // Remove all
- $full_subject = '[TESTING FORM] ' . trim($subject); // Compose your own subject format to help YOU organise
- // messages from your websites.
- $final_subject = str_replace(array("\r","\n"),array(""," "),$full_subject);
- $final_message = wordwrap(trim($msg), 70, '\r\n');
- $final_name = trim($name);
- $success = mail($send_to, $final_subject, $final_message, "From: $final_name <$send_from>");
- if(!$success) {
- $server_error += ERROR_550_SENDING_FAILURE; // If the e-mail failed to send, show the message.
- // This may be because of SMTP server missing or malfunctioning,
- // or function mail() is forbidden to use at the server.
- }
- }
- // All errors passed
- error:
- $dir_errors = '';
- $dir_success = '';
- // Get error bits
- $user_error_bits = $user_error - ERROR_440_BASE_CODE;
- $server_error_bits = $server_error - ERROR_550_BASE_CODE;
- $e440_address_missing = $user_error_bits & ERROR_440_ADDRESS_MISSING;
- $e440_name_missing = $user_error_bits & ERROR_440_NAME_MISSING;
- $e440_subject_missing = $user_error_bits & ERROR_440_SUBJECT_MISSING;
- $e440_body_missing = $user_error_bits & ERROR_440_BODY_MISSING;
- $e440_address_incorrect = $user_error_bits & ERROR_440_ADDRESS_INCORRECT;
- $e550_sending_failure = $server_error_bits & ERROR_550_SENDING_FAILURE;
- $e550_memory_failure = $server_error_bits & ERROR_550_MEMORY_FAILURE;
- $e550_spam_detected = $server_error_bits & ERROR_550_SPAM_DETECTED;
- $successfully_sent = ($user_error == ERROR_440_BASE_CODE) && ($server_error == ERROR_550_BASE_CODE) && !$default;
- if($e440_address_missing || $test_error_440_address_missing) {
- $dir_errors .= '<p>Please enter your <srong>e-mail address</strong> ';
- $dir_errors .= 'like that: \'<strong>john.cena@gmail.com</strong>\'. ';
- $dir_errors .= 'You can\'t send an e-mail <strong>without sender address</strong>.</p>';
- }
- if($e440_name_missing || $test_error_440_name_missing) {
- $dir_errors .= '<p>Please enter your <strong>name</strong> (or nickname at least). ';
- $dir_errors .= 'You have to <strong>identify</strong> somewhat.</p>';
- }
- if($e440_subject_missing || $test_error_440_subject_missing) {
- $dir_errors .= '<p>Please enter <strong>message subject</strong>. ';
- $dir_errors .= 'It helps to <strong>distinguish</strong> single mails.</p>';
- }
- if($e440_body_missing || $test_error_440_body_missing) {
- $dir_errors .= '<p>Please enter at least a single letter to <strong>message body</strong>. ';
- $dir_errors .= 'However, something <strong>meaningful</strong> would be better, ';
- $dir_errors .= 'to make sure the message won\'t end up <strong>in trash</strong>.</p>';
- }
- if($e440_address_incorrect || $test_error_440_address_incorrect) {
- $dir_errors .= '<p>Entered <strong>e-mail address</strong> is ';
- $dir_errors .= '<strong>incorrect format</strong>. ';
- $dir_errors .= 'E-mail address <strong>has to be</strong> in format ';
- $dir_errors .= '\'<strong>john.cena@gmail.com</strong>\'.</p>';
- }
- if($e550_sending_failure || $test_error_550_sending_failure) {
- $dir_errors .= '<p>Message failed to send. Please try again later.</p>';
- }
- if($e550_memory_failure || $test_error_550_memory_failure) {
- $dir_errors .= '<p>Memory failure has occured. Please try again later.</p>';
- }
- if($e550_spam_detected || $test_error_550_spam_detected) {
- $dir_errors .= '<p>Your message was detected by a spam filter. If you think ';
- $dir_errors .= 'it\'s a mistake, please write an e-mail to websites admin.</p>';
- }
- if($successfully_sent || $test_error_no_error_success) {
- $dir_success .= '<p>Your message was successfully sent.</p>';
- }
- ?><!DOCTYPE HTML>
- <html type="text/html" lang="en-US">
- <head>
- <meta charset="utf-8" />
- <title>Contact form</title>
- <style type="text/css">
- body {
- font-family: "Arial CE", Arial, Helvetica, sans-serif;
- font-size: 12pt;
- }
- p {
- margin: 0px;
- margin-bottom: 0.3em;
- margin-top: 0.3em;
- }
- table {
- border: 0px;
- margin: 0px;
- margin-top: 10px;
- margin-bottom: 10px;
- padding: 0px;
- table-collapse: separated;
- table-align: center;
- }
- .info, input, textarea {
- border: 1px solid;
- border-radius: 5px;
- padding: 5px;
- }
- .info {
- font-size: 10pt;
- margin-top: 20px;
- margin-bottom: 20px;
- }
- input {
- font-size: 15pt;
- }
- label {
- font-size: 15pt;
- font-weight: bold;
- }
- .error {
- /* ERROR background */
- background-color: #ef9669;
- border-color: maroon;
- }
- .success {
- /* SUCCESS background */
- background-color: #69ef69;
- border-color: green;
- }
- input[type="text"], textarea {
- background-color: #fceded;
- border-color: gray;
- width: 600px;
- }
- input[type="submit"], input[type="reset"] {
- background-color: #fedcab;
- border-color: gray;
- }
- input:hover, textarea:hover {
- border-color: silver;
- }
- </style>
- </head>
- <body>
- <h1><center>Contact form</center></h1>
- <?php if(!empty($dir_errors)) {
- echo '<div class="info error"';
- if($test_general) {
- echo ' title="Testing dump"'; // when you enter ?test_* after the contact-form.php in your address bar,
- // it will perform a testing dump of the errors. Combine multiple errors
- // by adding next &test_* statements. That applies also to test_success
- // statement, which tests the success message.
- // This should be removed in final tweaks on real website, don't forget!
- }
- echo '>';
- echo $dir_errors;
- echo '</div>';
- }
- if(!empty($dir_success)) {
- echo '<div class="info success"';
- if($test_general) {
- echo ' title="Testing dump"';
- }
- echo '>';
- echo $dir_success;
- echo '</div>';
- } ?>
- <form method="post" action="<?php echo $_SERVER['PHP_SELF']; // points to self page ?>">
- <center><strong>Every single field is required!</strong></center>
- <label for="from"><table>
- <tr><td width="200">Sender e-mail address:</td>
- <td width="650"><input type="text" name="from"<?php if(!$default) {
- if(!empty($from) && !$successfully_sent) {
- echo " value=\"$from\"";
- }
- } ?> placeholder="john.cena@gmail.com" /></td></tr>
- </table></label>
- <label for="name"><table>
- <tr><td width="200">Name:</td>
- <td width="650"><input type="text" name="name"<?php if(!$default) {
- if(!empty($name) && !$successfully_sent) {
- echo " value=\"$name\"";
- }
- } ?> placeholder="John Cena" /></td></tr>
- </table></label>
- <label for="subject"><table>
- <tr><td width="200">Subject:</td>
- <td width="650"><input type="text" name="subject"<?php if(!$default) {
- if(!empty($subject) && !$successfully_sent) {
- echo " value=\"$subject\"";
- }
- } ?> placeholder="Important notice!" /></td></tr>
- </table></label>
- <label for="msg"><table>
- <tr><td width="200">Message body:</td>
- <td width="650"><textarea name="msg" cols="70" rows="20"><?php if(!$default) {
- if(!empty($msg) && !$successfully_sent) {
- echo "$msg";
- }
- } ?></textarea></td></tr>
- </table></label>
- <center><input type="submit" value="Submit" /> <input type="reset" value="Cancel" /></center>
- </form>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement