Angular contact form that sends email

Formgong team ·

An Angular contact form is a standalone component that posts the form to Formgong with fetch or HttpClient. There is no backend to run, and the browser cannot speak SMTP. Telegram is instant. Email arrives for each submission, on the free plan too.

Standalone component

This component imports FormsModule so ngSubmit runs. Without that import, the browser submits the form and the page reloads.

The handler calls preventDefault, checks the fields, and posts FormData with fetch. It does not set Content-Type.

HttpClient is the other path. Use it when the app already calls provideHttpClient. The visible sample uses fetch so there is nothing to provide.

On Angular 17 and newer, standalone is the usual component. An older NgModule app adds this class to declarations and FormsModule to imports.

Free stores 250 submissions a month and emails each one right away to up to 3 verified addresses.

Telegram is instant on every plan, including Free. There is no auto-reply on Free.

Pro allows 5 email recipients.

contact-form.ts
import { Component } from "@angular/core";
import { FormsModule } from "@angular/forms";

@Component({
  selector: "contact-form",
  imports: [FormsModule],
  template: `
    <form (ngSubmit)="onSubmit($event)">
      <input type="hidden" name="access_key" value="YOUR_ACCESS_KEY" />
      <input type="hidden" name="_lang" value="en" />
      <label>Name <input name="name" autocomplete="name" required /></label>
      <label>Email <input name="email" type="email" autocomplete="email" required /></label>
      <label>Message <textarea name="message" required></textarea></label>
      <div aria-hidden="true" style="position:absolute;inset-inline-start:0;top:0;width:1px;height:1px;overflow:hidden;clip-path:inset(50%)">
        <input name="botcheck" tabindex="-1" autocomplete="off" />
      </div>
      <button type="submit" [disabled]="status === 'sending'">{{ status === "sending" ? "Sending…" : "Send" }}</button>
      <p [attr.role]="status === 'error' ? 'alert' : 'status'">{{ notice }}</p>
    </form>
  `,
})
export class ContactForm {
  status = "idle";
  notice = "";

  async onSubmit(event: Event) {
    event.preventDefault();
    const form = event.currentTarget;
    if (!(form instanceof HTMLFormElement) || !form.reportValidity()) return;
    this.status = "sending";
    this.notice = "";
    try {
      const response = await fetch("https://formgong.com/submit", {
        method: "POST",
        headers: { Accept: "application/json" },
        body: new FormData(form),
      });
      const result = await response.json();
      if (response.status === 429 || result.code === "rate_limited") {
        this.status = "error";
        this.notice = "Too many submissions. Wait a minute and try again.";
        return;
      }
      if (!response.ok || !result.success) {
        this.status = "error";
        this.notice = result.message || "Could not send. Try again.";
        return;
      }
      form.reset();
      this.status = "sent";
      this.notice = result.message || "Thanks. We have your message.";
    } catch {
      this.status = "error";
      this.notice = "Could not send. Check your connection and try again.";
    }
  }
}

Reactive forms

Angular's reactive forms guide builds a FormGroup from FormControl values. Bind it with formGroup and formControlName.

ngSubmit fires when the visitor submits. The handler reads the group. It does not need name attributes on the inputs.

HttpClient.post sends FormData as multipart. Do not set Content-Type yourself, or the boundary is lost.

observe response gives you the status. A 429 is the minute limit. success on the body is the lead.

HttpParams is the urlencoded path, for a form with no file. FormData is the one to keep if you might add a file later.

Mark the group touched when it is invalid, and do not send. The visitor can correct the field and try again.

FormGroup and HttpClient
FormGroup and HttpClient
import { Component, inject } from "@angular/core";
import { FormControl, FormGroup, ReactiveFormsModule, Validators } from "@angular/forms";
import { HttpClient } from "@angular/common/http";
import { firstValueFrom } from "rxjs";

type SubmitResult = { success?: boolean; message?: string; code?: string };

@Component({
  selector: "contact-form",
  imports: [ReactiveFormsModule],
  templateUrl: "./contact-form.html",
})
export class ContactForm {
  private readonly http = inject(HttpClient);
  readonly form = new FormGroup({
    name: new FormControl("", { nonNullable: true, validators: Validators.required }),
    email: new FormControl("", { nonNullable: true, validators: [Validators.required, Validators.email] }),
    message: new FormControl("", { nonNullable: true, validators: Validators.required }),
  });
  status = "idle";
  notice = "";

  async onSubmit() {
    if (this.form.invalid) {
      this.form.markAllAsTouched();
      return;
    }
    const body = new FormData();
    body.set("access_key", "YOUR_ACCESS_KEY");
    body.set("_lang", "en");
    body.set("name", this.form.controls.name.value);
    body.set("email", this.form.controls.email.value);
    body.set("message", this.form.controls.message.value);
    body.set("botcheck", "");
    this.status = "sending";
    this.notice = "";
    try {
      const response = await firstValueFrom(this.http.post<SubmitResult>("https://formgong.com/submit", body, {
        headers: { Accept: "application/json" },
        observe: "response",
      }));
      const result = response.body;
      if (!response.ok || !result?.success) {
        this.status = "error";
        this.notice = result?.message || "Could not send. Try again.";
        return;
      }
      this.form.reset();
      this.status = "sent";
      this.notice = result.message || "Thanks. We have your message.";
    } catch {
      this.status = "error";
      this.notice = "Could not send. Check your connection and try again.";
    }
  }
}
Template for the reactive form
Template for the reactive form
<form [formGroup]="form" (ngSubmit)="onSubmit()">
  <label>Name <input formControlName="name" autocomplete="name" /></label>
  <label>Email <input formControlName="email" type="email" autocomplete="email" /></label>
  <label>Message <textarea formControlName="message"></textarea></label>
  <button type="submit" [disabled]="status === 'sending'">Send</button>
  <p [attr.role]="status === 'error' ? 'alert' : 'status'">{{ notice }}</p>
</form>

Angular Material fields

mat-form-field wraps the control. matInput on the input or textarea is what the field requires.

Import MatFormFieldModule and MatInputModule. The form-field docs say a native input without matInput throws.

mat-label is the visible label. mat-error is the hint under the field. This sample uses only those pieces.

Keep formControlName. Material does not replace the FormGroup. It only draws the field.

mat-form-field markup
mat-form-field markup
<form [formGroup]="form" (ngSubmit)="onSubmit()">
  <mat-form-field>
    <mat-label>Email</mat-label>
    <input matInput type="email" formControlName="email" autocomplete="email" />
    <mat-error>Enter a valid email.</mat-error>
  </mat-form-field>
  <mat-form-field>
    <mat-label>Message</mat-label>
    <textarea matInput formControlName="message" rows="4"></textarea>
  </mat-form-field>
  <button mat-button type="submit">Send</button>
</form>

// imports: [ReactiveFormsModule, MatFormFieldModule, MatInputModule]

A file attachment

Name the file input attachment and post FormData. Do not set Content-Type. The browser must set the multipart boundary.

Turn on attachments in the form settings. Verify the account email, and add your Turnstile site key under Protection.

The server accepts PDF, JPEG and PNG only. The cap is 3 files, 5 MiB each and 10 MiB combined.

Free stores 1.0 GiB of files. Pro stores 10.0 GiB. Files follow the form's retention period and are deleted with the submission.

Only the form owner can download a file, and only after signing in. The Telegram alert does not attach the file.

On the free plan the email also lists the file link. You sign in to download it. Telegram is instant.

Add the file to FormData
Add the file to FormData
// Inside the FormGroup template. Do not set Content-Type on the HttpClient call.
<input type="file" (change)="onFile($event)" accept=".pdf,.jpg,.jpeg,.png" />

onFile(event: Event) {
  const input = event.target;
  if (!(input instanceof HTMLInputElement)) return;
  this.file = input.files?.[0] ?? null;
}

// When you build FormData:
if (this.file) body.set("attachment", this.file);

SMTP, EmailJS, or your own API

Search results for this form often start with SMTP. The browser cannot speak SMTP. That path always grows a backend.

EmailJS is a real option when you want their template and their from-address. It is still a third-party account, not a mail port in the page.

Your own API is the right tool when the submit must change data in your app. It is extra surface for a contact form.

Formgong is the short path: one public key, no SMTP secret, an inbox, and Telegram on the free plan.

Ways to send an Angular contact form
PathWhat it actually doesTrade-off
Browser SMTPNot possible. The page cannot open a mail port.You still need a server somewhere.
EmailJSTheir script sends from the browser through their API.You create an account and ship their public key. You own their quota.
Your APIA route you host calls SMTP or an email API.Right when the submit changes your database. Extra code for a contact form.
FormgongThe component posts FormData. No mail server in the repo.Email and Telegram arrive for each lead. The key is public.

Server rendering

Angular SSR, including the older Universal name, renders the component on the server and hydrates it in the browser.

The submit handler runs on ngSubmit. That event happens in the browser. The server render does not post the form.

Do not call HttpClient from the constructor or a field initializer to send the lead. That can run on the server.

Keep the empty form as the first render. The notice starts blank, so the server HTML and the client HTML match.

Error states

Treat the send as a success only when the HTTP status is OK and success is true.

Read code and message from the JSON. The message is already translated for _lang.

Leave botcheck empty. The same trap also answers to _gotcha, _honey and _honeypot.

A filled honeypot is stored as spam. It is not emailed, it is not sent to Telegram, and it does not use the monthly quota.

If the page sends _ts and the form arrives in under 1.5 seconds, that lead is scored as spam too.

Turnstile is optional on every plan. It is required when a file is attached. The secret stays in Formgong.

Formgong error codes for a contact form
CodeWhat it meansWhat to do
rate_limited429. More than 30 posts a minute for this form, or 20 from one IP.Wait a minute. Show the message and leave the fields filled.
limit_exceededThe account used its monthly submissions. Free is 250.The lead is not stored or delivered: the account is over its limit and its held submissions are full. The owner upgrades the plan or buys a pack.
unknown_access_key404. That access key does not match a form.Copy the key again from Formgong. A placeholder key always fails.
turnstile_missingTurnstile is on, or a file is attached, and the token was not posted.Add the widget only after you turn protection on. The field name is cf-turnstile-response.
turnstile_failedCloudflare rejected the token.Load the widget again and submit once. Check the site key.
too_large413. The text body is over 64 KB, or a file is over the upload cap.Shorten the message, or send fewer files. The cap is 3 files, 5 MiB each, 10 MiB total.
origin_not_allowedA domain list is on, and this host is not in it.Add the live host. Every plan, Free included, has the domain list.

Why a page cannot send mail by itself is in send email from frontend JavaScript. The fetch pattern without a framework is on the JavaScript contact form. Quotas are on the pricing page.

How forms work in Angular

An Angular app runs in the browser, so a contact form usually needs a backend of its own to send email. Formgong takes that role: the component sends the fields to https://formgong.com/submit and shows the translated answer.

The snippet is a standalone component that handles (ngSubmit). It uses the browser's fetch, so it needs no HttpClient setup. If your app already provides HttpClient with provideHttpClient(), you can send the same request with it.

Set it up in Angular

  1. Create a form in Formgong and copy the access key.
  2. Add the component below and replace the key. Import FormsModule in the component so (ngSubmit) fires and the page does not reload.
  3. If you use Reactive Forms, keep your FormGroup and send its value in the submit handler.
  4. Run ng serve and send a test. The lead is in the Formgong inbox. Telegram and email arrive right away.

Component package: npm install @formgong/angular (npm: @formgong/angular)

Add to an existing project: npx formgong init (npm: formgong)

Formgong or your own backend?

Your own backend is the right place when the form changes data in your system.

For contact and lead forms Formgong removes the backend, the email provider and the spam filtering from your project.

Questions and answers

fetch or HttpClient?

Either works. fetch needs no setup; HttpClient fits if you already use interceptors or want observables.

Why does the page reload when I submit?

Usually FormsModule is not imported, so (ngSubmit) does not run and the browser sends the form itself. Import it, or call event.preventDefault().

Does it work with Angular SSR?

Yes. The request runs only when the visitor submits the form in the browser.

Start free