Using Ajax to call a phpmail routine that sends out a series of emails (3 in this test) in a foreach loop when the SUBMIT button is clicked. Using the Ajax in ASYNC:FALSE mode, causes a several second delay (maybe 3 or 4 sec) until the mail routine completes, sends an "OK Sent" message back, then goes off to the new page.
WANT TO AVOID NOTICEABLE DELAY TO USER AND RUN MAILER IN BACKGROUND
Thinking to avoid the noticeable delay and have the mailer routine execute in the background, I use ASYNC:TRUE mode. Upon clicking SUBMIT, it just jumps to the header page but no email is sent out.
I was hoping that the phpmail routine would execute in the background, but it obviously gets destroyed once the page jumps to the header location.
Without the header, the phpmail routine works both in SYNC and ASYNC mode.
TRUE INTENT
What I would like to achieve is when a person publishes a document on the page, clicks the SUBMIT button, then subscribers immediately get notified via email that a new document has been published, and brings user to the next page - WITHOUT the user having to wait. Expected email list would be in the hundreds.
Using Apache on local machine and BLUEHOST as the real server.
What methods are there to achieving a trigger in code by a button press, pass an ID parameter, and have the phpmailer routine receive the data and process it in the background, while the user goes onto other parts of the site without having to wait? Solution without special add-ons would be nice...but if there is no other way, OK!
My code below if useful:
$(document).ready(function(){
//PUBLISH button clicked, so send email to subscribers of new questions
$(document).on('click', '#submit', function(){
var question_id = '<?php echo $question_id; ?>';
console.log('question_id is: '+question_id);
$.ajax({
type: "POST",
url: '/modules/posts/TEST_questions_new_send_mail.php',
async : false,//true does not execute the email loop, just jumps to the header below
data: { "action":'new_question_subscribers',
"question_id":question_id},
success: function(data) {
console.log(data);
},
error: function() {
alert('Ajax Failed');
}
});
window.location.assign('https://q.localhost/modules/posts/questions.php');
});
});