diff --git a/backend/src/routes/adminEmail.js b/backend/src/routes/adminEmail.js index acc37156..d880b884 100644 --- a/backend/src/routes/adminEmail.js +++ b/backend/src/routes/adminEmail.js @@ -94,6 +94,16 @@ router.post('/config', [ await db('email_configs').insert(configData); } + // Refresh the cached transporter so the new SMTP settings take effect + // immediately. Without this, a previously-initialised transporter stays + // cached (the queue processor only re-inits when it's null), so changing + // the email account had no effect until a backend restart — emails kept + // failing against the old/empty config. initializeTransporter catches its + // own errors and returns null, so this never throws; an invalid config + // simply leaves the transporter null (surfaced via the Test-email button). + const { initializeTransporter } = require('../services/emailProcessor'); + await initializeTransporter(true); + // Log activity await logActivity('email_config_updated', { smtp_host, from_email }, diff --git a/backend/src/services/emailProcessor.js b/backend/src/services/emailProcessor.js index 6b6308d3..0e9b2412 100644 --- a/backend/src/services/emailProcessor.js +++ b/backend/src/services/emailProcessor.js @@ -774,13 +774,18 @@ async function processEmailQueue({ ignoreSchedule = false, limit = 10 } = {}) { // queue split-payment emails relative to the event date. const now = new Date(); const query = db('email_queue') - .where('status', 'pending') - .where('retry_count', '<', 3); + .where('status', 'pending'); if (!ignoreSchedule) { - query.andWhere(function() { + // Automatic runs: respect the retry cap (don't hammer a failing + // address) AND the schedule (business-hours floor / future send). + query.where('retry_count', '<', 3).andWhere(function() { this.whereNull('scheduled_at').orWhere('scheduled_at', '<=', now); }); } + // A manual "send now" (ignoreSchedule) deliberately bypasses BOTH the + // schedule and the retry cap: the admin is forcing a retry, typically + // right after fixing SMTP. Without this, emails that failed 3× during + // an SMTP outage are stuck "pending" forever with no way to resend. pendingEmails = await query .orderBy('scheduled_at', 'asc') .orderBy('created_at', 'asc')