| 1 | // E-mail versturen (wachtwoord-reset, nieuwsbrief, notify). Optioneel: alleen actief
|
|---|
| 2 | // als SMTP is ingesteld — via Beheer → Instellingen (app_settings) OF env-vars.
|
|---|
| 3 | //
|
|---|
| 4 | // Config-bron (in deze volgorde): app_settings (ingesteld in de UI), anders env:
|
|---|
| 5 | // SMTP_HOST, SMTP_PORT (default 587), SMTP_USER, SMTP_PASS, SMTP_FROM (default = USER)
|
|---|
| 6 | // Niet ingesteld → versturen valt terug op CLI (reset-admin) / wordt overgeslagen.
|
|---|
| 7 |
|
|---|
| 8 | import nodemailer from 'nodemailer';
|
|---|
| 9 | import { getSetting } from '../services/SettingsService.js';
|
|---|
| 10 |
|
|---|
| 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 | }
|
|---|
| 19 |
|
|---|
| 20 | export function mailerConfigured() {
|
|---|
| 21 | const c = cfg();
|
|---|
| 22 | return !!(c.host && c.user && c.pass);
|
|---|
| 23 | }
|
|---|
| 24 |
|
|---|
| 25 | // Status voor de UI (zonder het wachtwoord te lekken).
|
|---|
| 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,
|
|---|
| 35 | // bron: handig om te tonen dat env nog actief is
|
|---|
| 36 | fromEnv: !getSetting('smtp_host', '') && !!process.env.SMTP_HOST,
|
|---|
| 37 | };
|
|---|
| 38 | }
|
|---|
| 39 |
|
|---|
| 40 | // Transport cachen, maar herbouwen zodra de config wijzigt (UI-edit zonder herstart).
|
|---|
| 41 | let _transport = null, _key = null;
|
|---|
| 42 | function transport() {
|
|---|
| 43 | const c = cfg();
|
|---|
| 44 | const key = [c.host, c.port, c.user, c.pass].join('|');
|
|---|
| 45 | if (!_transport || _key !== key) {
|
|---|
| 46 | _transport = nodemailer.createTransport({
|
|---|
| 47 | host: c.host,
|
|---|
| 48 | port: c.port,
|
|---|
| 49 | secure: c.port === 465, // 465 = impliciete TLS; 587 = STARTTLS
|
|---|
| 50 | auth: { user: c.user, pass: c.pass },
|
|---|
| 51 | });
|
|---|
| 52 | _key = key;
|
|---|
| 53 | }
|
|---|
| 54 | return _transport;
|
|---|
| 55 | }
|
|---|
| 56 |
|
|---|
| 57 | export async function sendMail({ to, subject, text, html }) {
|
|---|
| 58 | if (!mailerConfigured()) throw new Error('SMTP niet geconfigureerd');
|
|---|
| 59 | const c = cfg();
|
|---|
| 60 | return transport().sendMail({ from: c.from, to, subject, text, html });
|
|---|
| 61 | }
|
|---|