Newer
Older
EmailSender / script.js
0xRoM on 8 Sep 2023 6 KB first commit
  1. $(document).ready(function() {
  2. // Function to update the iframe content
  3. function updateIframeContent() {
  4. var textareaContent = $("#html_content").val();
  5. $("#iframe_id").attr("srcdoc", textareaContent);
  6. }
  7.  
  8. // Add a click event handler for the "Render" tab
  9. $("#pills-render-tab").on("click", function() {
  10. updateIframeContent();
  11. });
  12.  
  13. // Initially update the iframe content
  14. updateIframeContent();
  15. });
  16.  
  17.  
  18. // Function to initialize the iframe resizing
  19. function initResizableIframe() {
  20. const iframe = document.getElementById('iframe_id');
  21.  
  22. // Function to update the iframe's height based on its content
  23. function updateIframeHeight() {
  24. const iframeDocument = iframe.contentWindow.document;
  25. const newHeight = iframeDocument.body.scrollHeight || 'auto';
  26. iframe.style.height = newHeight + 'px';
  27. }
  28.  
  29. // Attach the update function to iframe's load event and window's resize event
  30. iframe.onload = updateIframeHeight;
  31. window.addEventListener('resize', updateIframeHeight);
  32.  
  33. // Initialize the iframe height
  34. updateIframeHeight();
  35. }
  36.  
  37. // Call the initialization function when the document is ready
  38. document.addEventListener('DOMContentLoaded', initResizableIframe);
  39.  
  40.  
  41. $(document).ready(function() {
  42. let headerCount = 1;
  43.  
  44. // Add Header button click event
  45. $("#addHeaderButton").click(function() {
  46. // Create a new header/value/delete form
  47. var newRow = $('<div class="form-row align-items-center">' +
  48. '<div class="col-md-5 mb-3">' +
  49. '<label for="input_id_' + headerCount + '">Header</label>' +
  50. '<input type="text" class="form-control form-control-sm" name="header_name_' + headerCount + '" id="header_id_' + headerCount + '" placeholder="CC, BCC, Reply-To">' +
  51. '</div>' +
  52. '<div class="col-md-5 mb-3">' +
  53. '<label for="input_id_' + headerCount + '">Value</label>' +
  54. '<input type="text" class="form-control form-control-sm" name="header_value_' + headerCount + '" id="header_id_' + headerCount + '" placeholder="person@place.com">' +
  55. '</div>' +
  56. '<div class="col-md-2 mb-3">' +
  57. '<label for="input_id_' + headerCount + '"></label>' +
  58. '<button type="button" class="btn btn-danger btn-sm delete-button">Delete</button>' +
  59. '</div>' +
  60. '</div>');
  61.  
  62. // Append the new header/value/delete form to the form group
  63. $(".header-row").append(newRow);
  64. $("#headerCount").val(headerCount);
  65. // Increment the headerCount for the next set
  66. headerCount++;
  67. });
  68.  
  69. // Delete button click event
  70. $(document).on("click", ".delete-button", function() {
  71. // Remove the parent row when the delete button is clicked
  72. $(this).closest(".form-row").remove();// Decrement the headerCount when a row is deleted
  73. });
  74. });
  75.  
  76. // to display ile name when attachment is chosen
  77. $(document).ready(function () {
  78. // Add an event listener to the file input
  79. $('#attachment').change(function () {
  80. // Get the name of the selected file
  81. var fileName = $(this).val().split('\\').pop();
  82. // Update the label text with the file name
  83. $('.custom-file-label').html(fileName);
  84. });
  85. });
  86.  
  87.  
  88. let lastSubmittedData = null;
  89.  
  90. function submitForm() {
  91. // Create a FormData object to easily handle form data, including file uploads
  92. const formData = new FormData(document.getElementById('emailForm'));
  93.  
  94. // Get the headerCount value from the hidden input
  95. let headerCount = parseInt(document.getElementById('headerCount').value);
  96.  
  97. // Convert dynamic header inputs into an array of objects
  98. const headers = [];
  99. let newHeaderCount = 0;
  100.  
  101. for (let i = 1; i <= headerCount; i++) {
  102. const headerNameInput = document.querySelector(`[name="header_name_${i}"]`);
  103. const headerValueInput = document.querySelector(`[name="header_value_${i}"]`);
  104.  
  105. // Check if both headerNameInput and headerValueInput exist
  106. if (headerNameInput && headerValueInput) {
  107. const headerName = headerNameInput.value;
  108. const headerValue = headerValueInput.value;
  109.  
  110. // Check if both headerName and headerValue exist
  111. if (headerName && headerValue) {
  112. newHeaderCount++;
  113.  
  114. // Rename the input fields to reflect new IDs
  115. headerNameInput.name = `header_name_${newHeaderCount}`;
  116. headerValueInput.name = `header_value_${newHeaderCount}`;
  117.  
  118. headers.push({ name: headerNameInput.name, value: headerValueInput.name });
  119. }
  120. }
  121. }
  122.  
  123.  
  124. // Update the headerCount hidden input with the new count
  125. document.getElementById('headerCount').value = newHeaderCount;
  126. headerCount = parseInt(document.getElementById('headerCount').value);
  127.  
  128. // Add the headers as a JSON string to the FormData
  129. formData.append('headers', JSON.stringify(headers));
  130.  
  131. // Convert form data to a JSON string for comparison
  132. const currentData = JSON.stringify(Array.from(formData.entries()));
  133.  
  134. // Check if the form data has changed
  135. if (currentData === lastSubmittedData) {
  136. const resultElement = document.getElementById('result');
  137. resultElement.textContent = "Already tried to send this data";
  138. resultElement.style.color = "red";
  139. return;
  140. }
  141.  
  142. // Save the current form data for future comparison
  143. lastSubmittedData = currentData;
  144.  
  145. // Send the form data to "submit.php"
  146. fetch('submit.php', {
  147. method: 'POST',
  148. body: formData,
  149. })
  150. .then(response => response.text()) // Read the response as plain text
  151. .then(data => {
  152. // Handle the response from the server here
  153. const resultElement = document.getElementById('result');
  154. resultElement.textContent = data;
  155. if (data === "Success") {
  156. resultElement.style.color = "lightgreen";
  157. } else {
  158. resultElement.style.color = "red";
  159. }
  160. })
  161. .catch(error => {
  162. // Handle errors here
  163. console.error('Error:', error);
  164. });
  165. }
Buy Me A Coffee