Advertisement
Guest User

Untitled

a guest
Sep 14th, 2017
493
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.13 KB | None | 0 0
  1. <?php
  2. //////////////////////////////////////////////////////////////////////
  3. // MAILER CONTACT FORM PAGE
  4. //////////////////////////////////////////////////////////////////////
  5.  
  6. // Prepare data fields
  7. $from = $name = $subject = $msg = null;
  8.  
  9. // Error set 440 : user screw up
  10. define("ERROR_440_BASE_CODE", 440); // base code = no error
  11. define("ERROR_440_ADDRESS_MISSING", 1); // bit 0 = sender address missing
  12. define("ERROR_440_NAME_MISSING", 2); // bit 1 = sender name missing
  13. define("ERROR_440_SUBJECT_MISSING", 4); // bit 2 = e-mail subject missing
  14. define("ERROR_440_BODY_MISSING", 8); // bit 3 = e-mail body missing
  15. define("ERROR_440_ADDRESS_INCORRECT", 16); // bit 4 = e-mail address is incorrect format
  16.  
  17. // Error set 550 : server screw up
  18. define("ERROR_550_BASE_CODE", 550); // base code = no error
  19. define("ERROR_550_SENDING_FAILURE", 1); // bit 0 = e-mail sending failed
  20. define("ERROR_550_MEMORY_FAILURE", 2); // bit 1 = memory failed
  21. define("ERROR_550_SPAM_DETECTED", 4); // bit 2 = spam was detected, e-mail was dropped
  22.  
  23. // Explanation: I decided to add spam detection as a server error for a simple reason:
  24. // it may be false positive, so the user may know that something went wrong.
  25. // Spambots won't actually perceive the returned page with additional informations,
  26. // so they won't ever notice their spam went dropped.
  27.  
  28. // Set basic error codes -> no bits = no error
  29. $user_error = ERROR_440_BASE_CODE;
  30. $server_error = ERROR_550_BASE_CODE;
  31.  
  32. // Testing errors preparation
  33. $test_error_440_address_missing = false;
  34. $test_error_440_name_missing = false;
  35. $test_error_440_subject_missing = false;
  36. $test_error_440_body_missing = false;
  37. $test_error_440_address_incorrect = false;
  38. //----------------------------------------
  39. $test_error_550_sending_failure = false;
  40. $test_error_550_memory_failure = false;
  41. $test_error_550_spam_detected = false;
  42. //----------------------------------------
  43. $test_error_no_error_success = false;
  44.  
  45. // Get the informations: testing errors (method get)
  46. if(isset($_GET['test_address_missing'])) {
  47. $test_error_440_address_missing = true;
  48. }
  49. if(isset($_GET['test_name_missing'])) {
  50. $test_error_440_name_missing = true;
  51. }
  52. if(isset($_GET['test_subject_missing'])) {
  53. $test_error_440_subject_missing = true;
  54. }
  55. if(isset($_GET['test_body_missing'])) {
  56. $test_error_440_body_missing = true;
  57. }
  58. if(isset($_GET['test_address_incorrect'])) {
  59. $test_error_440_address_incorrect = true;
  60. }
  61. if(isset($_GET['test_sending_failure'])) {
  62. $test_error_550_sending_failure = true;
  63. }
  64. if(isset($_GET['test_memory_failure'])) {
  65. $test_error_550_memory_failure = true;
  66. }
  67. if(isset($_GET['test_spam_detected'])) {
  68. $test_error_550_spam_detected = true;
  69. }
  70. if(isset($_GET['test_success'])) {
  71. $test_error_no_error_success = true;
  72. }
  73.  
  74. // Set general test
  75. $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;
  76.  
  77. // Get the informations: data fields (method post)
  78. if(isset($_POST['from'])) {
  79. $from = $_POST['from'];
  80. }
  81. if(isset($_POST['name'])) {
  82. $name = $_POST['name'];
  83. }
  84. if(isset($_POST['subject'])) {
  85. $subject = $_POST['subject'];
  86. }
  87. if(isset($_POST['msg'])) {
  88. $msg = $_POST['msg'];
  89. }
  90.  
  91. // Set default values
  92. $default = is_null($from) && is_null($name) && is_null($subject) && is_null($msg);
  93.  
  94. if(!$default) {
  95. // Errors: user screw up (440 set)
  96. if(empty($from)) {
  97. $user_error += ERROR_440_ADDRESS_MISSING;
  98. }
  99. if(empty($name)) {
  100. $user_error += ERROR_440_NAME_MISSING;
  101. }
  102. if(empty($subject)) {
  103. $user_error += ERROR_440_SUBJECT_MISSING;
  104. }
  105. if(empty($msg)) {
  106. $user_error += ERROR_440_BODY_MISSING;
  107. }
  108. if(!empty($from)) {
  109. preg_match("/^([a-zA-Z0-9\.\-]*)\@([a-zA-Z0-9\-]*)\.([a-zA-Z0-9]*)$/", $from, $from_array);
  110.  
  111. if(is_null($from_array)) {
  112. $server_error += ERROR_550_MEMORY_FAILURE;
  113. } elseif(empty($from_array)) {
  114. $user_error += ERROR_440_ADDRESS_INCORRECT;
  115. }
  116. }
  117.  
  118. // Check user or server error
  119. if($user_error != ERROR_440_BASE_CODE || $server_error != ERROR_550_BASE_CODE) {
  120. goto error;
  121. }
  122.  
  123. // Setup spam prevention
  124. $regex = "/(from\:|to\:|bcc\:|cc\:|content\-type\:|mime\-version\:|subject\:|x\-mailer\:|reply\-to\:|\%0a|\%0b)/i";
  125.  
  126. if(preg_match($regex, $from) || preg_match($regex, $name) || preg_match($regex, $subject) || preg_match($regex, $msg)) {
  127. $server_error += ERROR_550_SPAM_DETECTED; // if something in all four fields matches some of the headers,
  128. goto error; // it detects spam and drops the e-mail. Spambots won't ever notice.
  129. }
  130.  
  131. // Prepare mail send
  132. $send_to = 'Marek Poláček <marpolda@gmail.com>'; // e-mail address of receipt - change to yours!
  133. $send_from = filter_var($from, FILTER_SANITIZE_EMAIL); // Remove all
  134. $full_subject = '[TESTING FORM] ' . trim($subject); // Compose your own subject format to help YOU organise
  135. // messages from your websites.
  136. $final_subject = str_replace(array("\r","\n"),array(""," "),$full_subject);
  137. $final_message = wordwrap(trim($msg), 70, '\r\n');
  138. $final_name = trim($name);
  139.  
  140. $success = mail($send_to, $final_subject, $final_message, "From: $final_name <$send_from>");
  141.  
  142. if(!$success) {
  143. $server_error += ERROR_550_SENDING_FAILURE; // If the e-mail failed to send, show the message.
  144. // This may be because of SMTP server missing or malfunctioning,
  145. // or function mail() is forbidden to use at the server.
  146. }
  147. }
  148.  
  149. // All errors passed
  150. error:
  151. $dir_errors = '';
  152. $dir_success = '';
  153.  
  154. // Get error bits
  155. $user_error_bits = $user_error - ERROR_440_BASE_CODE;
  156. $server_error_bits = $server_error - ERROR_550_BASE_CODE;
  157.  
  158. $e440_address_missing = $user_error_bits & ERROR_440_ADDRESS_MISSING;
  159. $e440_name_missing = $user_error_bits & ERROR_440_NAME_MISSING;
  160. $e440_subject_missing = $user_error_bits & ERROR_440_SUBJECT_MISSING;
  161. $e440_body_missing = $user_error_bits & ERROR_440_BODY_MISSING;
  162. $e440_address_incorrect = $user_error_bits & ERROR_440_ADDRESS_INCORRECT;
  163.  
  164. $e550_sending_failure = $server_error_bits & ERROR_550_SENDING_FAILURE;
  165. $e550_memory_failure = $server_error_bits & ERROR_550_MEMORY_FAILURE;
  166. $e550_spam_detected = $server_error_bits & ERROR_550_SPAM_DETECTED;
  167.  
  168. $successfully_sent = ($user_error == ERROR_440_BASE_CODE) && ($server_error == ERROR_550_BASE_CODE) && !$default;
  169.  
  170. if($e440_address_missing || $test_error_440_address_missing) {
  171. $dir_errors .= '<p>Please enter your <srong>e-mail address</strong> ';
  172. $dir_errors .= 'like that: \'<strong>john.cena@gmail.com</strong>\'. ';
  173. $dir_errors .= 'You can\'t send an e-mail <strong>without sender address</strong>.</p>';
  174. }
  175. if($e440_name_missing || $test_error_440_name_missing) {
  176. $dir_errors .= '<p>Please enter your <strong>name</strong> (or nickname at least). ';
  177. $dir_errors .= 'You have to <strong>identify</strong> somewhat.</p>';
  178. }
  179. if($e440_subject_missing || $test_error_440_subject_missing) {
  180. $dir_errors .= '<p>Please enter <strong>message subject</strong>. ';
  181. $dir_errors .= 'It helps to <strong>distinguish</strong> single mails.</p>';
  182. }
  183. if($e440_body_missing || $test_error_440_body_missing) {
  184. $dir_errors .= '<p>Please enter at least a single letter to <strong>message body</strong>. ';
  185. $dir_errors .= 'However, something <strong>meaningful</strong> would be better, ';
  186. $dir_errors .= 'to make sure the message won\'t end up <strong>in trash</strong>.</p>';
  187. }
  188. if($e440_address_incorrect || $test_error_440_address_incorrect) {
  189. $dir_errors .= '<p>Entered <strong>e-mail address</strong> is ';
  190. $dir_errors .= '<strong>incorrect format</strong>. ';
  191. $dir_errors .= 'E-mail address <strong>has to be</strong> in format ';
  192. $dir_errors .= '\'<strong>john.cena@gmail.com</strong>\'.</p>';
  193. }
  194. if($e550_sending_failure || $test_error_550_sending_failure) {
  195. $dir_errors .= '<p>Message failed to send. Please try again later.</p>';
  196. }
  197. if($e550_memory_failure || $test_error_550_memory_failure) {
  198. $dir_errors .= '<p>Memory failure has occured. Please try again later.</p>';
  199. }
  200. if($e550_spam_detected || $test_error_550_spam_detected) {
  201. $dir_errors .= '<p>Your message was detected by a spam filter. If you think ';
  202. $dir_errors .= 'it\'s a mistake, please write an e-mail to websites admin.</p>';
  203. }
  204. if($successfully_sent || $test_error_no_error_success) {
  205. $dir_success .= '<p>Your message was successfully sent.</p>';
  206. }
  207. ?><!DOCTYPE HTML>
  208. <html type="text/html" lang="en-US">
  209. <head>
  210. <meta charset="utf-8" />
  211. <title>Contact form</title>
  212. <style type="text/css">
  213. body {
  214. font-family: "Arial CE", Arial, Helvetica, sans-serif;
  215. font-size: 12pt;
  216. }
  217.  
  218. p {
  219. margin: 0px;
  220. margin-bottom: 0.3em;
  221. margin-top: 0.3em;
  222. }
  223.  
  224. table {
  225. border: 0px;
  226. margin: 0px;
  227. margin-top: 10px;
  228. margin-bottom: 10px;
  229. padding: 0px;
  230. table-collapse: separated;
  231. table-align: center;
  232. }
  233.  
  234. .info, input, textarea {
  235. border: 1px solid;
  236. border-radius: 5px;
  237. padding: 5px;
  238. }
  239.  
  240. .info {
  241. font-size: 10pt;
  242. margin-top: 20px;
  243. margin-bottom: 20px;
  244. }
  245.  
  246. input {
  247. font-size: 15pt;
  248. }
  249.  
  250. label {
  251. font-size: 15pt;
  252. font-weight: bold;
  253. }
  254.  
  255. .error {
  256. /* ERROR background */
  257. background-color: #ef9669;
  258. border-color: maroon;
  259. }
  260.  
  261. .success {
  262. /* SUCCESS background */
  263. background-color: #69ef69;
  264. border-color: green;
  265. }
  266.  
  267. input[type="text"], textarea {
  268. background-color: #fceded;
  269. border-color: gray;
  270. width: 600px;
  271. }
  272.  
  273. input[type="submit"], input[type="reset"] {
  274. background-color: #fedcab;
  275. border-color: gray;
  276. }
  277.  
  278. input:hover, textarea:hover {
  279. border-color: silver;
  280. }
  281. </style>
  282. </head>
  283. <body>
  284. <h1><center>Contact form</center></h1>
  285. <?php if(!empty($dir_errors)) {
  286. echo '<div class="info error"';
  287. if($test_general) {
  288. echo ' title="Testing dump"'; // when you enter ?test_* after the contact-form.php in your address bar,
  289. // it will perform a testing dump of the errors. Combine multiple errors
  290. // by adding next &test_* statements. That applies also to test_success
  291. // statement, which tests the success message.
  292. // This should be removed in final tweaks on real website, don't forget!
  293. }
  294. echo '>';
  295. echo $dir_errors;
  296. echo '</div>';
  297. }
  298. if(!empty($dir_success)) {
  299. echo '<div class="info success"';
  300. if($test_general) {
  301. echo ' title="Testing dump"';
  302. }
  303. echo '>';
  304. echo $dir_success;
  305. echo '</div>';
  306. } ?>
  307. <form method="post" action="<?php echo $_SERVER['PHP_SELF']; // points to self page ?>">
  308. <center><strong>Every single field is required!</strong></center>
  309. <label for="from"><table>
  310. <tr><td width="200">Sender e-mail address:</td>
  311. <td width="650"><input type="text" name="from"<?php if(!$default) {
  312. if(!empty($from) && !$successfully_sent) {
  313. echo " value=\"$from\"";
  314. }
  315. } ?> placeholder="john.cena@gmail.com" /></td></tr>
  316. </table></label>
  317. <label for="name"><table>
  318. <tr><td width="200">Name:</td>
  319. <td width="650"><input type="text" name="name"<?php if(!$default) {
  320. if(!empty($name) && !$successfully_sent) {
  321. echo " value=\"$name\"";
  322. }
  323. } ?> placeholder="John Cena" /></td></tr>
  324. </table></label>
  325. <label for="subject"><table>
  326. <tr><td width="200">Subject:</td>
  327. <td width="650"><input type="text" name="subject"<?php if(!$default) {
  328. if(!empty($subject) && !$successfully_sent) {
  329. echo " value=\"$subject\"";
  330. }
  331. } ?> placeholder="Important notice!" /></td></tr>
  332. </table></label>
  333. <label for="msg"><table>
  334. <tr><td width="200">Message body:</td>
  335. <td width="650"><textarea name="msg" cols="70" rows="20"><?php if(!$default) {
  336. if(!empty($msg) && !$successfully_sent) {
  337. echo "$msg";
  338. }
  339. } ?></textarea></td></tr>
  340. </table></label>
  341. <center><input type="submit" value="Submit" />&nbsp;<input type="reset" value="Cancel" /></center>
  342. </form>
  343. </body>
  344. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement