"use strict"; /** * Outbound email delivery for verification and transactional messages. * Supports SMTP (production) and console logging (development). */ var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.normalizeSmtpPass = normalizeSmtpPass; exports.isSmtpConfigured = isSmtpConfigured; exports.verifySmtpConnection = verifySmtpConnection; exports.sendVerificationEmail = sendVerificationEmail; exports.sendPasswordResetEmail = sendPasswordResetEmail; exports.sendSecurityAlertEmail = sendSecurityAlertEmail; const nodemailer_1 = __importDefault(require("nodemailer")); const config_1 = require("../config"); let transporter = null; /** Gmail app passwords are often copied with spaces — strip them. */ function normalizeSmtpPass(pass) { return pass.replace(/\s+/g, ''); } function isGmailHost(host) { const h = host.toLowerCase(); return h.includes('gmail.com') || h.includes('googlemail.com'); } /** * Returns true when SMTP is fully configured for real email delivery. */ function isSmtpConfigured() { const { host, user, pass, from } = config_1.config.email; return !!(host && user && pass && from); } function buildTransportOptions() { const host = config_1.config.email.host; const port = config_1.config.email.port; const user = config_1.config.email.user.trim(); const pass = normalizeSmtpPass(config_1.config.email.pass); const gmail = isGmailHost(host); return { host, port, // Port 465 = implicit TLS; 587 = STARTTLS (Gmail default). secure: port === 465 ? true : config_1.config.email.secure, requireTLS: port === 587, auth: { user, pass }, ...(gmail ? { tls: { minVersion: 'TLSv1.2' } } : {}), }; } /** * Lazily creates the nodemailer SMTP transporter. * @throws When transport is smtp but required env vars are missing */ function getSmtpTransporter() { if (transporter) return transporter; if (!isSmtpConfigured()) { throw new Error('SMTP is not configured. Set SMTP_HOST, SMTP_USER, SMTP_PASS, and EMAIL_FROM.'); } transporter = nodemailer_1.default.createTransport(buildTransportOptions()); return transporter; } /** Verifies SMTP credentials (call on deploy or via CLI). */ async function verifySmtpConnection() { const transport = getSmtpTransporter(); await transport.verify(); } function formatEmailSendError(context, e) { const detail = e instanceof Error ? e.message : String(e); console.error(`[email] ${context} SMTP send failed:`, detail); if (config_1.config.env === 'production') { return new Error('Unable to send email at the moment. Please try again in a few minutes or contact support.'); } return new Error(`Failed to send ${context}: ${detail}`); } function resolveFromAddress() { const from = config_1.config.email.from; const user = config_1.config.email.user; if (!user) return from; const fromEmail = from.match(/<([^>]+)>/)?.[1] ?? from.trim(); const host = config_1.config.email.host.toLowerCase(); const usesGmail = host.includes('gmail.com') || host.includes('googlemail.com'); if (usesGmail && user.endsWith('@gmail.com') && !fromEmail.endsWith('@gmail.com')) { return `"Prediqo" <${user}>`; } return from; } /** * @param to - Recipient email address * @param verifyUrl - Full URL opened by the user to confirm ownership */ async function sendVerificationEmail(to, verifyUrl) { const subject = 'Verify your Prediqo email address'; const text = [ 'Welcome to Prediqo!', '', 'Please verify your email address by opening this link:', verifyUrl, '', `This link expires in ${config_1.config.email.verificationExpiryHours} hours.`, 'If you did not create an account, you can ignore this email.', ].join('\n'); const html = `
Thanks for joining Prediqo. Confirm your email address to secure your account.
Or copy this link:
${verifyUrl}
Link expires in ${config_1.config.email.verificationExpiryHours} hours.
We received a request to reset the password for your Prediqo account.
Or copy this link:
${resetUrl}
Link expires in ${config_1.config.email.passwordResetExpiryHours} hour(s). If you did not request this, ignore this email.
${alert.title}
${alert.details || ''}