function accSendEmail(senderName, senderEmail, subject, message, receiverId, executionCallbackMethod, errorCallbackMethod){ if(isInputValid(senderName, senderEmail, subject, message, receiverId)){ // API URL for sending the email // const apiUrl = 'https://acceleratron.in/email/sendemail'; const apiUrl = 'https://acceleratron.in/email/sendemail'; message=convertToParagraphs(message); const requestBody = { 'senderName': senderName, 'senderEmail': senderEmail, 'subject': subject, 'message': message, 'receiverId': receiverId }; // Make the API call using fetch fetch(apiUrl, { method: 'POST', headers: { 'Content-Type': 'application/json' // Add any other headers if needed (e.g., authorization token) }, body: JSON.stringify(requestBody) // Convert the JSON object to a string }) .then(response => { if (!response.ok) { throw new Error(`HTTP error! Status: ${response.status}`); } return response.json(); }) .then(data => { // Handle the API response data console.log('API response:', data); if (typeof executionCallbackMethod === "function") { executionCallbackMethod(data); }else{ console.log('Execution callback is not available!'); } }) .catch(error => { // Handle errors during the API call console.error('Error:', error); if (typeof errorCallbackMethod === "function") { errorCallbackMethod(error); }else{ console.log('Error callback is not available!'); } }); } else{ if (typeof errorCallbackMethod === "function") { errorCallbackMethod(new Error(`Data Validation Error!`)); }else{ console.log('Error callback is not available!'); } } } function isInputValid(senderName, senderEmail, subject, message, receiverId){ if(!requiredFieldValidation("senderName",senderName) || !requiredFieldValidation("senderEmail",senderEmail) || !requiredFieldValidation("subject", subject) || !requiredFieldValidation("message",message)){ console.error("All mandatory fields are not available!"); return false; }else if(!requiredFieldValidation("receiverId",receiverId)){ console.error("Registered user id for email service is not provided!"); return false; }else if(!validateEmail(senderEmail)){ console.error("Email is not valid!"); return false; }else { return true; } } function requiredFieldValidation(fieldName, fieldVal) { if ( fieldVal!= null && fieldVal!= "undefined" && fieldVal.trim().length > 0 ) { return true; } else { console.error("fieldName: "+fieldName+" fieldVal: "+fieldVal); return false; } } function validateEmail(email) { var expr = /^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/ return expr.test(email) } function convertToParagraphs(text) { // Handle special characters by encoding the text var encodedText = encodeURIComponent(text); // Replace tabs with a specified number of spaces (e.g., 4 spaces) var spacesPerTab = 4; var spaces = ' '.repeat(spacesPerTab); var indentedText = encodedText.replace(/\t/g, spaces); // Replace newline characters with HTML paragraphs var paragraphs = indentedText.replace(/(?:\r\n|\r|\n)/g, '

'); // Wrap the content with paragraph tags return '

' + paragraphs + '

'; }