Platform

Contact form API in Go

A Go package that gives the storefront a contact form and an “ask about this product” form, and the admin one inbox behind both. Each new message is announced to the store’s own address. An operator answers from the message’s drawer, and the reply goes to the shopper by email through the store’s own provider.

What it does

In the package’s own words it is “the storefront’s contact form, its product enquiries, and the inbox behind both”. A shopper writes, the store keeps the message, and tells its own address by email when the Plugins screen says where.

Both kinds land in one inbox on Customers › Contact messages. An enquiry is a message marked as one, carrying the product, and the variant and quantity if the shopper named them, as they were called when the shopper asked. Only a product on sale can be asked about. The inbox has three states, new, replied and archived, and a place for notes.

An operator holding contact.reply answers in the message’s drawer. The reply goes to the shopper from the provider’s sender with its reply-to address, so the answer comes back to the shop. Once the provider has taken it, the reply joins the message’s thread with who sent it and when, and the message is marked replied.

Configuration

Nothing is required. Set the announcement address in Config, which the reference binary reads from CONTACT_EMAIL, or on the Plugins screen, which wins. The form’s wording and the enquiry switch are on the Plugins screen only.

Contact form module settings — 5 settings, 0 required
SettingEnvironment variableRequiredWhat it does
NotifyEmail
Announce new messages to
CONTACT_EMAIL No The store’s own address, told of each new message and enquiry. Without it messages are still kept, and nobody is told. The panel asks for it when Config leaves it empty.
MessagesPerMinute — No How many messages and enquiries, counted together, one address may send in a minute. Ten when zero; a negative number turns the limit off. Go only; not in the panel.
subjects
Subjects the form offers
— No One per line, shown by the storefront as a list. By default: An order, A product, Returns and exchanges, Something else. Panel only.
thanks
After sending
— No What the storefront says once the form is sent. By default “Thanks — we’ll reply by email.” Panel only.
enquiries
Take product enquiries
— No On by default. Off, the enquiry form answers 404 and the contact form stays open. Panel only.

The module reads its Config struct, not the environment. The variable names are the ones the package’s own example or the reference binary uses; in your own main() you choose where each value comes from. Where a setting has a panel label, the admin’s settings drawer can hold it too, and a value typed there wins over Config.

Setting it up

  1. Install the modulePass contact.New to gocommerce.New, as below, or start the reference binary with -contact and CONTACT_EMAIL set. The Contact form plugin starts switched on.
  2. Say where to announceSet “Announce new messages to” on the Plugins screen, or NotifyEmail in Config. Each announcement goes through the store’s email provider and is on the Notifications screen too.
  3. Post from the storefrontPOST /x/contact/messages with a name, an email and the message; POST /x/contact/enquiries with the same and a product_id or product_slug. Leave the website field empty: it is the honeypot.
  4. Answer from the inboxCustomers › Contact messages lists both kinds, with filters for status and kind. Reply in the drawer, or with POST /api/admin/x/contact/messages/{id}/reply.

main.go

import (
	"os"

	"github.com/itswadesh/gocommerce/core"
	"github.com/itswadesh/gocommerce/ext/contact"
)

app, err := gocommerce.New(cfg,
	contact.New(contact.Config{
		NotifyEmail: os.Getenv("CONTACT_EMAIL"),
	}),
)

The package doc has no example of its own; this is how the reference binary’s -contact flag and examples/store/main.go install it. cfg is your gocommerce.Config. Import path github.com/itswadesh/gocommerce/ext/contact.

How it works

What it does not do

Read these before an order depends on it. No store is known to run GoCommerce in production yet, so these come from the code, not from anyone’s experience.

FAQ

Questions about the Contact form module

What does the storefront post?

POST /x/contact/messages takes name, email and body, with an optional phone, subject and order_number. POST /x/contact/enquiries takes name, email and body with product_id or product_slug, and an optional phone, variant_id and quantity. Both answer 201 with the status new.

What happens with no email provider set up?

Messages and enquiries are still kept in the inbox. The announcement to the store’s address is best effort, so nobody is told by email. A reply is refused with 409, the drawer says why beside the button, and the text stays in the box.

Can staff reply to shoppers?

Not by default. Staff read the inbox with contact.read; replying needs contact.reply, which managers hold. A store that wants staff answering grants it on the Roles screen.

How is spam kept out?

A honeypot field no person sees, and one limit for both forms of ten messages a minute from one address. Every attempt counts, so alternating between the two forms does not double the allowance. There is no captcha.

Can I turn off product enquiries and keep the contact form?

Yes. Switch off “Take product enquiries” on the Plugins screen and POST /x/contact/enquiries answers 404, while the contact form stays open. Switching off the Contact form plugin closes both.

Source

Everything on this page is read from ext/contact in the GoCommerce repository, MIT licensed. When this page and the code disagree, the code is right and this page is out of date.

Try it against a store of your own

The backend + admin quick start runs GoCommerce’s API and admin on your own machine, without a storefront. Add this module to it and try it on test orders before a real one depends on it.

Chat on WhatsApp