Advertisement
Guest User

Untitled

a guest
May 29th, 2016
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.71 KB | None | 0 0
  1. if (data.error) {
  2. alert(data.error.message);
  3. }`
  4.  
  5. $(document).ready(function (e){
  6. $("#main-contact-form").on('submit',(function(e){
  7. e.preventDefault();
  8. $('#sendingemail').fadeIn();
  9. $.ajax({
  10. url: "../sendemail.php",
  11. type: "POST",
  12. data: new FormData(this),
  13. contentType: false,
  14. cache: false,
  15. processData: false,
  16. success: function(data){
  17. if (data.error) {
  18. alert(data.error.message);
  19. }
  20. else{
  21. $('#sendingemail').fadeOut();
  22. $('#emailsent').fadeIn();
  23. alert(data.message);
  24. }
  25. },
  26. error: function(XHR,textStatus,errorThrown) {
  27. console.log(data);
  28. //alert("error");
  29. alert(XHR.status);
  30. alert(textStatus);
  31. alert(errorThrown);
  32. }
  33. }
  34. );
  35. }));
  36. });
  37.  
  38. <?php
  39.  
  40. header('Content-type: application/json');
  41.  
  42. // WE SHOULD ASSUME THAT THE EMAIL WAS NOT SENT AT FIRST UNTIL WE KNOW MORE.
  43. // WE ALSO ADD AN ATTACHMENT KEY TO OUR STATUS ARRAY TO INDICATE THE STATUS OF OUR ATTACHMENT:
  44. $status = array(
  45. 'type' =>'Error',
  46. 'message' =>'Couldn't send the Email at this Time. Something went wrong',
  47. 'attachement' =>'Couldn't attach the uploaded File to the Email.'
  48. );
  49.  
  50. //Added to deal with Files
  51. require_once('PHPMailer/class.phpmailer.php');
  52.  
  53. if(isset($_FILES['uploaded_file'])){
  54. //Get the uploaded file information
  55. $name_of_uploaded_file =
  56. basename($_FILES['uploaded_file']['name']);
  57.  
  58. //get the file extension of the file
  59. $type_of_uploaded_file =
  60. substr($name_of_uploaded_file,
  61. strrpos($name_of_uploaded_file, '.') + 1);
  62.  
  63. $size_of_uploaded_file =
  64. $_FILES["uploaded_file"]["size"]/1024;//size in KBs
  65.  
  66. //Settings
  67. $max_allowed_file_size = 10000; // size in KB
  68. $allowed_extensions = array("jpg", "jpeg", "gif", "bmp","png");
  69.  
  70. //Validations
  71. if($size_of_uploaded_file > $max_allowed_file_size )
  72. {
  73. $status['type'] = 'Error';
  74. $status['message'] = 'Error: Size of file should be less than ~10MB. The file you attempted to upload is too large. To reduce the size, open the file in an image editor and change the Image Size and resave the file.';
  75. echo(json_encode($status));
  76. exit;
  77. }
  78.  
  79. //------ Validate the file extension -----
  80. $allowed_ext = false;
  81. for($i=0; $i<sizeof($allowed_extensions); $i++)
  82. {
  83. if(strcasecmp($allowed_extensions[$i],$type_of_uploaded_file) == 0)
  84. {
  85. $allowed_ext = true;
  86. }
  87. }
  88.  
  89. if(!$allowed_ext)
  90. {
  91. $status['type'] = 'Error';
  92. $status['message'] = 'Error: The uploaded file is not a supported file type.';
  93. echo(json_encode($status));
  94. exit;
  95. }
  96.  
  97. $upload_folder = "temp/";
  98. $path_of_uploaded_file = $upload_folder . $name_of_uploaded_file;
  99. $tmp_path = $_FILES["uploaded_file"]["tmp_name"];
  100.  
  101. if(is_uploaded_file($tmp_path))
  102. {
  103. if(!copy($tmp_path,$path_of_uploaded_file))
  104. {
  105. $status['type'] = 'Error';
  106. $status['message'] = 'Error: Encountered an error while copying the uploaded file';
  107. exit;
  108. }
  109. }
  110. }
  111. //--end
  112.  
  113. $name = @trim(stripslashes($_POST['name']));
  114. $clientemail = @trim(stripslashes($_POST['email']));
  115. $subject = @trim(stripslashes($_POST['subject']));
  116. $message = @trim(stripslashes($_POST['message']));
  117.  
  118. $body = 'Name: ' . $name . "nn" . 'Email: ' . $clientemail . "nn" . 'Subject: ' . $subject . "nn" . 'Message: ' . $message;
  119.  
  120. $email = new PHPMailer();
  121.  
  122. $email->From = $clientemail;
  123. $email->FromName = $name;
  124. $email->Subject = $subject;
  125. $email->Body = $body;
  126. $email->AddAddress( 'root@localhost.com' ); //Send to this email
  127.  
  128. $email->isMail();
  129.  
  130. if(isset($_FILES['uploaded_file'])){
  131. if($email->AddAttachment( $path_of_uploaded_file , $name_of_uploaded_file )){
  132. $status['message'] = 'The Uploaded File was successfully attached to the Email.';
  133. }
  134. }
  135. header("Content-Type: application/json; charset=utf-8", true);
  136. // NOW, TRY TO SEND THE EMAIL ANYWAY:
  137. try{
  138. $success = $email->send();
  139. $status['type'] = 'success';
  140. $status['message'] = 'Thank you for contacting us. We will reply as soon as possible.';
  141. }catch(Exception $e){
  142. $status['type'] ='Error';
  143. $status['message'] ='Couldn't send the Email at this Time. Something went wrong';
  144. }
  145.  
  146. die(json_encode($status));
  147.  
  148. <form id="main-contact-form" class="contact-form" name="contact-form" method="post" action="sendemail.php" enctype="multipart/form-data">
  149. <div class="col-sm-5 col-sm-offset-1">
  150. <div class="form-group">
  151. <label>Name *</label>
  152. <input type="text" name="name" class="form-control" required="required">
  153. </div>
  154. <div class="form-group">
  155. <label>Email *</label>
  156. <input type="email" name="email" class="form-control" required="required">
  157. </div>
  158. <div class="form-group">
  159. <label>Phone</label>
  160. <input type="number" class="form-control">
  161. </div>
  162. <div class="form-group">
  163. <label>Company Name</label>
  164. <input type="text" class="form-control">
  165. </div>
  166. </div>
  167. <div class="col-sm-5">
  168. <div class="form-group">
  169. <label>Subject *</label>
  170. <input type="text" name="subject" class="form-control" required="required">
  171. </div>
  172. <div class="form-group">
  173. <label>Message *</label>
  174. <textarea name="message" id="message" required="required" class="form-control" rows="8" style="height:125px"></textarea>
  175. <label for='uploaded_file' style="margin-top:10px">Select A Photo To Upload:</label>
  176. <input type="file" name="uploaded_file">
  177. </div>
  178. <div class="form-group">
  179. <button type="submit" name="submit" class="btn btn-primary btn-lg" required="required">Submit Message</button>
  180. </div>
  181. </div>
  182. </form>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement