fixed progress email send

This commit is contained in:
2026-10-05 15:37:38 +02:00
parent 7c1e76e8cd
commit 075628f5a9
2 changed files with 104 additions and 32 deletions
+98 -25
View File
@@ -907,11 +907,23 @@ Ti ringraziamo per la collaborazione e per l’attenzione alla sicurezza di tutt
});
});
// Invio effettivo email
// Invio effettivo email: a gruppi, con barra di avanzamento
var invioInCorso = false;
window.addEventListener('beforeunload', function(e) {
if (invioInCorso) {
e.preventDefault();
e.returnValue = '';
}
});
$('#sendEmailBtn').on('click', function() {
if (invioInCorso) return;
var subject = $('#emailSubject').val(),
body = $('#emailBody').val();
var recipients = $('#emailRecipients').val();
var recipients = JSON.parse($('#emailRecipients').val() || '[]');
if (!subject || !body) {
alertBox({
title: 'Errore',
@@ -920,31 +932,92 @@ Ti ringraziamo per la collaborazione e per l’attenzione alla sicurezza di tutt
});
return;
}
$.post('send_email.php', {
subject: subject,
body: body,
recipients: recipients
}, null, 'json')
.done(function(resp) {
if (resp.success) {
alertBox({
title: 'Fatto',
text: resp.message,
icon: 'success'
});
$('#emailModal').modal('hide');
} else alertBox({
title: 'Errore',
text: resp.message,
icon: 'error'
});
})
.fail((xhr) => alertBox({
if (recipients.length === 0) {
alertBox({
title: 'Errore',
text: 'Errore durante l\'invio: ' + (xhr.responseText || xhr.statusText || 'nessuna risposta').substring(0, 400),
text: 'Nessun destinatario.',
icon: 'error'
}));
});
return;
}
invioInCorso = true;
$('#sendEmailBtn').prop('disabled', true);
$('#emailModal').modal('hide');
var BATCH = 5; // email per ogni richiesta
var total = recipients.length,
done = 0,
sent = 0,
failed = [];
alertBox({
title: 'Invio in corso…',
html: '<p id="sendProgressText">0 di ' + total + '</p>' +
'<div class="progress" style="height:22px;">' +
'<div id="sendProgressBar" class="progress-bar progress-bar-striped progress-bar-animated" style="width:0%">0%</div>' +
'</div>' +
'<p class="mt-3 mb-0 text-muted" style="font-size:13px;">Non chiudere questa pagina finché l\'invio non è terminato.</p>',
allowOutsideClick: false,
allowEscapeKey: false,
showConfirmButton: false
});
function aggiorna() {
var pct = Math.round(done / total * 100);
$('#sendProgressBar').css('width', pct + '%').text(pct + '%');
$('#sendProgressText').text(done + ' di ' + total);
}
function fine() {
invioInCorso = false;
$('#sendEmailBtn').prop('disabled', false);
var html = 'Email inviate: <strong>' + sent + '</strong> su ' + total + '.';
if (failed.length) {
html += '<br><br>Non inviate (' + failed.length + '):<br>' +
$('<div>').text(failed.join(', ')).html();
}
alertBox({
title: failed.length ? 'Invio completato con errori' : 'Invio completato',
html: html,
icon: failed.length ? 'warning' : 'success'
});
}
function inviaGruppo(start) {
if (start >= total) {
fine();
return;
}
var gruppo = recipients.slice(start, start + BATCH);
$.post('send_email.php', {
subject: subject,
body: body,
recipients: JSON.stringify(gruppo)
}, null, 'json')
.done(function(resp) {
if (resp && resp.success) {
sent += resp.sent || 0;
failed = failed.concat(resp.failed || []);
} else {
failed = failed.concat(gruppo.map(function(r) {
return r.email;
}));
}
})
.fail(function() {
failed = failed.concat(gruppo.map(function(r) {
return r.email;
}));
})
.always(function() {
done += gruppo.length;
aggiorna();
inviaGruppo(start + BATCH);
});
}
inviaGruppo(0);
});
});
</script>