import type { Env } from "./types"; const VERIFY_FROM = { email: "noreply@vtit-agent-coding.dev", name: "VTIT Agent Coding" }; export async function sendVerificationEmail(env: Env, to: string, url: string): Promise { if (isLocalDev(env)) { console.info(`[email-verification] ${to}: ${localVerificationUrl(url)}`); return; } await env.EMAIL.send({ to, from: VERIFY_FROM, subject: "Verify your VTIT Agent Coding email", html: verificationHtml(url), text: `Verify your VTIT Agent Coding email: ${url}\n\nThis link expires in 1 hour.`, }); } function verificationHtml(url: string): string { const escapedUrl = escapeHtml(url); return `

Verify your VTIT Agent Coding email address.

Verify email

This link expires in 1 hour.

`; } function escapeHtml(value: string): string { return value.replace(/&/g, "&").replace(/"/g, """).replace(//g, ">"); } function isLocalDev(env: Env): boolean { return env.ALLOWED_HOSTS.split(",").some((host) => host.startsWith("localhost") || host.startsWith("127.0.0.1")); } function localVerificationUrl(url: string): string { return url.replace(/^https:\/\/(localhost|127\.0\.0\.1)(:\d+)?/, "http://$1$2"); }