An Angular contact form for a static build
One standalone component and provideHttpClient() are all it takes. The component posts the form as JSON and shows each error under its field, and every message is spam filtered, stored in the EU and emailed to you. Your ng build output stays a folder of static files.
14 days free, no card. Paste the address where the code says YOUR_FORM_ID.
import { Component, inject, signal } from '@angular/core';
import { HttpClient, HttpErrorResponse } from '@angular/common/http';
type Problem = { code: string; field?: string; message: string };
type Reply = { ok: boolean; message?: string; errors?: Problem[] };
@Component({
selector: 'app-contact-form',
template: `
<form action="https://formhaven.net/f/YOUR_FORM_ID" method="POST" (submit)="send($event)">
<label>Your email <input type="email" name="email" required></label>
@if (problem('email'); as text) { <p>{{ text }}</p> }
<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">{{ status() }}</p>
</form>
`,
})
export class ContactForm {
private http = inject(HttpClient);
protected sending = signal(false);
protected status = signal('');
private problems = signal<Problem[]>([]);
protected problem(field: string) {
return this.problems().find((p) => p.field === field)?.message;
}
protected send(event: SubmitEvent) {
event.preventDefault();
const form = event.target as HTMLFormElement;
// Every named input, _gotcha and the bot check's _botcheck included.
const body = Object.fromEntries(new FormData(form));
this.sending.set(true);
this.http.post<Reply>(form.action, body, { headers: { Accept: 'application/json' } }).subscribe({
next: () => {
this.sending.set(false);
this.problems.set([]);
this.status.set('Thanks, your message is on its way.');
form.reset();
},
error: (response: HttpErrorResponse) => {
// A refusal (422, 429) carries our JSON; a network failure carries none.
const reply: Reply | null = response.error?.ok === false ? response.error : null;
this.sending.set(false);
this.problems.set(reply?.errors ?? []);
this.status.set(reply?.errors?.some((p) => p.field) ? '' : reply?.message ?? 'Could not send. Check your connection and try again.');
},
});
}
}
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
Provide HttpClient
Add provideHttpClient() to the providers in src/app/app.config.ts, as shown below. HttpClient sends its requests with fetch by default.
-
3
Add the component
Save the code above as src/app/contact-form.ts with your address in the action. Add ContactForm to the imports of the page that shows it, and put <app-contact-form /> in that page's template.
-
4
Send a test
Run ng serve and open http://localhost:4200. Localhost is always allowed, so you can test before you deploy. Type a@b as the address to see the error appear under the field.
Provide HttpClient once
Once provideHttpClient() is in the providers, the component gets HttpClient with inject().
import { ApplicationConfig, provideBrowserGlobalErrorListeners } from '@angular/core';
import { provideHttpClient } from '@angular/common/http';
import { provideRouter } from '@angular/router';
import { routes } from './app.routes';
export const appConfig: ApplicationConfig = {
providers: [provideBrowserGlobalErrorListeners(), provideRouter(routes), provideHttpClient()],
};
A refused message arrives in error
HttpClient treats every status outside 200 to 299 as an error, so a refused message lands in the error callback: 422 for an address that is not valid, 429 when one address sends too often. response.error holds our JSON. Each entry in errors has a code, a message and, when one input caused it, the field. A network failure has no such body, so the component checks ok before it trusts one.
{
"ok": false,
"message": "The email address \"a@b\" is not valid.",
"error": "The email address \"a@b\" is not valid.",
"errors": [
{ "code": "TYPE_EMAIL", "field": "email", "message": "The email address \"a@b\" is not valid." }
]
}
Stop bots that skip the hidden field
Turn on the bot check in the form's settings and add this line once to src/index.html, inside <head>. It finds the form after Angular draws it, and the component sends its answer along, because new FormData(form) reads every named input, _botcheck included. Visitors see nothing, and it sets no cookies.
<script src="https://formhaven.net/bot-check.js" async></script>
Build and deploy
ng build writes the site to dist, in a browser folder under your project's name. Upload that folder to any host that serves static files. The form posts to FormHaven, so nothing has to run on the host.
ng build
# upload dist/YOUR_PROJECT/browser to Netlify, Cloudflare Pages,
# GitHub Pages or any other static host
Questions about this setup
- Which Angular version is this for?
- It was tested on Angular 21.2 in a new app from ng new: a standalone component, signals and the @if block, posting to a FormHaven form. The @if block needs Angular 17 or later.
- Can I use Reactive Forms or ngModel instead?
- Yes. Send your form model as the body instead of the FormData object, and keep an empty _gotcha key in it. With the bot check on, also send the value of the _botcheck input that the script adds to the form element when it is sent.
- Do I need a proxy or a CORS setting?
- No. Form addresses accept requests from any website, so HttpClient can post from your own domain, a preview deployment or ng serve on localhost.
- Is the form address a secret?
- No. It is meant to sit in your bundle: it can receive messages, never read them. Spam filtering and the allowed sites setting decide what reaches you.
- 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.