88 lines
3.6 KiB
TypeScript
88 lines
3.6 KiB
TypeScript
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: '<div style="font-family: sans-serif; padding: 20px; border: 1px solid #ccc;"><h3>SMTP Test Successful!</h3><p>Your backend SMTP configuration is working properly.</p></div>',
|
||
});
|
||
console.log(`✅ SUCCESS: Email delivered successfully to ${recipient}!`);
|
||
console.log(`• Message ID: ${info.messageId}`);
|
||
} catch (err: any) {
|
||
console.error(`❌ FAILED: Could not send test email to ${recipient}.`);
|
||
console.error(`Reason: ${err.message || err}`);
|
||
}
|
||
} else {
|
||
console.log('\nℹ️ To send an actual test email, run:');
|
||
console.log(' npx ts-node scripts/test-smtp.ts <your-email-address>');
|
||
}
|
||
console.log('==================================================\n');
|
||
}
|
||
|
||
main().catch((err) => {
|
||
console.error('Unexpected error:', err);
|
||
process.exit(1);
|
||
});
|