# Contact form to inbox

A contact form that emails your team, sends the visitor a confirmation, and lets your team reply straight from their inbox. It uses Next.js, but the same calls work from any backend.

## How it works

1. The form posts to your server.
2. Your server sends the message to your team's mailbox, with `reply_to` set to the visitor, so pressing **Reply** in the webmail answers the visitor directly.
3. Your server sends the visitor a short confirmation.

Optional: give the form its own address, such as `hello@acme.com`, as an [inbound route](https://voozin.com/docs/inbound) to store every conversation in your app too.

## Server action

```ts
// app/contact/actions.ts
"use server";

import { voozin } from "@/lib/voozin";

export async function sendContact(_: unknown, formData: FormData) {
  const name = String(formData.get("name") ?? "").slice(0, 100);
  const email = String(formData.get("email") ?? "");
  const message = String(formData.get("message") ?? "").slice(0, 5000);
  if (!/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email) || !message) return { ok: false, error: "Please fill in all fields." };
  if (formData.get("website")) return { ok: true }; // honeypot field, hidden from people

  const id = crypto.randomUUID();

  await voozin.emails.send(
    {
      from: "Acme website <website@acme.com>",
      to: ["team@acme.com"],
      reply_to: `${name} <${email}>`,
      subject: `Contact form: ${name}`,
      text: `${message}\n\n— ${name} <${email}>`,
      tags: { category: "contact-form" },
    },
    { idempotencyKey: `contact-${id}-team` },
  );

  await voozin.emails.send(
    {
      from: "Acme <hello@acme.com>",
      to: [email],
      subject: "We got your message",
      text: `Hi ${name},\n\nThanks for contacting Acme. We usually reply within one business day.\n\nYour message:\n${message}`,
    },
    { idempotencyKey: `contact-${id}-ack` },
  );

  return { ok: true };
}
```

## Form

```tsx
// app/contact/page.tsx
"use client";

import { useActionState } from "react";
import { sendContact } from "./actions";

export default function ContactPage() {
  const [state, action, pending] = useActionState(sendContact, null);
  if (state?.ok) return <p>Thanks! We'll be in touch.</p>;
  return (
    <form action={action}>
      <input name="name" placeholder="Name" required />
      <input name="email" type="email" placeholder="Email" required />
      <textarea name="message" placeholder="Message" required />
      <input name="website" tabIndex={-1} autoComplete="off" hidden />
      {state?.error && <p>{state.error}</p>}
      <button disabled={pending}>Send</button>
    </form>
  );
}
```

## Protect the form

- Rate limit the action by IP address, and consider a CAPTCHA such as Cloudflare Turnstile for public forms.
- Never put user input into the `from` address; use `reply_to`. Sending "from" the visitor's address would fail SPF and DMARC.
- Keep the confirmation short and plain. If the form is abused, confirmations go to addresses that never asked for them; a rate limit keeps that from hurting your reputation.

## WordPress

With the [WordPress plugin](https://voozin.com/docs/wordpress), form plugins such as Contact Form 7, WPForms and Gravity Forms send through Voozin automatically. Set their **Reply-To** to the visitor's email field.
