210 lines
5.8 KiB
TypeScript
210 lines
5.8 KiB
TypeScript
import nodemailer from "nodemailer";
|
|
import { logger } from "../logger";
|
|
import {
|
|
buildWorkshopConfirmationMail,
|
|
buildWorkshopInboxMail,
|
|
type WorkshopInquiryMailInput,
|
|
} from "../workshopSignupMail";
|
|
import { buildEmailChangeMail, buildMagicLinkMail } from "./magicLinkMail";
|
|
import { getSmtpUrl } from "./env";
|
|
import {
|
|
buildRuleStakeholderInviteMail,
|
|
stakeholderInviteReplyTo,
|
|
type RuleStakeholderInviteMailInput,
|
|
} from "./ruleStakeholderInviteMail";
|
|
import {
|
|
buildRuleFollowUpMail,
|
|
type RuleFollowUpMailInput,
|
|
} from "./ruleFollowUpMail";
|
|
|
|
function escapeHtml(value: string): string {
|
|
return value
|
|
.replace(/&/g, "&")
|
|
.replace(/</g, "<")
|
|
.replace(/>/g, ">")
|
|
.replace(/"/g, """);
|
|
}
|
|
|
|
/** Display name is CommunityRule, one word, even if SMTP_FROM still has a space. */
|
|
function formatMailFrom(from: string): string {
|
|
const trimmed = from
|
|
.trim()
|
|
.replace(/^"?Community Rule"?(\s*<)/, "CommunityRule$1");
|
|
if (trimmed.includes("<")) return trimmed;
|
|
if (trimmed.includes("@")) return `CommunityRule <${trimmed}>`;
|
|
return trimmed;
|
|
}
|
|
|
|
export function resolveMailFrom(): string {
|
|
const raw =
|
|
process.env.SMTP_FROM?.trim() ||
|
|
process.env.CLOUDRON_MAIL_FROM?.trim() ||
|
|
"noreply@localhost";
|
|
return formatMailFrom(raw);
|
|
}
|
|
|
|
/** Avoid quoted-printable `token=3D` wrapping that some webmail clients do not decode. */
|
|
export const MAIL_TEXT_ENCODING = "base64" as const;
|
|
|
|
/** Plaintext + HTML for one-time verify URLs. */
|
|
export function buildVerifyLinkParts(
|
|
verifyUrl: string,
|
|
intro: string,
|
|
outro: string,
|
|
linkLabel: string,
|
|
): { text: string; html: string } {
|
|
const href = escapeHtml(verifyUrl);
|
|
const text = `${intro}\n\n<${verifyUrl}>\n\n${outro}`;
|
|
const html =
|
|
`<p>${escapeHtml(intro).replace(/\n/g, "<br />")}</p>` +
|
|
`<p><a href="${href}">${escapeHtml(linkLabel)}</a></p>` +
|
|
`<p><a href="${href}">${href}</a></p>` +
|
|
`<p>${escapeHtml(outro)}</p>`;
|
|
return { text, html };
|
|
}
|
|
|
|
async function sendHtmlMail(opts: {
|
|
to: string;
|
|
subject: string;
|
|
text: string;
|
|
html: string;
|
|
from?: string;
|
|
replyTo?: string;
|
|
devLog: string;
|
|
}): Promise<void> {
|
|
const smtpUrl = getSmtpUrl();
|
|
|
|
if (!smtpUrl) {
|
|
if (process.env.NODE_ENV === "development") {
|
|
logger.info(opts.devLog);
|
|
return;
|
|
}
|
|
throw new Error("CLOUDRON_MAIL_SMTP_* is not configured");
|
|
}
|
|
|
|
const transporter = nodemailer.createTransport(smtpUrl);
|
|
await transporter.sendMail({
|
|
from: opts.from ?? resolveMailFrom(),
|
|
to: opts.to,
|
|
subject: opts.subject,
|
|
text: opts.text,
|
|
html: opts.html,
|
|
textEncoding: MAIL_TEXT_ENCODING,
|
|
replyTo: opts.replyTo,
|
|
});
|
|
}
|
|
|
|
export async function sendMagicLinkEmail(
|
|
to: string,
|
|
verifyUrl: string,
|
|
origin: string,
|
|
): Promise<void> {
|
|
const { subject, text, html } = buildMagicLinkMail({ verifyUrl, origin });
|
|
await sendHtmlMail({
|
|
to,
|
|
subject,
|
|
text,
|
|
html,
|
|
devLog: `[dev] Magic link for ${to}: ${verifyUrl}`,
|
|
});
|
|
}
|
|
|
|
/** Stakeholder invite after finishing a Rule (one-time link, same Mailhog pattern as magic link). */
|
|
export async function sendRuleStakeholderInviteEmail(
|
|
input: RuleStakeholderInviteMailInput,
|
|
): Promise<void> {
|
|
const { subject, text, html } = buildRuleStakeholderInviteMail(input);
|
|
const replyTo = stakeholderInviteReplyTo(input.inviterEmail);
|
|
await sendHtmlMail({
|
|
to: input.to,
|
|
subject,
|
|
text,
|
|
html,
|
|
...(replyTo ? { replyTo } : {}),
|
|
devLog: `[dev] Rule stakeholder invite (${input.communityName}) for ${input.to}: ${input.verifyUrl}`,
|
|
});
|
|
}
|
|
|
|
/** 48-hour print follow-up to the address that holds the pen. */
|
|
export async function sendRuleFollowUpEmail(
|
|
input: RuleFollowUpMailInput,
|
|
): Promise<void> {
|
|
const { subject, text, html } = buildRuleFollowUpMail(input);
|
|
await sendHtmlMail({
|
|
to: input.to,
|
|
subject,
|
|
text,
|
|
html,
|
|
devLog: `[dev] Rule follow-up mail for ${input.to}`,
|
|
});
|
|
}
|
|
|
|
/** Notify support/organizers when a visitor submits the Ask an organizer form. */
|
|
export async function sendOrganizerInquiryNotification(params: {
|
|
/** Destination inbox (e.g. from ORGANIZER_INQUIRY_TO). */
|
|
to: string;
|
|
fromEmail: string;
|
|
visitorEmail: string;
|
|
message: string;
|
|
requestId: string;
|
|
}): Promise<void> {
|
|
const { to, fromEmail, visitorEmail, message, requestId } = params;
|
|
const text = `Request ID: ${requestId}\nFrom: ${visitorEmail}\n\n${message}\n`;
|
|
const html =
|
|
`<p>Request ID: ${escapeHtml(requestId)}<br />From: ${escapeHtml(visitorEmail)}</p>` +
|
|
`<pre>${escapeHtml(message)}</pre>`;
|
|
await sendHtmlMail({
|
|
to,
|
|
from: fromEmail,
|
|
replyTo: visitorEmail,
|
|
subject: `Ask an organizer inquiry from ${visitorEmail}`,
|
|
text,
|
|
html,
|
|
devLog: `[dev] Organizer inquiry (request ${requestId}) from ${visitorEmail} to ${to}:\n${message}`,
|
|
});
|
|
}
|
|
|
|
export async function sendWorkshopInquiryNotification(
|
|
input: WorkshopInquiryMailInput & { to: string },
|
|
): Promise<void> {
|
|
const { subject, text, html } = buildWorkshopInboxMail(input);
|
|
await sendHtmlMail({
|
|
to: input.to,
|
|
replyTo: input.values.email,
|
|
subject,
|
|
text,
|
|
html,
|
|
devLog: `[dev] Workshop inquiry ${input.requestId} from ${input.values.email} to ${input.to}:\n${text}`,
|
|
});
|
|
}
|
|
|
|
export async function sendWorkshopInquiryConfirmation(input: {
|
|
to: string;
|
|
largeOrganization: boolean;
|
|
schedulingUrl: string;
|
|
}): Promise<void> {
|
|
const { subject, text, html } = buildWorkshopConfirmationMail(input);
|
|
await sendHtmlMail({
|
|
to: input.to,
|
|
subject,
|
|
text,
|
|
html,
|
|
devLog: `[dev] Workshop confirmation for ${input.to} (large=${input.largeOrganization})`,
|
|
});
|
|
}
|
|
|
|
export async function sendEmailChangeEmail(
|
|
to: string,
|
|
verifyUrl: string,
|
|
origin: string,
|
|
): Promise<void> {
|
|
const { subject, text, html } = buildEmailChangeMail({ verifyUrl, origin });
|
|
await sendHtmlMail({
|
|
to,
|
|
subject,
|
|
text,
|
|
html,
|
|
devLog: `[dev] Email change verify for ${to}: ${verifyUrl}`,
|
|
});
|
|
}
|