A Nuxt contact form that emails you every message
Save one component and every message from your Nuxt site reaches your inbox, with the error shown under the field when an address is mistyped. It needs no server, so it works the same after nuxt generate. 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 setup lang="ts">
type Problem = { field?: string; message: string };
const sending = ref(false);
const sent = ref(false);
const problems = ref<Problem[]>([]);
const problem = (field: string) => problems.value.find((p) => p.field === field)?.message;
async function send(event: Event) {
const form = event.currentTarget as HTMLFormElement;
sending.value = true;
const response = await fetch(form.action, {
method: 'POST',
headers: { Accept: 'application/json' },
body: new FormData(form),
});
const result = await response.json();
sending.value = false;
sent.value = result.ok;
problems.value = result.ok ? [] : result.errors;
if (result.ok) form.reset();
}
</script>
<template>
<form action="https://formhaven.net/f/YOUR_FORM_ID" method="POST" @submit.prevent="send">
<label>Your email <input type="email" name="email" required></label>
<p v-if="problem('email')">{{ problem('email') }}</p>
<label>Message <textarea name="message" required /></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>
</template>
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 app/components/ContactForm.vue, with your own address in the action. On Nuxt 3, which has no app folder, use components/ContactForm.vue.
-
3
Put it on a page
Write <ContactForm /> in your contact page, for example app/pages/contact.vue. Nuxt imports components on its own, so there is no import line.
-
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.
Stop bots that skip the hidden field
Turn on the bot check in the form's settings and load the script once from nuxt.config.ts, so every page has it. It solves its puzzle in the background and adds its answer as the form is sent, so Vue's hydration never sees a field it did not render. Visitors see nothing, and it sets no cookies.
export default defineNuxtConfig({
app: {
head: {
script: [{ src: 'https://formhaven.net/bot-check.js', async: true }],
},
},
})
A static site with nuxt generate
The form needs no server, so you can prerender the whole site and upload the .output/public folder to Netlify, Cloudflare Pages, GitHub Pages or your own server.
npm run generate
# upload .output/public
Questions about this setup
- Why fetch and not $fetch?
- $fetch throws when we answer 422, so a mistyped address would land in a catch block instead of under the field. fetch returns the answer either way, and the JSON says what went wrong.
- Why not a server route with an email API?
- A server/api route needs a running server, an email service and its key, and gives you nowhere to read old messages. With nuxt generate there is no server at all. Posting to FormHaven covers all of it.
- Does it work without JavaScript?
- Yes. The form has a real action and method, so the browser posts it the normal way and the visitor sees our thank-you page, or yours if you set one on the form.
- 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.