Files
echecsfrance/src/utils/nodemailerProvider.ts
T
owenrees feadb465eb upgrade Nodemailer to v10
fix dead code with knip

upgrade TS to v6 and fix linting errors

auto apply oxfmt rules

fix linting errors - unused deps etc

ignore file

remove all ignored files in knip

add knip - dead code analysis tool

swap prettier and eslint of oxfmt and oxlint

fix: fix map rendering in react strict mode

fix: headlessUI float replace

fix: disable Matomo if not URL and SITE ID exist

upgrade: 1st pass upgrade to Next.js v16

deps: upgrade react to v19
2026-10-05 16:15:25 +02:00

167 lines
5.1 KiB
TypeScript

import { EmailConfig } from "next-auth/providers/email";
import { getTranslations } from "next-intl/server";
import { createTransport } from "nodemailer";
import type { Transport, TransportOptions } from "nodemailer";
import SMTPTransport from "nodemailer/lib/smtp-transport";
import SMTPPool from "nodemailer/lib/smtp-pool";
import SendmailTransport from "nodemailer/lib/sendmail-transport";
import StreamTransport from "nodemailer/lib/stream-transport";
import JSONTransport from "nodemailer/lib/json-transport";
import SESTransport from "nodemailer/lib/ses-transport";
type Awaitable<T> = T | PromiseLike<T>;
function html(params: {
url: string;
host: string;
t: Awaited<ReturnType<typeof getTranslations<"SignIn">>>;
}) {
const { url, host, t } = params;
const escapedHost = host.replace(/\./g, "&#8203;.");
const brandColor = "#346df1";
const buttonText = "#fff";
const color = {
background: "#f9f9f9",
text: "#444",
mainBackground: "#fff",
buttonBackground: brandColor,
buttonBorder: brandColor,
buttonText,
};
return `
<body style="background: ${color.background};">
<table width="100%" border="0" cellspacing="20" cellpadding="0"
style="background: ${color.mainBackground}; max-width: 600px; margin: auto; border-radius: 10px;">
<tr>
<td align="center"
style="padding: 10px 0px; font-size: 22px; font-family: Helvetica, Arial, sans-serif; color: ${color.text};">
${t("email.subject", { host: escapedHost })}
</td>
</tr>
<tr>
<td align="center" style="padding: 20px 0px;">
<table border="0" cellspacing="0" cellpadding="0">
<tr>
<td align="center" style="border-radius: 5px;" bgcolor="${color.buttonBackground}">
<a href="${url}"
target="_blank"
style="font-size: 18px; font-family: Helvetica, Arial, sans-serif; color: ${color.buttonText}; text-decoration: none; border-radius: 5px; padding: 10px 20px; border: 1px solid ${color.buttonBorder}; display: inline-block; font-weight: bold;">
${t("email.button")}
</a>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td align="center"
style="padding: 0px 0px 10px 0px; font-size: 16px; line-height: 22px; font-family: Helvetica, Arial, sans-serif; color: ${color.text};">
${t("email.ignore")}
</td>
</tr>
</table>
</body>
`;
}
/** Email Text body (fallback for email clients that don't render HTML, e.g. feature phones) */
function text({
url,
host,
t,
}: {
url: string;
host: string;
t: Awaited<ReturnType<typeof getTranslations<"SignIn">>>;
}) {
return t("email.textMessage", { host, url });
}
type AllTransportOptions =
| string
| SMTPTransport
| SMTPTransport.Options
| SMTPPool
| SMTPPool.Options
| SendmailTransport
| SendmailTransport.Options
| StreamTransport
| StreamTransport.Options
| JSONTransport
| JSONTransport.Options
| SESTransport
| SESTransport.Options
| Transport<any>
| TransportOptions;
export interface NodemailerConfig extends EmailConfig {
server?: AllTransportOptions;
sendVerificationRequest: (params: {
identifier: string;
url: string;
expires: Date;
provider: NodemailerConfig;
token: string;
theme: any;
request: Request;
}) => Awaitable<void>;
options?: NodemailerUserConfig;
}
export type NodemailerUserConfig = Omit<Partial<NodemailerConfig>, "options" | "type">;
export default function Nodemailer(config: NodemailerUserConfig): NodemailerConfig {
return {
id: "nodemailer",
type: "email",
name: "Nodemailer",
server: { host: "localhost", port: 25, auth: { user: "", pass: "" } },
from: "Echecs France <no-reply@echecsfrance.com>",
maxAge: 24 * 60 * 60,
async sendVerificationRequest(params) {
const { identifier, url, provider } = params;
let locale = "fr";
const { searchParams } = new URL(url);
if (searchParams.has("callbackUrl")) {
const { pathname } = new URL(searchParams.get("callbackUrl")!);
const maybeLocale = pathname.split("/")[1];
if (["en", "fr"].includes(maybeLocale)) {
locale = maybeLocale;
}
}
const t = await getTranslations({ locale, namespace: "SignIn" });
const { host } = new URL(url);
if (!(config.server as unknown as any)?.host) {
console.log("Connection URL", url);
console.log(text({ url, host, t }));
return;
}
const transport = createTransport(provider.server);
const result = await transport.sendMail({
to: identifier,
from: provider.from,
subject: t("email.subject", { host }),
text: text({ url, host, t }),
html: html({ url, host, t }),
});
const failed = result.rejected.concat(result.pending).filter(Boolean);
if (failed.length) {
throw new Error(`Email (${failed.join(", ")}) could not be sent`);
}
},
options: config,
};
}