Files
highland_games_website/app/actions/contact.ts
T
Mathew 0cd61f2a12
Deploy to VPS / deploy (push) Has been cancelled
Branche le formulaire de contact sur Resend
- Envoi reel via le SDK Resend dans app/actions/contact.ts (avec gestion d'erreur)
- Cles/adresses lues depuis l'env (RESEND_API_KEY, CONTACT_TO_EMAIL, CONTACT_FROM_EMAIL)
- Ajout de resend en dependance + lock synchronise
- .env.example documente la config (autorise dans .gitignore)
- README + TODO a jour

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-23 08:38:56 +02:00

87 lines
2.4 KiB
TypeScript

"use server";
import { Resend } from "resend";
export type ContactState = {
status: "idle" | "success" | "error";
message: string;
fieldErrors?: Partial<Record<"name" | "email" | "subject" | "message", string>>;
};
const EMAIL_RE = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
export async function sendContact(
_prev: ContactState,
formData: FormData,
): Promise<ContactState> {
const name = String(formData.get("name") ?? "").trim();
const email = String(formData.get("email") ?? "").trim();
const subject = String(formData.get("subject") ?? "").trim();
const message = String(formData.get("message") ?? "").trim();
const honeypot = String(formData.get("website") ?? "");
if (honeypot) {
return { status: "success", message: "Thanks for reaching out." };
}
const fieldErrors: ContactState["fieldErrors"] = {};
if (!name) fieldErrors.name = "Required.";
if (!email || !EMAIL_RE.test(email)) fieldErrors.email = "Valid email required.";
if (!subject) fieldErrors.subject = "Required.";
if (!message || message.length < 10) {
fieldErrors.message = "Tell us a bit more (10+ chars).";
}
if (Object.keys(fieldErrors).length > 0) {
return {
status: "error",
message: "Please fix the highlighted fields.",
fieldErrors,
};
}
const apiKey = process.env.RESEND_API_KEY;
const to = process.env.CONTACT_TO_EMAIL;
const from = process.env.CONTACT_FROM_EMAIL;
if (!apiKey || !to || !from) {
console.error(
"[contact] Missing email env vars (RESEND_API_KEY / CONTACT_TO_EMAIL / CONTACT_FROM_EMAIL).",
);
return {
status: "error",
message: "Email isn't configured yet. Please try again later.",
};
}
try {
const resend = new Resend(apiKey);
const { error } = await resend.emails.send({
from,
to,
replyTo: email,
subject: `[Contact] ${subject}`,
text: `From: ${name} <${email}>\n\n${message}`,
});
if (error) {
console.error("[contact] Resend error:", error);
return {
status: "error",
message: "Something went wrong sending your message. Please try again.",
};
}
} catch (err) {
console.error("[contact] Unexpected error:", err);
return {
status: "error",
message: "Something went wrong sending your message. Please try again.",
};
}
return {
status: "success",
message: "Message sent. We'll get back to you soon.",
};
}