A SvelteKit contact form that emails you every message
Save one component and every message from your SvelteKit site reaches your inbox, with the error shown under the field when an address is mistyped. It runs on any adapter, static included, because the browser posts straight to us. Messages are stored in the EU and spam is filtered out.
14 days free, no card. Paste the address where the code says YOUR_FORM_ID.
<script lang="ts">
type Problem = { field?: string; message: string };
let sending = $state(false);
let sent = $state(false);
let problems: Problem[] = $state([]);
const problem = (field: string) => problems.find((p) => p.field === field)?.message;
async function send(event: SubmitEvent) {
event.preventDefault();
const form = event.currentTarget as HTMLFormElement;
sending = true;
const response = await fetch(form.action, {
method: 'POST',
headers: { Accept: 'application/json' },
body: new FormData(form),
});
const result = await response.json();
sending = false;
sent = result.ok;
problems = result.ok ? [] : result.errors;
if (result.ok) form.reset();
}
</script>
<form action="https://formhaven.net/f/YOUR_FORM_ID" method="POST" onsubmit={send}>
<label>Your email <input type="email" name="email" required /></label>
{#if problem('email')}<p>{problem('email')}</p>{/if}
<label>Message <textarea name="message" required></textarea></label>
<input type="text" name="_gotcha" style="display:none" tabindex="-1" autocomplete="off" />
<button type="submit" disabled={sending}>{sending ? 'Sending...' : 'Send'}</button>
<p role="status">{sent ? 'Thanks, your message is on its way.' : problems.find((p) => !p.field)?.message}</p>
</form>
Set it up
-
1
Create a form in FormHaven
Sign up, press New form and copy its address. It looks like https://formhaven.net/f/YOUR_FORM_ID.
-
2
Save the component
Save the code above as src/lib/ContactForm.svelte, with your own address in the action.
-
3
Put it on a page
In src/routes/contact/+page.svelte, import ContactForm from '#lib/ContactForm.svelte' and write <ContactForm /> where the form belongs. On SvelteKit 2 the import starts with $lib instead.
-
4
Send a test
Run npm run dev, fill in the form and check your inbox. Localhost is always allowed, so you can test before you deploy.
The page that shows it
SvelteKit 3 imports from src/lib with #lib. On SvelteKit 2, write $lib/ContactForm.svelte instead; nothing else changes.
<script lang="ts">
import ContactForm from '#lib/ContactForm.svelte';
</script>
<h1>Contact</h1>
<ContactForm />
Stop bots that skip the hidden field
Turn on the bot check in the form's settings and add this line once inside the head of src/app.html, above %sveltekit.head%. It solves its puzzle in the background and adds its answer as the form is sent, so hydration never sees a field it did not render. Visitors see nothing, and it sets no cookies.
<script src="https://formhaven.net/bot-check.js" async></script>
Questions about this setup
- Why not a SvelteKit form action?
- A form action runs on your server. With adapter-static there is no server, and on Vercel or Netlify the action still needs an email service, a key and somewhere to keep old messages. Posting to FormHaven gives you all three with no server code.
- Does it work without JavaScript?
- Yes. The form has a real action and method, so before the page has loaded, or with JavaScript off, the browser posts it the normal way and the visitor sees our thank-you page, or yours if you set one on the form.
- Which adapters does this work with?
- All of them: adapter-static, adapter-auto, Vercel, Netlify, Cloudflare and Node. The form posts to FormHaven, so nothing runs on your host when a visitor presses Send.
- Can my client get the messages instead of me?
- Yes. Add up to 5 more addresses to the form. Each one confirms by a link first, and their copy carries no FormHaven branding. You can turn your own copy off.
- Where is the data stored?
- On our servers in the EU, for 1 to 365 days as you choose per form, or not at all: then each message is emailed and its answers deleted. A data processing agreement covers every account.
More to read
Get your form address in a minute
14 days free, no card. Then €1 per form a month, €0.50 from the 11th form.