| [834bcc3] | 1 | // Send email (password reset, newsletter, notify). Optional: only active
|
|---|
| 2 | // when SMTP is configured — via Admin → Settings (app_settings) OR env vars.
|
|---|
| [9e27d64] | 3 | //
|
|---|
| [834bcc3] | 4 | // Config source (in this order): app_settings (set via the UI), otherwise env:
|
|---|
| [ca507ef] | 5 | // SMTP_HOST, SMTP_PORT (default 587), SMTP_USER, SMTP_PASS, SMTP_FROM (default = USER)
|
|---|
| [834bcc3] | 6 | // Not configured → sending falls back to CLI (reset-admin) / is skipped.
|
|---|
| [9e27d64] | 7 |
|
|---|
| 8 | import nodemailer from 'nodemailer';
|
|---|
| [ca507ef] | 9 | import { getSetting } from '../services/SettingsService.js';
|
|---|
| [9e27d64] | 10 |
|
|---|
| [ca507ef] | 11 | function cfg() {
|
|---|
| 12 | const host = getSetting('smtp_host', '') || process.env.SMTP_HOST || '';
|
|---|
| 13 | const port = parseInt(getSetting('smtp_port', '') || process.env.SMTP_PORT || '587', 10) || 587;
|
|---|
| 14 | const user = getSetting('smtp_user', '') || process.env.SMTP_USER || '';
|
|---|
| 15 | const pass = getSetting('smtp_pass', '') || process.env.SMTP_PASS || '';
|
|---|
| 16 | const from = getSetting('smtp_from', '') || process.env.SMTP_FROM || user;
|
|---|
| 17 | return { host, port, user, pass, from };
|
|---|
| 18 | }
|
|---|
| [9e27d64] | 19 |
|
|---|
| 20 | export function mailerConfigured() {
|
|---|
| [ca507ef] | 21 | const c = cfg();
|
|---|
| 22 | return !!(c.host && c.user && c.pass);
|
|---|
| 23 | }
|
|---|
| 24 |
|
|---|
| [834bcc3] | 25 | // Status for the UI (without leaking the password).
|
|---|
| [ca507ef] | 26 | export function mailerStatus() {
|
|---|
| 27 | const c = cfg();
|
|---|
| 28 | return {
|
|---|
| 29 | configured: mailerConfigured(),
|
|---|
| 30 | host: c.host,
|
|---|
| 31 | port: c.port,
|
|---|
| 32 | user: c.user,
|
|---|
| 33 | from: c.from,
|
|---|
| 34 | passSet: !!c.pass,
|
|---|
| [834bcc3] | 35 | // source: useful to show that env vars are still active
|
|---|
| [ca507ef] | 36 | fromEnv: !getSetting('smtp_host', '') && !!process.env.SMTP_HOST,
|
|---|
| 37 | };
|
|---|
| [9e27d64] | 38 | }
|
|---|
| 39 |
|
|---|
| [834bcc3] | 40 | // Cache the transport, but rebuild it whenever the config changes (UI edit without restart).
|
|---|
| [ca507ef] | 41 | let _transport = null, _key = null;
|
|---|
| [9e27d64] | 42 | function transport() {
|
|---|
| [ca507ef] | 43 | const c = cfg();
|
|---|
| 44 | const key = [c.host, c.port, c.user, c.pass].join('|');
|
|---|
| 45 | if (!_transport || _key !== key) {
|
|---|
| [9e27d64] | 46 | _transport = nodemailer.createTransport({
|
|---|
| [ca507ef] | 47 | host: c.host,
|
|---|
| 48 | port: c.port,
|
|---|
| [834bcc3] | 49 | secure: c.port === 465, // 465 = implicit TLS; 587 = STARTTLS
|
|---|
| [ca507ef] | 50 | auth: { user: c.user, pass: c.pass },
|
|---|
| [9e27d64] | 51 | });
|
|---|
| [ca507ef] | 52 | _key = key;
|
|---|
| [9e27d64] | 53 | }
|
|---|
| 54 | return _transport;
|
|---|
| 55 | }
|
|---|
| 56 |
|
|---|
| 57 | export async function sendMail({ to, subject, text, html }) {
|
|---|
| [834bcc3] | 58 | if (!mailerConfigured()) throw new Error('SMTP not configured');
|
|---|
| [ca507ef] | 59 | const c = cfg();
|
|---|
| 60 | return transport().sendMail({ from: c.from, to, subject, text, html });
|
|---|
| [9e27d64] | 61 | }
|
|---|