Sostituisce Resend con Cloudflare Email Service.
Usa il binding EMAIL per contatti e newsletter, eliminando la dipendenza da Resend e dalle relative API key. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
45
src/lib/email.ts
Normal file
45
src/lib/email.ts
Normal file
@ -0,0 +1,45 @@
|
||||
/**
|
||||
* Outbound email via Cloudflare Email Service (`send_email` binding).
|
||||
*/
|
||||
|
||||
export type RuntimeEnv = Record<string, unknown>;
|
||||
|
||||
type EmailBinding = {
|
||||
send: (message: {
|
||||
to: string;
|
||||
from: string;
|
||||
subject: string;
|
||||
text?: string;
|
||||
html?: string;
|
||||
replyTo?: string;
|
||||
}) => Promise<{ messageId: string }>;
|
||||
};
|
||||
|
||||
export function getEmailBinding(runtimeEnv: RuntimeEnv): EmailBinding | null {
|
||||
const binding = runtimeEnv.EMAIL;
|
||||
if (binding && typeof binding === 'object' && 'send' in binding) {
|
||||
return binding as EmailBinding;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
export async function sendCloudflareEmail(
|
||||
email: EmailBinding,
|
||||
options: {
|
||||
fromEmail: string;
|
||||
toEmail: string;
|
||||
subject: string;
|
||||
text: string;
|
||||
html?: string;
|
||||
replyTo?: string;
|
||||
},
|
||||
): Promise<void> {
|
||||
await email.send({
|
||||
from: options.fromEmail,
|
||||
to: options.toEmail,
|
||||
subject: options.subject,
|
||||
text: options.text,
|
||||
...(options.html ? { html: options.html } : {}),
|
||||
...(options.replyTo ? { replyTo: options.replyTo } : {}),
|
||||
});
|
||||
}
|
||||
@ -100,34 +100,7 @@ export async function verifyTurnstile(
|
||||
return payload.success === true;
|
||||
}
|
||||
|
||||
export async function sendWithResend(options: {
|
||||
apiKey: string;
|
||||
fromEmail: string;
|
||||
toEmail: string;
|
||||
subject: string;
|
||||
text: string;
|
||||
html?: string;
|
||||
}): Promise<void> {
|
||||
const response = await fetch('https://api.resend.com/emails', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
Authorization: `Bearer ${options.apiKey}`,
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify({
|
||||
from: options.fromEmail,
|
||||
to: [options.toEmail],
|
||||
subject: options.subject,
|
||||
text: options.text,
|
||||
...(options.html ? { html: options.html } : {}),
|
||||
}),
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
const detail = await response.text();
|
||||
throw new Error(`Resend error ${response.status}: ${detail}`);
|
||||
}
|
||||
}
|
||||
export { getEmailBinding, sendCloudflareEmail } from './email';
|
||||
|
||||
export function siteOrigin(requestUrl: URL, runtimeEnv: RuntimeEnv): string {
|
||||
const configured = readEnv('PUBLIC_SITE_URL', runtimeEnv) || readEnv('SITE', runtimeEnv);
|
||||
|
||||
Reference in New Issue
Block a user