From 04c420d255e1eb30234c151155ba53322ad2de22 Mon Sep 17 00:00:00 2001 From: fahed Date: Mon, 11 May 2026 13:30:20 +0300 Subject: [PATCH] fix: add test-email endpoint for SMTP diagnostics MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit POST /api/admin/test-email — superadmin only, returns exact SMTP host/port/error so we can diagnose why notifications aren't delivered. Co-Authored-By: Claude Sonnet 4.6 --- server/server.js | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/server/server.js b/server/server.js index cb7ae9a..8afa87b 100644 --- a/server/server.js +++ b/server/server.js @@ -640,6 +640,28 @@ app.get('/api/health', async (req, res) => { }); }); +// ─── EMAIL TEST ───────────────────────────────────────────────── + +app.post('/api/admin/test-email', requireAuth, async (req, res) => { + if (req.session.userRole !== 'superadmin') return res.status(403).json({ error: 'Superadmin only' }); + const to = req.body.to || req.session.userEmail; + if (!to) return res.status(400).json({ error: 'No recipient — pass { "to": "email@example.com" }' }); + const { sendMail, getSmtpConfig } = require('./mail'); + const config = getSmtpConfig(); + if (!config) return res.status(503).json({ error: 'SMTP not configured', env: { server: !!process.env.CLOUDRON_MAIL_SMTP_SERVER || !!process.env.MAIL_SMTP_SERVER } }); + try { + const info = await sendMail({ + to, + subject: 'Rawaj — Test Email', + html: '

If you received this, email delivery is working correctly.

', + text: 'If you received this, email delivery is working correctly.', + }); + res.json({ success: true, to, messageId: info?.messageId, smtp: { host: config.host, port: config.port, from: config.from } }); + } catch (err) { + res.status(500).json({ success: false, error: err.message, code: err.code, smtp: { host: config.host, port: config.port } }); + } +}); + // ─── SETUP ROUTES ─────────────────────────────────────────────── app.get('/api/setup/status', async (req, res) => {