110 lines
3.8 KiB
TypeScript
110 lines
3.8 KiB
TypeScript
/**
|
|
* Verify SMTP credentials and print the resolved From / Reply-To headers.
|
|
*
|
|
* From repo root:
|
|
* npm run phase-b:verify-smtp
|
|
*
|
|
* Optional test send (does not log the password):
|
|
* SMTP_TEST_TO=you@example.com npm run phase-b:verify-smtp
|
|
*/
|
|
|
|
import nodemailer from 'nodemailer';
|
|
import { config } from '../config';
|
|
import {
|
|
isSmtpConfigured,
|
|
resolveFromAddress,
|
|
resolveReplyToAddress,
|
|
verifySmtpConnection,
|
|
normalizeSmtpPass,
|
|
} from '../services/emailService';
|
|
|
|
async function main(): Promise<void> {
|
|
console.log('Email transport:', config.email.transport);
|
|
if (config.email.transport !== 'smtp') {
|
|
console.error(
|
|
'SMTP is not active. Set EMAIL_TRANSPORT=smtp and SMTP_HOST, SMTP_USER, SMTP_PASS, EMAIL_FROM in .env'
|
|
);
|
|
process.exit(1);
|
|
}
|
|
if (!isSmtpConfigured()) {
|
|
console.error('Missing SMTP_HOST, SMTP_USER, SMTP_PASS, or EMAIL_FROM.');
|
|
process.exit(1);
|
|
}
|
|
|
|
const from = resolveFromAddress();
|
|
const replyTo = resolveReplyToAddress();
|
|
|
|
console.log('SMTP host:', config.email.host);
|
|
console.log('SMTP port:', config.email.port);
|
|
console.log('SMTP user (auth):', config.email.user);
|
|
console.log('EMAIL_FROM (env):', config.email.from);
|
|
console.log('Resolved From:', from);
|
|
console.log('Resolved Reply-To:', replyTo || '(none)');
|
|
|
|
await verifySmtpConnection();
|
|
console.log('SMTP connection OK — credentials accepted by the mail server.');
|
|
|
|
const testTo = (process.env.SMTP_TEST_TO || '').trim();
|
|
if (testTo) {
|
|
const transport = nodemailer.createTransport({
|
|
host: config.email.host,
|
|
port: config.email.port,
|
|
secure: config.email.port === 465 ? true : config.email.secure,
|
|
requireTLS: config.email.port === 587,
|
|
auth: {
|
|
user: config.email.user,
|
|
pass: normalizeSmtpPass(config.email.pass),
|
|
},
|
|
});
|
|
const info = await transport.sendMail({
|
|
from,
|
|
replyTo,
|
|
to: testTo,
|
|
subject: 'Tloto SMTP test',
|
|
text: [
|
|
'This is a test message from Tloto.',
|
|
`Auth user: ${config.email.user}`,
|
|
`From header: ${from}`,
|
|
`Reply-To: ${replyTo || '(none)'}`,
|
|
`Time: ${new Date().toISOString()}`,
|
|
].join('\n'),
|
|
});
|
|
console.log('Test message accepted by SMTP. messageId:', info.messageId);
|
|
console.log('Check the inbox for', testTo, '— From should show info@tloto.net (or your EMAIL_FROM).');
|
|
} else {
|
|
console.log('Tip: set SMTP_TEST_TO=you@example.com to send a real test message.');
|
|
}
|
|
|
|
process.exit(0);
|
|
}
|
|
|
|
main().catch((e) => {
|
|
const msg = e instanceof Error ? e.message : String(e);
|
|
console.error('SMTP verification failed:', msg);
|
|
if (
|
|
msg.includes('SmtpClientAuthentication is disabled') ||
|
|
msg.includes('smtp_auth_disabled')
|
|
) {
|
|
console.error(
|
|
'\nMicrosoft 365 blocked SMTP AUTH for this tenant/mailbox.\n' +
|
|
' Enable Authenticated SMTP for admin@tloto.net (M365 admin → user → Mail → Email apps),\n' +
|
|
' or see https://aka.ms/smtp_auth_disabled — then retry verify-smtp.\n' +
|
|
' If MFA is on, use a Microsoft app password in SMTP_PASS.'
|
|
);
|
|
} else if (msg.includes('535') || msg.includes('BadCredentials') || msg.includes('Authentication unsuccessful')) {
|
|
console.error(
|
|
'\nAuth rejected. Tloto uses Microsoft 365 / GoDaddy only (not Gmail):\n' +
|
|
' SMTP_HOST=smtp.office365.com SMTP_PORT=587 SMTP_SECURE=false\n' +
|
|
' SMTP_USER=admin@tloto.net (mailbox password or app password)\n' +
|
|
' EMAIL_FROM="Tloto <info@tloto.net>" (alias / Send As)\n' +
|
|
' EMAIL_REPLY_TO=info@tloto.net'
|
|
);
|
|
}
|
|
if (msg.includes('SendAsDenied') || msg.includes('not allowed to send') || msg.includes('550 5.7')) {
|
|
console.error(
|
|
'\nServer rejected From address. Grant “Send As” / confirm info@tloto.net is an alias of admin@tloto.net.'
|
|
);
|
|
}
|
|
process.exit(1);
|
|
});
|