PHP Email with attachment of uploaded file


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

A simple one page form with attachment that is meant to go straight to the email and not uploaded to the server. Emails an HTML version, a PlainText version and the attachment if there is one.


Copy this code and paste it in your HTML
  1. <?php
  2. if ($_SERVER['REQUEST_METHOD'] == 'POST')
  3. { // Form was submitted, do some checks and if it passes send the email
  4. $errors = 0; // Keep track of the number of validation errors
  5. $errorLog = ""; // Get some details on what didn't validate
  6. $message = ""; // Starter email message
  7. $request = array();
  8. $required = array("First_Name", "Middle_Initial", "Last_Name" );
  9. $successLog = ""; // Get some details on what went through
  10.  
  11. // Sanitize the inputs
  12. foreach ( $_POST as $formName => $formValue )
  13. { if ( is_array($formValue) )
  14. { foreach ( $formValue as $formName2 => $formValue2 ) { $request[$formName][$formName2] = htmlspecialchars(strip_tags($formValue2)); } }
  15. else
  16. { $request[$formName] = htmlspecialchars(strip_tags($formValue)); }
  17. }
  18.  
  19. // Validate required form fields
  20. foreach ( $request as $name => $value )
  21. { $displayName = str_replace("_", " ", $name);
  22. // Is this a required value, if so and it's empty track the errors
  23. if ( !is_array($value) )
  24. { if ( in_array( $name, $required ) && strlen($value) < 1 )
  25. { $errors++;
  26. $errorLog .= "The field <strong>".$displayName."</strong> is required.<br />";
  27. }
  28. }
  29. if ( $name == "Email" && !filter_var($value, FILTER_VALIDATE_EMAIL) )
  30. { $errors++;
  31. $errorLog .= "The field <strong>".$displayName."</strong> must be a valid email address.<br />";
  32. }
  33. }
  34.  
  35. if ( $errors == 0 )
  36. { // Validation passed, get the email ready to send
  37. $toEmail = "[email protected]";
  38. $subject = "Form Submission from ".$request["First_Name"]." ".$request["Last_Name"];
  39.  
  40. unset ( $request["submit"] );
  41.  
  42. $message .= "Following is an application submitted<br /><br />\n\n<table>";
  43. foreach ( $request as $name => $value )
  44. { $displayName = str_replace("_", " ", $name);
  45. if ( is_array($value) ) { $value = implode(", ", $value); }
  46. $message .= "\n<tr><th align='right' valign='top'><strong>".$displayName."</strong>:</th><td valign='top'>".$value."</td></tr>";
  47. }
  48. $message .= "</table><br /><br />\n\nSubmitted on ".date("F j, Y, g:i a");
  49.  
  50. //Normal headers
  51. $num = md5(time());
  52. $headers = "From: Mailer <[email protected]>
  53. ";
  54. $headers .= "Reply-to: ".$request["First_Name"]." ".$request["Last_Name"]." <".$request["Email"].">
  55. ";
  56. $headers .= "MIME-Version: 1.0
  57. ";
  58. $headers .= "Content-Type: multipart/alternative; boundary=".$num."
  59. ";
  60. // These two steps to help avoid spam
  61. $headers .= "Message-ID: <".gettimeofday()." TheSystem@".$_SERVER['SERVER_NAME'].">
  62. ";
  63. $headers .= "X-Mailer: PHP v".phpversion()."
  64. ";
  65. // Text version
  66. $headers .= "\n--".$num."\n";
  67. $headers .= "Content-Type: text/plain; charset=iso-8859-1
  68. ";
  69. $headers .= "Content-Transfer-Encoding: 8bit
  70. ";
  71. $headers .= "".strip_tags($message)."\n";
  72. // HTML message
  73. $headers .= "\n--".$num."\n";
  74. $headers .= "Content-Type: text/html; charset=iso-8859-1
  75. ";
  76. $headers .= "".$message."\n";
  77.  
  78. if ( strlen($_FILES['File']['name']) > 0 )
  79. { // If a file was uploaded, do some processing
  80. $filename = preg_replace('/[^a-zA-Z0-9._-]/', '', $_FILES['File']['name']);
  81. $filetype = $_FILES["File"]["type"];
  82. $filesize = $_FILES["File"]["size"];
  83. $filetemp = $_FILES["File"]["tmp_name"];
  84. $ext = substr($filename, strpos($filename,'.'), strlen($filename)-1);
  85.  
  86. if ( !preg_match('/\.(doc|docx|odt|pdf|rtf|txt)/i', $ext) )
  87. { $errors++;
  88. $errorLog .= "Upload filetype not supported.";
  89. }
  90. else if ( $filesize > 2000000 )
  91. { $errors++;
  92. $errorLog .= "File size too high, up to 2MB is allowed.";
  93. }
  94. else
  95. { // Looks like the file is good, send it with the email
  96. $fp = fopen($filetemp, "rb");
  97. $file = fread($fp, $filesize);
  98. $file = chunk_split(base64_encode($file));
  99.  
  100. // Attachment headers
  101. $headers .= "\n--".$num."\n";
  102. $headers .= "Content-Type:".$filetype." ";
  103. $headers .= "name=\"".$filename."\"r\n";
  104. $headers .= "Content-Disposition: attachment; ";
  105. $headers .= "filename=\"".$filename."\"
  106. \n";
  107. $headers .= "".$file."
  108. ";
  109.  
  110. }
  111. }
  112. if ( $errors == 0 )
  113. { if ( mail( $toEmail, $subject, "", $headers ))
  114. { $successLog .= "<b>Thank you for your interest<br />Your form has been submitted.</b>"; }
  115. else
  116. { $errorLog .= "Message failed to send, please refresh to try again"; }
  117. // Send email
  118. }
  119. }
  120. }
  121. ?>
  122. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  123. <html xmlns="http://www.w3.org/1999/xhtml">
  124. <head>
  125. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  126. <title>Untitled Document</title>
  127. </head>
  128.  
  129. <body>
  130. <form method="post" enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF'];?>" name="frmApplication" id="frmApplication">
  131. <fieldset><legend>Personal Information</legend>
  132. <table cellpadding="0" cellspacing="6" border="0" class="appFormTable">
  133. <tr>
  134. <td valign="top" colspan="3">
  135. <label for="First_Name" class="required">First Name*</label>
  136. <input type="text" name="First_Name" id="First_Name" maxlength="40" value="<?php echo @$request['First_Name']; ?>" />
  137. </td>
  138. <td valign="top" colspan="2">
  139. <label for="Middle_Initial" class="required">Middle Initial*</label>
  140. <input type="text" name="Middle_Initial" id="Middle_Initial" maxlength="1" value="<?php echo @$request['Middle_Initial']; ?>" />
  141. </td>
  142. <td valign="top" colspan="3">
  143. <label for="Last_Name" class="required">Last Name*</label>
  144. <input type="text" name="Last_Name" id="Last_Name" maxlength="40" value="<?php echo @$request['Last_Name']; ?>" />
  145. </td>
  146. </tr>
  147. <tr>
  148. <td valign="top" colspan="8">
  149. <label for="Address" class="required">Address*</label>
  150. <input type="text" name="Address" id="Address" maxlength="100" value="<?php echo @$request['Address']; ?>" />
  151. </td>
  152. </tr>
  153. <tr>
  154. <td valign="top" colspan="3">
  155. <label for="City" class="required">City*</label>
  156. <input type="text" name="City" id="City" maxlength="50" value="<?php echo @$request['City']; ?>" />
  157. </td>
  158. <td valign="top" colspan="3">
  159. <label for="State" class="required">State*</label>
  160. <select name="State" id="State">
  161. <option value="" <?php if ( @$request['State'] == "") { echo "selected='selected'"; } ?>>- Select a State -</option>
  162. <option value="AL" <?php if ( @$request['State'] == "AL") { echo "selected='selected'"; } ?>>Alabama</option>
  163. <option value="AK" <?php if ( @$request['State'] == "AK") { echo "selected='selected'"; } ?>>Alaska</option>
  164. <option value="AZ" <?php if ( @$request['State'] == "AZ") { echo "selected='selected'"; } ?>>Arizona</option>
  165. <option value="AR" <?php if ( @$request['State'] == "AR") { echo "selected='selected'"; } ?>>Arkansas</option>
  166. <option value="CA" <?php if ( @$request['State'] == "CA") { echo "selected='selected'"; } ?>>California</option>
  167. <option value="CO" <?php if ( @$request['State'] == "CO") { echo "selected='selected'"; } ?>>Colorado</option>
  168. <option value="CT" <?php if ( @$request['State'] == "CT") { echo "selected='selected'"; } ?>>Connecticut</option>
  169. <option value="DE" <?php if ( @$request['State'] == "DE") { echo "selected='selected'"; } ?>>Delaware</option>
  170. <option value="DC" <?php if ( @$request['State'] == "DC") { echo "selected='selected'"; } ?>>District Of Columbia</option>
  171. <option value="FL" <?php if ( @$request['State'] == "FL") { echo "selected='selected'"; } ?>>Florida</option>
  172. <option value="GA" <?php if ( @$request['State'] == "GA") { echo "selected='selected'"; } ?>>Georgia</option>
  173.  
  174. <option value="HI" <?php if ( @$request['State'] == "HI") { echo "selected='selected'"; } ?>>Hawaii</option>
  175. <option value="ID" <?php if ( @$request['State'] == "ID") { echo "selected='selected'"; } ?>>Idaho</option>
  176. <option value="IL" <?php if ( @$request['State'] == "IL") { echo "selected='selected'"; } ?>>Illinois</option>
  177. <option value="IN" <?php if ( @$request['State'] == "IN") { echo "selected='selected'"; } ?>>Indiana</option>
  178. <option value="IA" <?php if ( @$request['State'] == "IA") { echo "selected='selected'"; } ?>>Iowa</option>
  179. <option value="KS" <?php if ( @$request['State'] == "KS") { echo "selected='selected'"; } ?>>Kansas</option>
  180. <option value="KY" <?php if ( @$request['State'] == "KY") { echo "selected='selected'"; } ?>>Kentucky</option>
  181. <option value="LA" <?php if ( @$request['State'] == "LA") { echo "selected='selected'"; } ?>>Louisiana</option>
  182. <option value="ME" <?php if ( @$request['State'] == "ME") { echo "selected='selected'"; } ?>>Maine</option>
  183. <option value="MD" <?php if ( @$request['State'] == "MD") { echo "selected='selected'"; } ?>>Maryland</option>
  184.  
  185. <option value="MA" <?php if ( @$request['State'] == "MA") { echo "selected='selected'"; } ?>>Massachusetts</option>
  186. <option value="MI" <?php if ( @$request['State'] == "MI") { echo "selected='selected'"; } ?>>Michigan</option>
  187. <option value="MN" <?php if ( @$request['State'] == "MN") { echo "selected='selected'"; } ?>>Minnesota</option>
  188. <option value="MS" <?php if ( @$request['State'] == "MS") { echo "selected='selected'"; } ?>>Mississippi</option>
  189. <option value="MO" <?php if ( @$request['State'] == "MO") { echo "selected='selected'"; } ?>>Missouri</option>
  190. <option value="MT" <?php if ( @$request['State'] == "MT") { echo "selected='selected'"; } ?>>Montana</option>
  191. <option value="NE" <?php if ( @$request['State'] == "NE") { echo "selected='selected'"; } ?>>Nebraska</option>
  192. <option value="NV" <?php if ( @$request['State'] == "NV") { echo "selected='selected'"; } ?>>Nevada</option>
  193. <option value="NH" <?php if ( @$request['State'] == "NH") { echo "selected='selected'"; } ?>>New Hampshire</option>
  194. <option value="NJ" <?php if ( @$request['State'] == "NJ") { echo "selected='selected'"; } ?>>New Jersey</option>
  195.  
  196. <option value="NM" <?php if ( @$request['State'] == "NM") { echo "selected='selected'"; } ?>>New Mexico</option>
  197. <option value="NY" <?php if ( @$request['State'] == "NY") { echo "selected='selected'"; } ?>>New York</option>
  198. <option value="NC" <?php if ( @$request['State'] == "NC") { echo "selected='selected'"; } ?>>North Carolina</option>
  199. <option value="ND" <?php if ( @$request['State'] == "ND") { echo "selected='selected'"; } ?>>North Dakota</option>
  200. <option value="OH" <?php if ( @$request['State'] == "OH") { echo "selected='selected'"; } ?>>Ohio</option>
  201. <option value="OK" <?php if ( @$request['State'] == "OK") { echo "selected='selected'"; } ?>>Oklahoma</option>
  202. <option value="OR" <?php if ( @$request['State'] == "OR") { echo "selected='selected'"; } ?>>Oregon</option>
  203. <option value="PA" <?php if ( @$request['State'] == "PA") { echo "selected='selected'"; } ?>>Pennsylvania</option>
  204. <option value="RI" <?php if ( @$request['State'] == "RI") { echo "selected='selected'"; } ?>>Rhode Island</option>
  205. <option value="SC" <?php if ( @$request['State'] == "SC") { echo "selected='selected'"; } ?>>South Carolina</option>
  206.  
  207. <option value="SD" <?php if ( @$request['State'] == "SD") { echo "selected='selected'"; } ?>>South Dakota</option>
  208. <option value="TN" <?php if ( @$request['State'] == "TN") { echo "selected='selected'"; } ?>>Tennessee</option>
  209. <option value="TX" <?php if ( @$request['State'] == "TX") { echo "selected='selected'"; } ?>>Texas</option>
  210. <option value="UT" <?php if ( @$request['State'] == "UT") { echo "selected='selected'"; } ?>>Utah</option>
  211. <option value="VT" <?php if ( @$request['State'] == "VT") { echo "selected='selected'"; } ?>>Vermont</option>
  212. <option value="VA" <?php if ( @$request['State'] == "VA") { echo "selected='selected'"; } ?>>Virginia</option>
  213. <option value="WA" <?php if ( @$request['State'] == "WA") { echo "selected='selected'"; } ?>>Washington</option>
  214. <option value="WV" <?php if ( @$request['State'] == "WV") { echo "selected='selected'"; } ?>>West Virginia</option>
  215. <option value="WI" <?php if ( @$request['State'] == "WI") { echo "selected='selected'"; } ?>>Wisconsin</option>
  216. <option value="WY" <?php if ( @$request['State'] == "WY") { echo "selected='selected'"; } ?>>Wyoming</option>
  217. </select>
  218. </td>
  219. <td valign="top" colspan="2">
  220. <label for="Zip" class="required">Zip*</label>
  221. <input type="text" name="Zip" id="Zip" maxlength="10" value="<?php echo @$request['Zip']; ?>" />
  222. </td>
  223. </tr>
  224. <tr>
  225. <td valign="top" colspan="4">
  226. <label for="Phone_1" class="required">Phone 1*</label>
  227. <input type="text" name="Phone_1" id="Phone_1" maxlength="20" value="<?php echo @$request['Phone_1']; ?>" />
  228. </td>
  229. <td valign="top" colspan="4">
  230. <label for="Phone_2">Phone 2</label>
  231. <input type="text" name="Phone_2" id="Phone_2" maxlength="20" value="<?php echo @$request['Phone_2']; ?>" />
  232. </td>
  233. </tr>
  234. <tr>
  235. <td valign="top" colspan="8">
  236. <label for="Email" class="required">Email Address*</label>
  237. <input type="text" name="Email" id="Email" maxlength="100" value="<?php echo @$request['Email']; ?>" />
  238. </td>
  239. </tr>
  240. <tr>
  241. <td valign="top" colspan="8">
  242. <div id="Resume_Format_Attach">
  243. <p><small>Supported file types are doc,docx,odt,pdf,rtf,txt</small></p>
  244. <input type="hidden" name="max_file_size" value="2000000" />
  245. <input type="file" name="Resume_File" id="Resume_File" />
  246. </div>
  247. </td>
  248. </tr>
  249. </table>
  250. </fieldset>
  251. <div align="center"><input type="submit" name="submit" value="Submit Form" /></div>
  252. </form>
  253. </body>
  254. </html>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.