45 lines
1.7 KiB
TypeScript
45 lines
1.7 KiB
TypeScript
import nodemailer from "nodemailer";
|
|
|
|
let cached: nodemailer.Transporter | null = null;
|
|
|
|
/** SMTP transporter for the existing ANOUMA mail system - no external newsletter service. */
|
|
export function getMailTransport(): nodemailer.Transporter {
|
|
if (cached) return cached;
|
|
|
|
const { SMTP_HOST, SMTP_PORT, SMTP_USER, SMTP_PASSWORD } = process.env;
|
|
if (!SMTP_HOST || !SMTP_PORT || !SMTP_USER || !SMTP_PASSWORD) {
|
|
throw new Error("SMTP_HOST, SMTP_PORT, SMTP_USER and SMTP_PASSWORD must be set to send email.");
|
|
}
|
|
|
|
cached = nodemailer.createTransport({
|
|
host: SMTP_HOST,
|
|
port: Number(SMTP_PORT),
|
|
secure: Number(SMTP_PORT) === 465,
|
|
tls: {
|
|
// Keep certificate verification enabled by default. A private SMTP
|
|
// proxy may expose a self-signed certificate; that deployment can
|
|
// explicitly opt out without weakening unrelated HTTPS requests.
|
|
rejectUnauthorized: process.env.SMTP_TLS_REJECT_UNAUTHORIZED !== "false",
|
|
},
|
|
connectionTimeout: 10000,
|
|
greetingTimeout: 10000,
|
|
socketTimeout: 20000,
|
|
auth: { user: SMTP_USER, pass: SMTP_PASSWORD },
|
|
});
|
|
return cached;
|
|
}
|
|
|
|
export function getMailFrom(): string {
|
|
const configured = (process.env.SMTP_FROM || process.env.SMTP_USER || "support@anouma.org").trim();
|
|
const unquoted = configured.replace(/^("|')(.*)\1$/, "$2").trim();
|
|
|
|
// Keep the address in the message header, but show only ANOUMA in mail clients.
|
|
if (unquoted.includes("<") && unquoted.includes(">")) return unquoted;
|
|
return `ANOUMA <${unquoted}>`;
|
|
}
|
|
|
|
/** Envelope sender used by strict SMTP relays for sender ownership checks. */
|
|
export function getMailEnvelopeFrom(): string {
|
|
return process.env.SMTP_USER || "support@anouma.org";
|
|
}
|