Advertisement
Guest User

Untitled

a guest
Jul 20th, 2017
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.40 KB | None | 0 0
  1. <?php
  2.  
  3. $to = 'example@gmail.com';
  4. $errors = array();
  5. $method = $_SERVER['REQUEST_METHOD'];
  6. $c = true;
  7.  
  8. if ( $method === 'POST' ) {
  9.  
  10. $project_name = trim($_POST["project_name"]);
  11. $form_subject = trim($_POST["form_subject"]);
  12.  
  13. foreach ( $_POST as $key => $value ) {
  14. if ( $value != "" && $key != "project_name" && $key != "form_subject" ) {
  15. $message .= "
  16. " . ( ($c = !$c) ? '<tr>':'<tr style="background-color: #f8f8f8;">' ) . "
  17. <td style='padding: 10px; border: #e9e9e9 1px solid;'><b>$key</b></td>
  18. <td style='padding: 10px; border: #e9e9e9 1px solid;'>$value</td>
  19. </tr>
  20. ";
  21. }
  22. }
  23. } else if ( $method === 'GET' ) {
  24.  
  25. $project_name = trim($_GET["project_name"]);
  26. $form_subject = trim($_GET["form_subject"]);
  27.  
  28. foreach ( $_GET as $key => $value ) {
  29. if ( $value != "" && $key != "project_name" && $key != "form_subject" ) {
  30. $message .= "
  31. " . ( ($c = !$c) ? '<tr>':'<tr style="background-color: #f8f8f8;">' ) . "
  32. <td style='padding: 10px; border: #e9e9e9 1px solid;'><b>$key</b></td>
  33. <td style='padding: 10px; border: #e9e9e9 1px solid;'>$value</td>
  34. </tr>
  35. ";
  36. }
  37. }
  38. }
  39.  
  40. $message = "<table style='width: 100%;'>$message</table>";
  41.  
  42. function adopt($text) {
  43. return '=?UTF-8?B?'.Base64_encode($text).'?=';
  44. }
  45.  
  46. if ( isset( $_FILES["file"]) ) {
  47.  
  48.  
  49. $file_name = $_FILES["file"]["name"];
  50. $file_size =$_FILES["file"]["size"];
  51. $file_tmp =$_FILES["file"]["tmp_name"];
  52. $file_type=$_FILES["file"]["type"];
  53. $file_ext=strtolower(end(explode(".",$_FILES["file"]["name"])));
  54.  
  55. $expensions= array("jpeg","jpg","png", "doc", "docx", "pdf");
  56.  
  57. if(in_array($file_ext,$expensions) === false){
  58. $errors["status"] = "error";
  59. $errors["text"]= "Выберите файл jpg, png, pdf или doc";
  60. }
  61. else if ( !empty( $_FILES['file']['tmp_name'] ) and $_FILES['file']['error'] == 0 ) {
  62. $filepath = $_FILES['file']['tmp_name'];
  63. $filename = $_FILES['file']['name'];
  64. } else {
  65. $filepath = '';
  66. $filename = '';
  67. }
  68.  
  69. }
  70.  
  71. if(empty($errors)){
  72. send_mail($to, $message, $project_name, $form_subject, $filepath, $filename);
  73. $errors["status"] = "success";
  74. $errors["text"] = "Заявка отправлена!";
  75. }
  76.  
  77. // Вспомогательная функция для отправки почтового сообщения с вложением
  78. function send_mail($to, $message, $project_name, $form_subject, $filepath, $filename)
  79. {
  80.  
  81. $boundary = "--".md5(uniqid(time())); // генерируем разделитель
  82. $headers = "From: ".adopt($project_name)." <".$to.">" . PHP_EOL . "Reply-To: ".$to."" . PHP_EOL;
  83. $headers .= "MIME-Version: 1.0\r\n";
  84. $headers .="Content-Type: multipart/mixed; boundary=\"".$boundary."\"\r\n";
  85.  
  86. $multipart = "--".$boundary."\r\n";
  87. $multipart .= "Content-type: text/html; charset=\"utf-8\"\r\n";
  88. $multipart .= "Content-Transfer-Encoding: quoted-printable\r\n\r\n";
  89.  
  90. $message = $message."\r\n\r\n";
  91.  
  92. $multipart .= $message;
  93.  
  94. $file = '';
  95. if ( !empty( $filepath ) ) {
  96. $fp = fopen($filepath, "r");
  97. if ( $fp ) {
  98. $content = fread($fp, filesize($filepath));
  99. fclose($fp);
  100. $file .= "--".$boundary."\r\n";
  101. $file .= "Content-Type: application/octet-stream\r\n";
  102. $file .= "Content-Transfer-Encoding: base64\r\n";
  103. $file .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n";
  104. $file .= chunk_split(base64_encode($content))."\r\n";
  105. }
  106. }
  107. $multipart .= $file."--".$boundary."--\r\n";
  108. mail($to, adopt($form_subject), $multipart, $headers);
  109. }
  110.  
  111. echo json_encode($errors);
  112. exit;
  113.  
  114. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement