Share Pastebin
Guest
Public paste!

squirrel

By: a guest | Jul 30th, 2010 | Syntax: PHP | Size: 5.67 KB | Hits: 38 | Expires: Never
This paste has a previous version, view the difference. Copy text to clipboard
  1. <?php
  2. /*
  3. ** Ok. This script may require some editing at different points along it.
  4. ** It's been made very easy to edit when you find the right area. It's
  5. ** commented very well at important sections, so you shouldn't have
  6. ** any trouble getting things the way you want them. You can support
  7. ** any number of form elemets, and format them any way you like in a very
  8. ** easy manner. You can also format the 'thank you' page easily as well.
  9. ** Look through the script and find what you want to change and/or edit.
  10. ** Recommended things to look for are:
  11. **      $recipient_email        //Email of person getting this email
  12. **      $redirect_page          //The URL to redirect to after the thanks is displayed
  13. **      $redirect_wait          //How long to wait on the thanks page
  14. **      $thanks_text            //Text displayed to user after email successfully sent
  15. **      $require_fields         //Form fields required before email will be sent
  16. **      $subject                //Subject of email being sent
  17. **      $newsletter             //This is just interesting
  18. **      $message                //Text of the email being sent
  19. */
  20.  
  21.  
  22. /*
  23. ** This function used from http://snipplr.com/view/25/format-phone-number/
  24. */
  25.  
  26. function format_phone($phone)
  27. {
  28.         $phone = preg_replace("/[^0-9]/", "", $phone);
  29.  
  30.         if(strlen($phone) == 7)
  31.                 return preg_replace("/([0-9]{3})([0-9]{4})/", "$1-$2", $phone);
  32.         elseif(strlen($phone) == 10)
  33.                 return preg_replace("/([0-9]{3})([0-9]{3})([0-9]{4})/", "($1) $2-$3", $phone);
  34.         else
  35.                 return $phone;
  36. }
  37.  
  38.  
  39.  
  40. /*
  41. **This makes all the form fields into variables in the form of
  42. **      $f_<form field name>.
  43. **      Example:
  44. **              If I had a field on my form as:
  45. **                      <input type="text" name="email" />
  46. **              I could access it in this script with:
  47. **                      $f_email
  48. */
  49. import_request_variables("gp","f_");
  50.  
  51.  
  52. //Your email address
  53. $recipient_email = "name@example.com";
  54.  
  55. /*
  56. ** This is the page your user will be sent to after they send an email.
  57. ** Uncomment or edit this option as you see fit. If you comment it out
  58. ** the user will not be given a return link or automatically redirected.
  59. */
  60. //$redirect_page = "/"; //Return the user to your homepage.
  61. //$redirect_page = "http://url.com/path/to/page.html"; //Send user to a specific webpage.
  62. //$redirect_page="http://google.com";
  63.  
  64. /*
  65. ** Time (in seconds) to wait before redirect. If $thanks_page is commented
  66. ** out, this does nothing. Don't comment this out.
  67. */
  68. $redirect_wait = 5;
  69.  
  70. /*
  71. ** The text you put between <<<REDIR and REDIR; will be shown to the user
  72. ** as a thank you page. If you have defined $thanks_page above, a link
  73. ** will be automatically appended to the end of this.
  74. */
  75. $thanks_text = <<<REDIR
  76.  
  77. <h2>Thanks</h2>
  78. Thanks for sending your email to us!
  79.  
  80. REDIR;
  81.  
  82.  
  83. /*
  84. ** This array stores a list of required fields. If you put a field name here
  85. ** it just means that it must be included in the form and not be blank.
  86. ** This does no error or validity checking. That must be handled on an
  87. ** individual field basis further on in the script.
  88. **
  89. ** This is NOT handled by the variable import at the begginning of the file.
  90. ** These names should be exactly as they are in your html file.
  91. */
  92. $require_fields = array("email","first","last");
  93.  
  94. $errors = array();
  95.  
  96. /*
  97. ** Make sure all required fields are filled in. Set the required
  98. && fields at the top of this script.
  99. */
  100.  
  101. foreach($require_fields as $value)
  102. {
  103.         if(!isset($_REQUEST[$value]) || $_REQUEST[$value] == "")
  104.                 $errors[] = "The field '<strong>" . $value . "</strong>' is required!";
  105. }
  106.  
  107.  
  108. /*
  109. ** Validate the email address
  110. */
  111. if(isset($f_email) && $f_email != "")
  112. {
  113. $emailregex = "/^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/";
  114. if(!$val=preg_match($emailregex,$f_email))
  115.         $errors[] = "Email address is invalid.";
  116. }
  117.  
  118.  
  119. /*
  120. ** Format a message to email
  121. */
  122. //Subject of the email to send
  123. $subject = "Mail from my website";
  124. //Let's make $f_newsletter look a bit more friendly in the email.
  125. $newsletter = $f_newsletter?"Yes":"No";
  126. //Let's make $f_phone a little prettier
  127. $phone = format_phone($f_phone);
  128.  
  129.  
  130. /*
  131. **Format the email between the <<<EMAIL and EMAIL;
  132. **It should be emailed exactly as shown between those tags. If you want
  133. **a newline, press return. You can use $f_<form element name> to get the
  134. **strings of your form elements.
  135. */
  136. $message = <<<EMAIL
  137.  
  138. <p><strong>Name:</strong> $f_title $f_first $f_last $f_suffix</p>
  139. <p><strong>Email:</strong> $f_email</p>
  140. <p><strong>Phone:</strong> $phone</p>
  141. <p><strong>Address:</strong><br />
  142. <div style="margin-left:5em;">
  143. $f_address1<br />
  144. $f_address2<br />
  145. $f_city, $f_state  $f_zip
  146. </div></p>
  147. <p><strong>Newsletter:</strong> $newsletter</p>
  148. <p><strong>Comments:</strong> $f_comments</p>
  149.  
  150. EMAIL;
  151.  
  152. $headers = <<<HEAD
  153. MIME-Version: 1.0
  154. Content-type: text/html; charset=iso-8859-1
  155. From: $f_title $f_first $f_last $f_suffix <$f_email>
  156. Reply-To: $f_email
  157. HEAD;
  158.  
  159. if(!count($errors))
  160. {
  161.         if($result = mail($recipient_email,$subject,$message,$headers))
  162.         {
  163.                 echo <<<HTMLS
  164. <html>
  165. <head>
  166. HTMLS;
  167.                 if(isset($redirect_page) && $redirect_page != "")
  168.                         echo "<meta http-equiv=\"refresh\" content=\"".$redirect_wait.";".$redirect_page."\">";
  169.                 echo <<<HTMLM
  170. </head>
  171. <body>
  172. $thanks_text
  173. HTMLM;
  174.                 if(isset($redirect_page) && $redirect_page != "")
  175.                         echo "<p>Click <a href=\"".$redirect_page."\">here</a> to continue</p>";
  176.                 echo <<<HTMLF
  177. </body>
  178. </html>
  179. HTMLF;
  180.         }
  181.         else
  182.         {
  183.                 $errors[] = "Mail failed to send! Please contact the webmaster if this happens repeatedly.";           
  184.         }
  185. }
  186.  
  187.  
  188. /*
  189. ** Show all errors
  190. */
  191. if(count($errors))
  192. {
  193.  
  194. echo "<h2>Error</h2>";
  195. echo "<p>The following errors exist. Please press the browser's back button to correct them.</p>";
  196.  
  197. foreach($errors as $value)
  198. {
  199.         echo $value . "<br />";
  200. }
  201. }
  202.  
  203. ?>