mirror of
https://github.com/Komediruzecki/token-circles.git
synced 2026-10-20 09:46:43 +02:00
- billsRepo.markPaid: write last_paid_date instead of last_paid - bills route: read last_paid_date consistently - tax.js: fix quarter endDate (was hardcoded -31, now computes last day) - tax.js: store estimates in Map, GET :id returns real stored estimate - profilesRepo: add missing tables to deleteAllDataForProfile - emailService: remove dead transporter.from assignment - transactions: clamp LIMIT, validate OFFSET against NaN - tagsRepo: validate limit/offset against NaN - reports: add requireAuth to all financial report endpoints
66 lines
1.9 KiB
JavaScript
66 lines
1.9 KiB
JavaScript
const nodemailer = require('nodemailer');
|
|
|
|
let transporter = null;
|
|
let fromAddress = '';
|
|
|
|
function getTransporter() {
|
|
if (transporter) return transporter;
|
|
|
|
const host = process.env.SMTP_HOST || 'localhost';
|
|
const port = parseInt(process.env.SMTP_PORT || '587');
|
|
const user = process.env.SMTP_USER || '';
|
|
const pass = process.env.SMTP_PASS || '';
|
|
fromAddress = process.env.SMTP_FROM || 'finance-manager@localhost';
|
|
|
|
if (user && pass) {
|
|
transporter = nodemailer.createTransport({
|
|
host,
|
|
port,
|
|
secure: port === 465,
|
|
auth: { user, pass },
|
|
});
|
|
} else {
|
|
transporter = nodemailer.createTransport({
|
|
host,
|
|
port,
|
|
secure: false,
|
|
});
|
|
}
|
|
|
|
if (process.env.SMTP_HOST) {
|
|
console.log(`[email] SMTP configured: ${host}:${port}${user ? ' (auth)' : ''}, from: ${fromAddress}`);
|
|
} else {
|
|
console.log('[email] No SMTP_HOST set — emails will be logged to console only');
|
|
}
|
|
|
|
return transporter;
|
|
}
|
|
|
|
async function sendMail(to, subject, html) {
|
|
if (!to) {
|
|
console.warn('[email] Skipping: no recipient address');
|
|
return { skipped: true, reason: 'no recipient' };
|
|
}
|
|
|
|
const transport = getTransporter();
|
|
|
|
// Dev mode: log to console when no SMTP configured
|
|
if (!process.env.SMTP_HOST) {
|
|
console.log(`[email] DEV — To: ${to}`);
|
|
console.log(`[email] DEV — Subject: ${subject}`);
|
|
console.log(`[email] DEV — Body: ${html.substring(0, 200)}...`);
|
|
return { skipped: true, reason: 'no SMTP_HOST configured (dev mode)' };
|
|
}
|
|
|
|
try {
|
|
const info = await transport.sendMail({ from: fromAddress, to, subject, html });
|
|
console.log(`[email] Sent to ${to}: ${info.messageId}`);
|
|
return { sent: true, messageId: info.messageId };
|
|
} catch (err) {
|
|
console.error(`[email] Failed to send to ${to}:`, err.message);
|
|
return { sent: false, error: err.message };
|
|
}
|
|
}
|
|
|
|
module.exports = { sendMail };
|