Files
PIKKIE/server/src/mail.js
T
juli d24415ecf9 PIKKIE: picky issues keeper & knitpick ideas explorer
Self-hosted beta-tester issue tracker with a Kotlin Android companion.

- Bullet-point reports (classic and rapid fire), .txt/.md import,
  evidence by paste, drag, camera or gallery
- Projects, calibers (.22 -> 12 gauge) as weights, public overview
- Threaded discussions, "me too", admin flagging with messages and files
- Board with polls and an off-topic image board with link previews
- Profiles with avatars, in-app inbox, opt-in e-mail and phone alerts
- Invites, optional open sign-up, TOTP 2FA with admin policy
- MCP endpoint with per-user API keys
- QR login both ways, built for an HTTPS reverse proxy
- Encryption at rest for 2FA secrets and the SMTP password
- Android app with full tester/admin parity, in-app updates, no Google
  services required
- Interactive first-run tutorial
2026-11-10 23:51:30 +01:00

59 lines
2.6 KiB
JavaScript

import nodemailer from 'nodemailer';
import { getSetting } from './db.js';
import { getSmtpPassword } from './secrets.js';
const esc = (s) => String(s ?? '').replace(/[&<>"']/g, (c) => ({ '&': '&amp;', '<': '&lt;', '>': '&gt;', '"': '&quot;', "'": '&#39;' })[c]);
export function smtpConfigured() {
return !!getSetting('smtp_host');
}
function transport() {
const port = parseInt(getSetting('smtp_port'), 10) || 587;
const user = getSetting('smtp_user');
const host = getSetting('smtp_host');
const secure = getSetting('smtp_secure') === '1';
const local = /^(localhost|127\.\d+\.\d+\.\d+|::1|\[::1\])$/i.test(host);
return nodemailer.createTransport({
host,
port,
secure,
// Without this, STARTTLS is only opportunistic and a network attacker can strip it and read the SMTP
// password. When we log in to a remote server we insist on TLS (a local relay on this machine is exempt).
requireTLS: !secure && !!user && !local,
auth: user ? { user, pass: getSmtpPassword() } : undefined,
connectionTimeout: 10_000,
greetingTimeout: 10_000,
socketTimeout: 20_000,
});
}
export function wrapHtml(title, bodyHtml, link) {
return `<!doctype html><html><body style="margin:0;background:#1c1c1f;font-family:Helvetica,Arial,sans-serif">
<div style="max-width:560px;margin:0 auto;padding:24px">
<div style="font-family:Impact,'Arial Black',sans-serif;font-size:28px;letter-spacing:2px;color:#ff2e93;transform:rotate(-2deg)">PIKKIE<span style="color:#fff">.</span></div>
<div style="background:#26262b;border:2px solid #ff2e93;border-radius:6px;padding:20px;margin-top:14px;color:#f4f4f5">
<h2 style="margin:0 0 12px;color:#fff">${esc(title)}</h2>${bodyHtml}
${link ? `<p style="margin-top:18px"><a href="${esc(link)}" style="background:#ff2e93;color:#fff;text-decoration:none;font-weight:bold;padding:10px 16px;border-radius:4px;display:inline-block">Open in PIKKIE</a></p>` : ''}
</div><p style="color:#888;font-size:12px">You get this because you are a PIKKIE beta tester. Turn mails off on your profile page.</p></div></body></html>`;
}
export async function sendMail({ to, subject, text, html }) {
if (!smtpConfigured()) return { ok: false, error: 'SMTP not configured' };
try {
await transport().sendMail({ from: getSetting('smtp_from'), to, subject, text, html });
return { ok: true };
} catch (e) {
console.error('[mail] failed:', e.message);
return { ok: false, error: e.message };
}
}
/** fire-and-forget */
export function queueMail(msg) {
if (!smtpConfigured() || !msg.to || (Array.isArray(msg.to) && !msg.to.length)) return;
sendMail(msg).catch(() => {});
}
export const e = esc;