contactform

package
v0.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 23, 2026 License: MIT Imports: 10 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package contactform serves a plain HTML contact / feedback form that forwards each submission by mail.

The handler applies its defenses in a fixed order: body cap → CSRF (double-submit, see web/csrf) → honeypot → per-IP rate limit → field validation → send. Every outcome is a 303 to a rendered page (/<lang>/<SuccessPage> or /<lang>/<FailurePage>), so the form works without JavaScript beyond the CSRF copy and the browser never re-POSTs on reload. A honeypot hit is answered with the success page so bots learn nothing.

Wiring:

guard := csrf.New(csrf.Options{})
mux.Handle("POST /contact", &contactform.Handler{
    Fields: []contactform.Field{
        {Name: "message", Label: "Message", Required: true, Multiline: true},
        {Name: "email", Label: "Reply to", Email: true},
    },
    SubjectPrefix: "Contact: ",
    Sender:        notifier, // e.g. *scaleway.Notifier
    Limiter:       limiter,  // e.g. *ratelimit.Limiter
    CSRF:          guard,
    Langs:         cfg.SupportedLangs,
    DefaultLang:   cfg.DefaultLang,
})
mux.Handle("/", guard.Issue(pages))

Index

Constants

View Source
const (
	DefaultHoneypot    = "website"
	DefaultLangField   = "lang"
	DefaultSuccessPage = "thanks"
	DefaultFailurePage = "failed"
	DefaultMaxRunes    = 4000
	DefaultMaxBody     = 64 << 10
	DefaultSendTimeout = 15 * time.Second
	// MaxEmail caps an Email field, whatever its MaxRunes.
	MaxEmail = 254
)

Defaults applied when the corresponding Handler field is zero.

Variables

This section is empty.

Functions

func FirstLine

func FirstLine(s string, maxRunes int) string

FirstLine returns the first line of s, trimmed and cut to max runes with "..." appended when it was longer.

Types

type Field

type Field struct {
	// Name is the form field name.
	Name string
	// Label names the field in the mail body.
	Label string
	// Required rejects the submission when the field is blank.
	Required bool
	// MaxRunes caps the trimmed value; 0 means DefaultMaxRunes.
	MaxRunes int
	// Email requires a bare address ("a@example.com", no display
	// name). The first non-empty Email field becomes the Reply-To.
	Email bool
	// Multiline puts the label on its own line in the mail body.
	Multiline bool
}

Field describes one form input.

type Handler

type Handler struct {
	Fields  []Field
	Sender  Sender
	CSRF    Verifier
	Limiter Limiter // nil disables the rate limit

	// Langs and DefaultLang validate the posted language, which
	// picks the result page.
	Langs       []string
	DefaultLang string
	LangField   string // defaults to DefaultLangField
	Honeypot    string // defaults to DefaultHoneypot

	SuccessPage string // defaults to DefaultSuccessPage
	FailurePage string // defaults to DefaultFailurePage

	// Subject builds the mail subject. Nil uses SubjectPrefix plus
	// the first line of the first field.
	Subject       func(Submission) string
	SubjectPrefix string
	// Intro is the first paragraph of the mail body (e.g. which site
	// the submission came from).
	Intro string

	MaxBody     int64         // defaults to DefaultMaxBody
	SendTimeout time.Duration // defaults to DefaultSendTimeout
}

Handler serves the form's POST endpoint. Fields, Sender and CSRF are required: without CSRF every post is rejected (fail closed).

func (*Handler) Body

func (h *Handler) Body(s Submission) string

Body renders the plain-text mail: Intro, then each field (multiline fields as a labelled paragraph, others as "Label: value"), then the page language.

func (*Handler) Parse

func (h *Handler) Parse(r *http.Request, lang string) (Submission, bool)

Parse reads and validates the fields of a parsed form. It reports false when a required field is blank, a value is too long or an Email field is not a bare address.

func (*Handler) ServeHTTP

func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP handles one submission.

type Limiter

type Limiter interface {
	Allow(ctx context.Context, key string) bool
}

Limiter caps submissions per client IP. *ratelimit.Limiter from github.com/hblabs-co/kit/core implements it.

type Sender

type Sender interface {
	Enabled() bool
	Send(ctx context.Context, subject, text, replyTo string) error
}

Sender delivers one plain-text mail; replyTo may be empty. *scaleway.Notifier from github.com/hblabs-co/kit/mail implements it.

type Submission

type Submission struct {
	Lang    string
	Values  []Value
	ReplyTo string
}

Submission is one validated form post.

func (Submission) Get

func (s Submission) Get(name string) string

Get returns the value of the field called name, or "".

type Value

type Value struct {
	Field Field
	Text  string
}

Value is one submitted field.

type Verifier

type Verifier interface {
	Verify(r *http.Request) error
}

Verifier checks the CSRF token of a POST. *csrf.Guard implements it.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL