feat(mail): add SMTP mail-sending module (Gitea #6, GDPR export prep)
Thin nodemailer wrapper reading SMTP config from env vars. Uses a provider-hosted mailbox (Strato, same as the domain host) rather than a self-hosted relay from the NAS's residential/dynamic IP, which would get spam-flagged by most receiving servers regardless of DKIM/SPF. Foundation for the GDPR Art. 15 data-export feature (Plan B: expiring download link + email notification) — no caller wired up yet. Verified end-to-end with a real test send before committing.
This commit is contained in:
1 parent
328e54c68e
commit
86ac5f4fee
4 files changed
+81
No files matched your search
@@ -0,0 +1,48 @@
|
||||
import "server-only";
|
||||
import nodemailer, { type Transporter } from "nodemailer";
|
||||
|
||||
/**
|
||||
* SMTP mail sender — same-provider mailbox as the domain host (Strato),
|
||||
* not self-hosted. A self-hosted mail relay from the NAS's residential/
|
||||
* dynamic IP would be flagged as spam by most receiving servers regardless
|
||||
* of DKIM/SPF, so this deliberately goes through the domain provider's
|
||||
* mail infrastructure instead (see Gitea #6 discussion).
|
||||
*/
|
||||
|
||||
let transporter: Transporter | null = null;
|
||||
|
||||
function getTransporter(): Transporter {
|
||||
if (transporter) return transporter;
|
||||
|
||||
const host = process.env.SMTP_HOST;
|
||||
const port = process.env.SMTP_PORT;
|
||||
const user = process.env.SMTP_USER;
|
||||
const pass = process.env.SMTP_PASSWORD;
|
||||
|
||||
if (!host || !port || !user || !pass) {
|
||||
throw new Error(
|
||||
"SMTP not configured — SMTP_HOST/SMTP_PORT/SMTP_USER/SMTP_PASSWORD must all be set."
|
||||
);
|
||||
}
|
||||
|
||||
transporter = nodemailer.createTransport({
|
||||
host,
|
||||
port: Number(port),
|
||||
secure: Number(port) === 465, // 465 = implicit TLS, 587 = STARTTLS
|
||||
auth: { user, pass },
|
||||
});
|
||||
|
||||
return transporter;
|
||||
}
|
||||
|
||||
interface SendMailInput {
|
||||
to: string;
|
||||
subject: string;
|
||||
text: string;
|
||||
html?: string;
|
||||
}
|
||||
|
||||
export async function sendMail({ to, subject, text, html }: SendMailInput): Promise<void> {
|
||||
const from = process.env.MAIL_FROM ?? process.env.SMTP_USER;
|
||||
await getTransporter().sendMail({ from, to, subject, text, html });
|
||||
}
|
||||
Reference in new issue
Block a user