Aggiunge routing contatti per reparto con endpoint Cloudflare e Resend

Introduce selettore reparto nel form, instradamento verso destinatari dedicati via variabili ambiente e fallback al destinatario generale. Mantiene modalita dev/live e validazione Turnstile lato server.

Made-with: Cursor
This commit is contained in:
Javaxman
2026-04-23 21:12:45 +02:00
parent 5ab329adbf
commit 70759d6c1c
6 changed files with 63 additions and 3 deletions

View File

@ -7,4 +7,9 @@ CONTACT_FORM_MODE=dev
CONTACT_FORM_RESEND_API_KEY=
CONTACT_FORM_FROM_EMAIL=
CONTACT_FORM_TO_EMAIL=
CONTACT_FORM_TO_EMAIL_PRIVACY=
CONTACT_FORM_TO_EMAIL_SECURITY=
CONTACT_FORM_TO_EMAIL_ETHICS=
CONTACT_FORM_TO_EMAIL_LEGAL=
CONTACT_FORM_TO_EMAIL_HR=
CONTACT_TURNSTILE_SECRET_KEY=

View File

@ -67,7 +67,12 @@ Server-side (Cloudflare runtime / local `.env`):
- `CONTACT_FORM_MODE` (`dev` or `live`)
- `CONTACT_FORM_RESEND_API_KEY`
- `CONTACT_FORM_FROM_EMAIL` (verified sender in Resend)
- `CONTACT_FORM_TO_EMAIL` (recipient inbox or alias)
- `CONTACT_FORM_TO_EMAIL` (default recipient inbox or alias)
- `CONTACT_FORM_TO_EMAIL_PRIVACY`
- `CONTACT_FORM_TO_EMAIL_SECURITY`
- `CONTACT_FORM_TO_EMAIL_ETHICS`
- `CONTACT_FORM_TO_EMAIL_LEGAL`
- `CONTACT_FORM_TO_EMAIL_HR`
- `CONTACT_TURNSTILE_SECRET_KEY`
Mode behavior:
@ -75,4 +80,10 @@ Mode behavior:
- `dev`: accepts form and logs payload server-side (no real email send)
- `live`: verifies Turnstile (if secret configured) and sends via Resend API
Department routing:
- the contact form includes a department selector
- if a department-specific env is set, the email is routed to that recipient
- otherwise it falls back to `CONTACT_FORM_TO_EMAIL`
Copy `.env.example` to `.env` for local development and configure the same keys in Cloudflare Pages/Workers for production.

View File

@ -11,6 +11,7 @@ const model = {
fieldName: cf.fieldName,
fieldCompany: cf.fieldCompany,
fieldCountry: cf.fieldCountry,
fieldDepartment: cf.fieldDepartment,
fieldEmail: cf.fieldEmail,
fieldMessage: cf.fieldMessage,
};
@ -161,6 +162,20 @@ const fieldClass =
class={fieldClass}
/>
</div>
<div>
<label class="block text-xs font-medium text-nx-muted" for="nx-cf-department">
{cf.departmentLabel}
</label>
<select
id="nx-cf-department"
name={cf.fieldDepartment}
class={fieldClass}
>
{cf.departmentOptions.map((opt) => (
<option value={opt.value}>{opt.label}</option>
))}
</select>
</div>
<div>
<label class="block text-xs font-medium text-nx-muted" for="nx-cf-msg">
{cf.messageLabel}

View File

@ -30,6 +30,7 @@ export const cta = {
nameLabel: 'Nome',
companyLabel: 'Azienda (opzionale)',
countryLabel: 'Paese',
departmentLabel: 'Reparto',
emailLabel: 'Email',
messageLabel: 'Scrivi qui il tuo messaggio',
submitLabel: 'Invia messaggio',
@ -49,7 +50,16 @@ export const cta = {
fieldName: 'name',
fieldCompany: 'company',
fieldCountry: 'country',
fieldDepartment: 'department',
fieldEmail: 'email',
fieldMessage: 'message',
departmentOptions: [
{ value: 'general', label: 'Generale' },
{ value: 'privacy', label: 'Privacy / DPO' },
{ value: 'security', label: 'Security / Incident Response' },
{ value: 'ethics', label: 'Segnalazioni confidenziali (Ethics)' },
{ value: 'legal', label: 'Legal' },
{ value: 'hr', label: 'HR' },
],
},
} as const;

View File

@ -79,6 +79,18 @@ async function sendWithResend(apiKey: string, fromEmail: string, toEmail: string
}
}
function resolveDepartmentRecipient(runtimeEnv: RuntimeEnv, defaultRecipient: string, department: string): string {
const normalized = department.trim().toLowerCase();
const map: Record<string, string> = {
privacy: readEnv('CONTACT_FORM_TO_EMAIL_PRIVACY', runtimeEnv),
security: readEnv('CONTACT_FORM_TO_EMAIL_SECURITY', runtimeEnv),
ethics: readEnv('CONTACT_FORM_TO_EMAIL_ETHICS', runtimeEnv),
legal: readEnv('CONTACT_FORM_TO_EMAIL_LEGAL', runtimeEnv),
hr: readEnv('CONTACT_FORM_TO_EMAIL_HR', runtimeEnv),
};
return map[normalized] || defaultRecipient;
}
export const POST: APIRoute = async ({ request, locals, url }) => {
const runtimeEnv = getRuntimeEnv(locals);
const mode = readEnv('CONTACT_FORM_MODE', runtimeEnv).toLowerCase() || 'dev';
@ -98,6 +110,7 @@ export const POST: APIRoute = async ({ request, locals, url }) => {
const company = required(form.get('company'));
const email = required(form.get('email'));
const country = required(form.get('country'));
const department = required(form.get('department')) || 'general';
const message = required(form.get('message'));
if (!name || !email || !country || !message) {
@ -122,6 +135,7 @@ export const POST: APIRoute = async ({ request, locals, url }) => {
company ? `Azienda: ${company}` : '',
`Email: ${email}`,
`Paese: ${country}`,
`Reparto: ${department}`,
'',
message,
]
@ -134,13 +148,15 @@ export const POST: APIRoute = async ({ request, locals, url }) => {
status: 500,
});
}
await sendWithResend(resendApiKey, fromEmail, toEmail, subject, text);
const destination = resolveDepartmentRecipient(runtimeEnv, toEmail, department);
await sendWithResend(resendApiKey, fromEmail, destination, subject, text);
} else {
console.info('[contact:dev] Contact payload received', {
name,
company,
email,
country,
department,
subject,
});
}

View File

@ -8,6 +8,7 @@ interface ContactFormCfg {
fieldName: string;
fieldCompany: string;
fieldCountry: string;
fieldDepartment: string;
fieldEmail: string;
fieldMessage: string;
}
@ -25,6 +26,7 @@ function readCfg(): ContactFormCfg {
fieldName: 'name',
fieldCompany: 'company',
fieldCountry: 'country',
fieldDepartment: 'department',
fieldEmail: 'email',
fieldMessage: 'message',
};
@ -67,12 +69,13 @@ function bind(): void {
const company = String(fd.get(cfg.fieldCompany) ?? '').trim();
const email = String(fd.get(cfg.fieldEmail) ?? '').trim();
const country = String(fd.get(cfg.fieldCountry) ?? '').trim();
const department = String(fd.get(cfg.fieldDepartment) ?? '').trim();
const message = String(fd.get(cfg.fieldMessage) ?? '').trim();
if (cfg.mailto) {
const subject = encodeURIComponent(cfg.notifySubject || 'Contatto sito');
const companyLine = company ? `\nAzienda: ${company}` : '';
const body = encodeURIComponent(
`Nome: ${name}${companyLine}\nEmail: ${email}\nPaese: ${country}\n\n${message}`,
`Nome: ${name}${companyLine}\nEmail: ${email}\nPaese: ${country}\nReparto: ${department || 'general'}\n\n${message}`,
);
window.location.href = `mailto:${cfg.mailto}?subject=${subject}&body=${body}`;
return;