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 ¶
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 ¶
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.
type Limiter ¶
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 ¶
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 "".