PHP Contact Script


/ Published in: PHP
Save to your folder(s)

Version 1.5


Copy this code and paste it in your HTML
  1. // where you want the emails to go to
  2. // separate multiple emails with a comma.
  3. $contact_to_email="...";
  4.  
  5. // this will be the first part of the subject line of mail
  6. // sent from this script (identifies mail from this page)
  7. $contact_subject="...";
  8.  
  9. // emails sent from this page may appear to come from this
  10. // email address. change YOURDOMAIN.COM is to be the same
  11. // as your website's domain name
  12. $contact_from_email="...";
  13.  
  14. // emails sent from here may come from this name.
  15. // Change this to be the name of your website.
  16. $contact_from_name="...";
  17.  
  18.  
  19. // If your host blocks messages with To: fields coming
  20. // from other domains, then you may change this to true.
  21. // If the script works fine as it is, just leave it as false.
  22. //
  23. // If this variable is false, messages will appear to come
  24. // directly from the email address of the person who
  25. // filled out the form, rather than appearing to come from
  26. // the $contact_from_email above.
  27. $send_from_internal_address=false;
  28.  
  29. // The color the errors will come out as when they
  30. // are displayed on the screen, use either a name or code
  31. // such as #0000FF
  32. $error_color="red";
  33.  
  34. // Use a security image only if you have a problem with automated
  35. // bots submitting SPAM to this form. You will need the
  36. // securityimage directory, which is located in the zip
  37. // file on www.douglassdavis.com
  38. //
  39. // change to true to use security image
  40. // false to not use security image.
  41. //
  42. // The downside to using a security image is that visually impaired
  43. // people will not be able to use the form.
  44.  
  45. $use_security_image=false;
  46.  
  47.  
  48. function previous_request_value($str)
  49. {
  50. if (isset($_REQUEST[$str]) )
  51. return $_REQUEST[$str];
  52. else
  53. return '';
  54. }
  55.  
  56. function cndstrips($str)
  57. {
  58. if (get_magic_quotes_gpc())
  59. return stripslashes($str);
  60. else
  61. return $str;
  62. }
  63.  
  64. $visitor_email=cndstrips(trim(previous_request_value('visitor_email')));
  65. $visitor_name=cndstrips(trim(previous_request_value('visitor_name')));
  66. $message_body=cndstrips(previous_request_value('message_body'));
  67. $message_subject=cndstrips(previous_request_value('message_subject'));
  68. $security_code=str_replace(' ','',cndstrips(trim(previous_request_value('security_code'))));
  69.  
  70. $errors="";
  71. $message_sent=false;
  72.  
  73. function validate_email($email) {
  74. return preg_match('/^[A-Za-z0-9_\-\.]+@[A-Za-z0-9_\-\.]+\.[A-Za-z0-9_\-\.]+$/', $email) == 0;
  75. }
  76.  
  77.  
  78. if ($_SERVER['REQUEST_METHOD'] == 'POST')
  79. {
  80.  
  81. if (validate_email($visitor_email) ) {
  82. $errors.="<br/><br/>Please enter a valid email address in the form of [email protected]";
  83. }
  84.  
  85. if ($use_security_image && (strtolower($security_code) != strtolower($_SESSION['contact_form_security_code']) || $_SESSION['contact_form_security_code']=='') ) {
  86. $errors.="<br/><br/>The verification code for the image presented was incorrect. Please enter a correct verification code.";
  87. }
  88.  
  89. if ($message_body == '')
  90. $errors.="<br/><br/>Please enter a message";
  91.  
  92. if ($message_subject == '')
  93. $errors.="<br/><br/>Please enter a message subject";
  94.  
  95. if ($visitor_name == '')
  96. $errors.="<br/><br/>Please enter your name";
  97.  
  98. if ( !$errors ) {
  99. $ip = $_SERVER["REMOTE_ADDR"];
  100. $httpagent = $_SERVER["HTTP_USER_AGENT"];
  101. $time = date("D, F j, Y H:i O");
  102.  
  103. if ($visitor_name)
  104. $visitor_name_and_email="$visitor_name <$visitor_email>";
  105. else
  106. $visitor_name_and_email="$visitor_email";
  107.  
  108. if ($contact_from_name)
  109. $contact_from_email="$contact_from_name <$contact_from_email>";
  110.  
  111. $message = "
  112.  
  113. $message_body
  114.  
  115. ____________________________
  116. Browser Info: $ip $httpagent
  117. ";
  118.  
  119. if ($send_from_internal_address) {
  120. $message= "
  121. From: $visitor_name_and_email
  122. Date: $time
  123. Subject: $message_subject
  124. ".$message;
  125. }
  126.  
  127. if ($send_from_internal_address) {
  128. mail($contact_to_email, $contact_subject." $message_subject", $message, "From: $contact_from_email
  129. Reply-To: $visitor_name_and_email");
  130. }
  131. else {
  132. mail($contact_to_email, $contact_subject." $message_subject", $message, "From: $visitor_name_and_email");
  133. }
  134.  
  135. echo "Your message:";
  136. echo "<div style='border: 1px solid white; margin: 10px 10px 10px 10px; padding: 10px 10px 10px 10px;'>From: ".htmlentities($visitor_name_and_email)."<br />Re: ".htmlentities($message_subject)."<br />".htmlentities($message_body)."</div>";
  137. echo "Has been sent. Thank you for contacting us.";
  138. $message_sent=true;
  139. }
  140. }
  141.  
  142. if (!$message_sent) {
  143. $this_file = substr(strrchr($_SERVER['PHP_SELF'], "/"), 1);
  144.  
  145. ?>
  146.  
  147. <form name="ContactForm" id="ContactForm" method="post" action="<?php echo $this_file ?>">
  148. <br />We are happy to hear from you. Please enter the requested information <?php if (!$message_body) echo "and message" ?> below, then click the Send button.
  149.  
  150. <?php
  151. if ($errors) {
  152. echo "<span style='color:$error_color'>$errors</span>";
  153. }
  154. ?>
  155.  
  156. <br /><br/><label for="visitor_name">Your name</label><br />
  157. <input name="visitor_name" type="text" id="visitor_name" value="<?php echo htmlentities($visitor_name) ?>" size="25" />
  158. <br /><br />
  159.  
  160. <label for="visitor_name">Your Email Address</label><br />
  161. <input name="visitor_email" type="text" id="visitor_email" value="<?php echo htmlentities($visitor_email) ?>" size="25"/>
  162. <br /><br />
  163.  
  164. <label for="message_subject">Subject</label><br />
  165. <input name="message_subject" type="text" id="message_subject" value="<?php echo htmlentities($message_subject) ?>" size="25"/>
  166. <br /><br />
  167.  
  168. <?php
  169. if ($use_security_image) {
  170. ?>
  171. <div id="security"><img src="securityimage/security-image.php?width=200" width="200" height="40" alt="Verification Image" /></div>
  172.  
  173. Verification Image (please enter the text in the image above) <br />
  174. <input name="security_code" type="text" id="security_code" size="25"/>
  175. <br /><br />
  176.  
  177. <?php
  178. }
  179. ?>
  180.  
  181. <label for="message_body">Message</label><br />
  182. <textarea name="message_body" cols="30" rows="6" id="message_body" ><?php echo htmlentities($message_body) ?></textarea>
  183. <br /><br />
  184.  
  185. <input type="submit" name="Submit" value="Send" />
  186.  
  187. </form>
  188. <?php
  189. }
  190. ?>

URL: http://www.douglassdavis.com/

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.