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:
@ -1,4 +1,5 @@
|
||||
import type { APIRoute } from 'astro';
|
||||
import { getEmailBinding, sendCloudflareEmail } from '../../lib/email';
|
||||
|
||||
export const prerender = false;
|
||||
|
||||
@ -58,27 +59,6 @@ async function verifyTurnstile(token: string, secret: string, ip?: string | null
|
||||
return payload.success === true;
|
||||
}
|
||||
|
||||
async function sendWithResend(apiKey: string, fromEmail: string, toEmail: string, subject: string, text: string) {
|
||||
const response = await fetch('https://api.resend.com/emails', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
Authorization: `Bearer ${apiKey}`,
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify({
|
||||
from: fromEmail,
|
||||
to: [toEmail],
|
||||
subject,
|
||||
text,
|
||||
}),
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
const detail = await response.text();
|
||||
throw new Error(`Resend error ${response.status}: ${detail}`);
|
||||
}
|
||||
}
|
||||
|
||||
function resolveDepartmentRecipient(runtimeEnv: RuntimeEnv, defaultRecipient: string, department: string): string {
|
||||
const normalized = department.trim().toLowerCase();
|
||||
const map: Record<string, string> = {
|
||||
@ -92,7 +72,6 @@ function resolveDepartmentRecipient(runtimeEnv: RuntimeEnv, defaultRecipient: st
|
||||
export const POST: APIRoute = async ({ request, locals, url }) => {
|
||||
const runtimeEnv = getRuntimeEnv(locals);
|
||||
const mode = readEnv('CONTACT_FORM_MODE', runtimeEnv).toLowerCase() || 'dev';
|
||||
const resendApiKey = readEnv('CONTACT_FORM_RESEND_API_KEY', runtimeEnv);
|
||||
const fromEmail = readEnv('CONTACT_FORM_FROM_EMAIL', runtimeEnv);
|
||||
const toEmail = readEnv('CONTACT_FORM_TO_EMAIL', runtimeEnv);
|
||||
const turnstileSecret = readEnv('CONTACT_TURNSTILE_SECRET_KEY', runtimeEnv);
|
||||
@ -141,13 +120,23 @@ export const POST: APIRoute = async ({ request, locals, url }) => {
|
||||
.join('\n');
|
||||
|
||||
if (mode === 'live') {
|
||||
if (!resendApiKey || !fromEmail || !toEmail) {
|
||||
return new Response('Live mode requires CONTACT_FORM_RESEND_API_KEY, CONTACT_FORM_FROM_EMAIL and CONTACT_FORM_TO_EMAIL.', {
|
||||
status: 500,
|
||||
});
|
||||
const emailBinding = getEmailBinding(runtimeEnv);
|
||||
if (!emailBinding || !fromEmail || !toEmail) {
|
||||
return new Response(
|
||||
'Live mode requires EMAIL binding (Cloudflare Email Service), CONTACT_FORM_FROM_EMAIL and CONTACT_FORM_TO_EMAIL.',
|
||||
{
|
||||
status: 500,
|
||||
},
|
||||
);
|
||||
}
|
||||
const destination = resolveDepartmentRecipient(runtimeEnv, toEmail, department);
|
||||
await sendWithResend(resendApiKey, fromEmail, destination, subject, text);
|
||||
await sendCloudflareEmail(emailBinding, {
|
||||
fromEmail,
|
||||
toEmail: destination,
|
||||
subject,
|
||||
text,
|
||||
replyTo: email,
|
||||
});
|
||||
} else {
|
||||
console.info('[contact:dev] Contact payload received', {
|
||||
name,
|
||||
|
||||
Reference in New Issue
Block a user