import dotenv from 'dotenv'; import nodemailer from 'nodemailer'; import path from 'path'; // Load environment variables from .env dotenv.config({ path: path.resolve(__dirname, '../.env') }); async function main() { const host = process.env.SMTP_HOST || 'localhost'; const port = parseInt(process.env.SMTP_PORT || '587', 10); const user = process.env.SMTP_USER || ''; const pass = process.env.SMTP_PASS || ''; const from = process.env.SMTP_FROM || user; const enableReal = process.env.ENABLE_REAL_EMAILS; const nodeEnv = process.env.NODE_ENV; console.log('=================================================='); console.log(' SMTP CREDENTIALS & SERVICE TEST '); console.log('=================================================='); console.log(`• NODE_ENV : ${nodeEnv || '(not set)'}`); console.log(`• ENABLE_REAL_EMAILS : ${enableReal || '(not set)'}`); console.log(`• SMTP_HOST : ${host}`); console.log(`• SMTP_PORT : ${port}`); console.log(`• SMTP_USER : ${user || '(empty)'}`); console.log(`• SMTP_PASS : ${pass ? '********' : '(empty)'}`); console.log(`• SMTP_FROM : ${from}`); console.log('--------------------------------------------------'); if (enableReal !== 'true') { console.warn('⚠️ WARNING: ENABLE_REAL_EMAILS is not set to "true". Real email dispatch is disabled in application code.'); } const transporter = nodemailer.createTransport({ host, port, secure: port === 465, auth: user && pass ? { user, pass } : undefined, tls: { rejectUnauthorized: false, }, }); console.log('\n[1/2] Verifying SMTP connection & authentication credentials...'); try { const verified = await transporter.verify(); console.log('✅ SUCCESS: SMTP Server is reachable and authentication credentials are VALID!'); } catch (err: any) { console.error('❌ FAILED: SMTP Connection or Authentication failed!'); console.error(`Reason: ${err.message || err}`); if (err.code === 'ECONNREFUSED') { console.error(`👉 Suggestion: Port ${port} is blocked or not accepting connections on ${host}. Check firewall rules.`); } else if (err.responseCode === 535 || err.code === 'EAUTH') { console.error('👉 Suggestion: Invalid SMTP_USER or SMTP_PASS.'); } process.exit(1); } // Optional: If an email argument is provided, send a test email const recipient = process.argv[2]; if (recipient) { console.log(`\n[2/2] Sending test email to: ${recipient}...`); try { const info = await transporter.sendMail({ from: from || user, to: recipient, subject: 'Tech4Biz Channel Partner SMTP Verification', text: 'This is a test email sent from the Tech4Biz SMTP Test Script.', html: '
Your backend SMTP configuration is working properly.