PageRenderTime 47ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/files/mailer.php

https://gitlab.com/eluar/infocus
PHP | 289 lines | 181 code | 45 blank | 63 comment | 25 complexity | 2223e4c437db47453691f7dcd376cdc6 MD5 | raw file
  1. <?php
  2. //start the session
  3. session_start();
  4. //////////////////////////////////////////////////////
  5. // Begin variables to be written out by RapidWeaver //
  6. //////////////////////////////////////////////////////
  7. //set the return URL
  8. $return_url = "../index.php";
  9. //set the users email address
  10. $email = "sami@skynite.com";
  11. //array of fields in form. (In the format "field_name" => "field_label")
  12. $form_fields = array(
  13. "element0" => 'Your Name',
  14. "element1" => 'Your Email',
  15. "element2" => 'Partner Type- Please indicate one of the following types:',
  16. "element3" => 'Partner Name',
  17. "element4" => 'Event/Program Name',
  18. "element5" => 'Event Location City',
  19. "element6" => 'Event Location State',
  20. "element7" => 'Attending Infocus Employees',
  21. "element8" => 'Requested Amount',
  22. "element9" => 'Description of Event',
  23. "element10" => 'Activity/Event Date',
  24. "element11" => 'Products being displayed',
  25. "element12" => 'Web URL for Event',
  26. "element13" => 'Audience Type',
  27. "element14" => 'Marketing items needed',
  28. "element15" => 'Demo Equipment Needed',
  29. "element16" => 'Email blast from Saleforce needed to cutomer data base?',
  30. "element17" => 'Additional Comments'
  31. );
  32. $required_fields = array("element0", "element1", "element2", "element3", "element4", "element5", "element6", "element7", "element8", "element9", "element10", "element12", "element13", "element14", "element15", "element16");
  33. $mail_from_name = "element0";
  34. $mail_from_email = "element1";
  35. $mail_subject = "element9";
  36. //uses the email address defined above as the from email.
  37. $send_from_users_email = false;
  38. //sets the PHP setting 'sendmail_from' for use on a windows server.
  39. $windows_server = false;
  40. // Set up the error and success messages.
  41. $message_success = 'Thank you, your message has been sent.';
  42. $message_unset_fields = "Fields marked with * are required.";
  43. ////////////////////////////////////////////////////
  44. // End variables to be written out by RapidWeaver //
  45. ////////////////////////////////////////////////////
  46. // Check key variable from form against session key.
  47. if ( ! isset($_POST['form_token']) || $_POST['form_token'] !== $_SESSION['security_token']) {
  48. // Set a fixed error message if the keys don't match.
  49. redirect($return_url, 'We cannot verify that you are trying to send an email from this form. Please try again.');
  50. }
  51. // SPAM checking. If the "comment" form field has been filled out,
  52. // send back to form asking to remove content and exit the script.
  53. if ($_POST['comment']) {
  54. redirect($return_url, 'Please remove content from the last textarea before submitting the form again. This is to protect against SPAM abuse.');
  55. }
  56. /////////////////////////
  57. // PROCESS FORM FIELDS //
  58. /////////////////////////
  59. $magic_quotes = (bool) get_magic_quotes_gpc();
  60. foreach ($_POST['form'] as $key => $value) {
  61. if ($magic_quotes) {
  62. $value = stripslashes($value);
  63. }
  64. $_SESSION['form'][$key] = $value;
  65. }
  66. ///////////////////////////
  67. // CHECK REQUIRED FIELDS //
  68. ///////////////////////////
  69. //if any of the required fields are empty
  70. if (check_required_fields($required_fields) === false) {
  71. //return to form with error message.
  72. redirect($return_url, $message_unset_fields);
  73. } else {
  74. ///////////////////////////////////
  75. // ALL IS OK, SETUP GLOBAL VAR'S //
  76. ///////////////////////////////////
  77. //check email address
  78. if ( ! check_email($email)) unset($email);
  79. //set mime boundry. Needed to send the email. Mixed seperates text from attachments.
  80. $mixed_mime_boundary = 'rms-mix-x'.md5(mt_rand()).'x';
  81. //alt seperates html from plain text.
  82. $alt_mime_boundary = 'rms-alt-x'.md5(mt_rand()).'x';
  83. //set the from address if user supplied email is invalid use form owners.
  84. $submitted_email = '';
  85. if (isset($_SESSION['form'][$mail_from_email])) {
  86. $submitted_email = $_SESSION['form'][$mail_from_email];
  87. }
  88. if (check_email($submitted_email) && $send_from_users_email === false) {
  89. $from = $reply_to = $_SESSION['form'][$mail_from_name].' <'.$submitted_email.'>';
  90. } else {
  91. $from = '<'.$email.'>';
  92. $reply_to = check_email($submitted_email) ? '<'.$submitted_email.'>' : $from;
  93. }
  94. //set the email subject
  95. $subject = '';
  96. if (isset($_SESSION['form'][$mail_subject])) {
  97. $subject = $_SESSION['form'][$mail_subject];
  98. }
  99. //email headers
  100. if ($windows_server === true) {
  101. $headers = "From: $from\r\n" .
  102. "Reply-to: $reply_to\r\n" .
  103. "MIME-Version: 1.0\r\nContent-Type: multipart/mixed; " .
  104. "boundary=$mixed_mime_boundary";
  105. } else {
  106. $headers = "From: $from\n" .
  107. "Reply-to: $reply_to\n" .
  108. "MIME-Version: 1.0\nContent-Type: multipart/mixed; " .
  109. "boundary=$mixed_mime_boundary";
  110. }
  111. ////////////////////////////
  112. // CONSTRUCT HTML CONTENT //
  113. ////////////////////////////
  114. //Construct HTML email content, looping through each form element
  115. //Note: When you get to a file attachment you need to use $_FILES['form_element']['name']
  116. //This will just output the name of the file. The files will actually be attached at the end of the message.
  117. //Set a variable for the message content
  118. $html_content = "<html>\n<head>\n<title>" .
  119. safe_escape_string($subject) .
  120. "</title>\n</head>\n<body>\n<p>\n";
  121. ////////////////////////////
  122. // CONSTRUCT TEXT CONTENT //
  123. ////////////////////////////
  124. //construct a plain text version of the email.
  125. $text_content = '';
  126. //build a message from the reply for both HTML and text in one loop.
  127. foreach ($form_fields as $field => $label) {
  128. $html_content .= '<b>' . safe_escape_string($label) . '</b> ';
  129. $text_content .= "$label ";
  130. if (isset($_FILES[$field])) {
  131. $string = (isset($_FILES[$field]['name'])) ? $_FILES[$field]['name'] : '';
  132. } else {
  133. $string = (isset($_SESSION['form'][$field])) ? $_SESSION['form'][$field] : '';
  134. }
  135. $html_content .= nl2br(safe_escape_string($string)) . "<br /><br />\n";
  136. $text_content .= "$string\n\n";
  137. }
  138. //close the HTML content.
  139. $html_content .= "</p>\n</body>\n</html>";
  140. /////////////////////////////
  141. // CONSTRUCT EMAIL MESSAGE //
  142. /////////////////////////////
  143. //Now we combine both HTML and plain text version of the email into one.
  144. //Creating the message body which contains a Plain text version and an HTML version,
  145. //users email client will decide which version to display
  146. $message = "\r\n--$mixed_mime_boundary\r\n" .
  147. "Content-Type: multipart/alternative; boundary=$alt_mime_boundary\r\n\r\n" .
  148. "--$alt_mime_boundary\r\n" .
  149. "Content-Type: text/plain; charset=UTF-8; format=flowed\r\n" .
  150. "Content-Transfer-Encoding: Quoted-printable\r\n\r\n" .
  151. "$text_content\r\n\r\n" .
  152. "--$alt_mime_boundary\r\n" .
  153. "Content-Type: text/html; charset=UTF-8\r\n" .
  154. "Content-Transfer-Encoding: Quoted-printable\r\n\r\n" .
  155. "$html_content\r\n\r\n" .
  156. "--$alt_mime_boundary--\r\n\r\n" .
  157. "\r\n\r\n--$mixed_mime_boundary";
  158. //////////////////////
  159. // FILE ATTACHMENTS //
  160. //////////////////////
  161. //loop through the $_FILES global array and add each attachment to the form.
  162. if (isset($_FILES)) {
  163. foreach ($_FILES as $attachment) {
  164. $filename = $attachment['name'];
  165. //if the file has been uploaded
  166. if ($attachment['error'] === UPLOAD_ERR_OK && is_uploaded_file($attachment['tmp_name'])) {
  167. $file = fopen($attachment['tmp_name'],'rb');
  168. $data = fread($file,filesize($attachment['tmp_name']));
  169. fclose($file);
  170. $data = chunk_split(base64_encode($data));
  171. $message .= "\r\nContent-Type: application/octet-stream; name=\"$filename\"" .
  172. "\r\nContent-Disposition: attachment; filename=\"$filename\"" .
  173. "\r\nContent-Transfer-Encoding: base64\r\n\r\n$data\r\n\r\n--$mixed_mime_boundary";
  174. } else if ($attachment['error'] !== UPLOAD_ERR_NO_FILE) {
  175. //try to provide a useful error message determined from the error code.
  176. switch ($attachment['error']) {
  177. case UPLOAD_ERR_INI_SIZE:
  178. case UPLOAD_ERR_FORM_SIZE:
  179. $error = "File $filename exceeds the " . ini_get('upload_max_filesize') . 'B limit for the server.';
  180. break;
  181. case UPLOAD_ERR_PARTIAL:
  182. $error = "Only part of the file $filename could be uploaded, please try again.";
  183. break;
  184. default:
  185. $error = "There has been an error attaching the file $filename, please try again.";
  186. }
  187. redirect($return_url, $error);
  188. }
  189. }
  190. }
  191. //finish off message
  192. $message .= '--';
  193. //for windows users.
  194. if ($windows_server === true) {
  195. ini_set('sendmail_from', $email);
  196. }
  197. //if the mail sending works
  198. if (@mail($email, $subject, $message, $headers)) {
  199. //set the success message
  200. $notice = $message_success;
  201. unset($_SESSION['form']);
  202. } else {
  203. $notice = "I'm sorry, there seems to have been an error trying to send your email. Please try again.";
  204. }
  205. //redirect to the form
  206. redirect($return_url, $notice);
  207. }
  208. //////////////////////
  209. // GLOBAL FUNCTIONS //
  210. //////////////////////
  211. // Redirects back to the form, an optional session message can be defined.
  212. function redirect($url, $message = NULL) {
  213. if ($message) {
  214. $_SESSION['formMessage'] = $message;
  215. }
  216. header('Location: ' . $url);
  217. exit;
  218. }
  219. // Function to escape data inputted from users. This is to protect against embedding
  220. // of malicious code being inserted into the HTML email.
  221. // Sample code: safe_escape_string($_POST['form_field'])
  222. function safe_escape_string($string) {
  223. return htmlspecialchars($string, ENT_QUOTES);
  224. }
  225. // Function to check the validity of email address.
  226. function check_email($email) {
  227. return (bool) preg_match('/^([a-z0-9_]|\-|\.)+@(([a-z0-9_]|\-)+\.)+[a-z]{2,}$/i', $email);
  228. }
  229. // Function to check the required fields are filled in.
  230. function check_required_fields($required_fields) {
  231. foreach ($required_fields as $field) {
  232. if (( ! isset($_SESSION['form'][$field]) || empty($_SESSION['form'][$field])) &&
  233. ( ! isset($_FILES[$field]) || empty($_FILES[$field]['name']))) {
  234. return false;
  235. }
  236. }
  237. return true;
  238. }