🛡️ Step 3: Backend & Security
Since this is a static site (HTML/CSS), we can't send emails directly from the browser. We need a "Serverless Function" that runs in the cloud. We also need to block bots.
1. The API Route
We created src/pages/api/send-mail.ts. This file exports a POST function that handles form submissions.
Key feature:
export const prerender = false; // Forces server-side execution
2. Anti-Spam (Cloudflare Turnstile)
We integrated Cloudflare Turnstile, a smart CAPTCHA that doesn't annoy users with puzzles.
- Frontend: Added the widget to
ContactForm.astro.<div class="cf-turnstile" data-sitekey={...}></div> - Backend: We verify the token before sending the email.
const result = await fetch('https://challenges.cloudflare.com/turnstile/v0/siteverify', ...); if (!result.success) throw new Error('Bot detected');
3. Gmail Integration
To avoid spam folders, we don't use generic SMTP. We use the Gmail API with OAuth2.
- We registered an App in the Google Cloud Console.
- We generated a Refresh Token (via OAuth Playground).
- The code uses
googleapisandnodemailerto authenticate and dispatch the message.
This ensures high deliverability and security, as our actual password is never stored – only a revocable token.