Files
CubeAdmin/lib/email/index.ts
2026-03-08 15:49:34 +01:00

54 lines
1.4 KiB
TypeScript

import { Resend } from "resend";
import { render } from "@react-email/render";
import { InvitationEmail } from "./templates/invitation";
function getResend(): Resend {
const key = process.env.RESEND_API_KEY;
if (!key) throw new Error("RESEND_API_KEY is not configured");
return new Resend(key);
}
export async function sendMagicLinkEmail({
email,
url,
token: _token,
}: {
email: string;
url: string;
token: string;
}): Promise<void> {
const { error } = await getResend().emails.send({
from: process.env.EMAIL_FROM ?? "CubeAdmin <noreply@example.com>",
to: email,
subject: "Your CubeAdmin sign-in link",
html: `<p>Click the link below to sign in to CubeAdmin. This link expires in 1 hour.</p><p><a href="${url}">${url}</a></p>`,
});
if (error) throw new Error(`Failed to send magic link email: ${error.message}`);
}
export async function sendInvitationEmail({
to,
invitedByName,
inviteUrl,
role,
}: {
to: string;
invitedByName: string;
inviteUrl: string;
role: string;
}): Promise<void> {
const html = await render(
InvitationEmail({ invitedByName, inviteUrl, role }),
);
const { error } = await getResend().emails.send({
from: process.env.EMAIL_FROM ?? "CubeAdmin <noreply@example.com>",
to,
subject: `You've been invited to CubeAdmin`,
html,
});
if (error) throw new Error(`Failed to send email: ${error.message}`);
}