Messaging
SendGrid email for GoCommerce
A Go package that sends a GoCommerce store’s emails through SendGrid’s v3 API. Two settings are required — an API key and a verified sender — and the wording is the engine’s, edited in the admin.
- Messaging module
- 8 settings
- 5 tests
- ext/notify-sendgrid
What it does
The module registers a notifier on the engine’s email channel. When the engine has something to tell a shopper — an order placed, paid, shipped, delivered, cancelled, refunded or returned — it hands the module the event and its flat data, and the module sends one plain-text email through POST /v3/mail/send.
Installed with nothing configured, the module is idle. The package notes that the engine keeps writing every message to the log until an operator fills in Notifications › Setup Email, and that the settings screen and the doctor say so.
The wording, in the package’s words, is not the module’s at all: it asks the engine’s notification templates for the effective subject and body at send time, so what the operator edits in the panel is what goes out. No SDK — sending one email is a single JSON POST.
Configuration
Two settings are required: the API key and the from address. Set them in Config from your own main(), or leave Config empty and fill them in under Notifications › Setup Email.
| Setting | Environment variable | Required | What it does |
|---|---|---|---|
APIKeyAPI key | SENDGRID_API_KEY | Yes | A SendGrid API key with Mail Send permission. |
FromFrom address | SENDGRID_FROM | Yes | A sender SendGrid has verified. There is no default: without it, nothing sends. |
FromNameFrom name | SENDGRID_FROM_NAME | No | The name beside the address in the inbox. |
ReplyToReply-to address | — | No | Where a shopper’s reply lands; empty means the from address. |
Subjects | — | No | Overrides the engine’s subject line for an event, keyed by event name, for a store that keeps its wording in code. Checked when the store boots; a subject edited in the panel wins. Go only. |
Bodies | — | No | The same, for the plain-text body. Go only. |
BaseURL | — | No | Overrides https://api.sendgrid.com, for tests. Go only. |
Client | — | No | Replaces the HTTP client, which otherwise times out after 15 seconds. Go 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
- Create a key and a senderIn SendGrid: an API key with Mail Send permission, and a sender address SendGrid has verified.
- Install the moduleImport it and pass sendgrid.New to gocommerce.New, as below — or run the reference binary with -sendgrid, which reads SENDGRID_API_KEY, SENDGRID_FROM and SENDGRID_FROM_NAME.
- Give it bothKey and sender in Config, which starts it switched on, or under Notifications › Setup Email. A value typed into the panel counts on the next message, with no restart.
- Edit the wordingSubjects and bodies are the engine’s templates, edited under Notifications › Setup Email. Place an order and the confirmation goes out.
main.go
import (
"os"
"github.com/itswadesh/gocommerce/core"
sendgrid "github.com/itswadesh/gocommerce/ext/notify-sendgrid"
)
app, err := gocommerce.New(cfg,
sendgrid.New(sendgrid.Config{
APIKey: os.Getenv("SENDGRID_API_KEY"),
From: "orders@example.com",
}),
) The package doc’s own example, with its imports; cfg is your gocommerce.Config. With an empty sendgrid.Config the module installs idle and waits for the panel. Import path github.com/itswadesh/gocommerce/ext/notify-sendgrid.
How it works
-
The engine’s words
Subject and body come from the engine’s templates at send time — the panel’s edit if there is one, Config’s Subjects and Bodies if not, the engine’s default otherwise — rendered with Go’s text/template over the event’s flat data.
-
Idle until complete
Without both a key and a sender the module does not count as configured. The engine skips it, and the message goes to the log instead.
-
Rejections settle, outages retry
A 4xx other than 429 is logged and settled: a bad address will be rejected again. Unreachable, 429 or 5xx is returned as an error, and the engine retries the notification with backoff.
-
Overrides checked at boot
A Subjects or Bodies template that does not parse stops the store booting, rather than failing on the first sale.
-
Only events with wording
An event with no email template is skipped, not an error, so another module can add events this one has never heard of.
-
Settings without a restart
The panel’s values are read on every send and win over Config. The engine keeps a row per message in its notification log, and an operator can resend one from there.
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.
- Plain text onlyEach email is sent as text/plain. There is no HTML body, no attachment and no SendGrid dynamic template.
- Amounts in minor unitsThe engine hands templates totals in minor units — 49900, not 499.00 — and a store that wants them formatted edits the template.
- Two backends, two emailsEvery configured email backend gets every message. With SendGrid and Resend both switched on, a shopper gets two; and a retry after one fails re-sends through both.
- Refusals count as sentA message SendGrid refuses returns no error, so the engine’s notification log records it as sent. The refusal is only in the application log.
- No successful-send log lineUnlike the Resend module, a message SendGrid accepts leaves no line in the application log; the engine’s notification log is the record.
- No bounce trackingThe module registers no webhook. Bounces, complaints and opens stay in SendGrid.
FAQ
Questions about the SendGrid module
Why are two settings required?
SendGrid sends only from a sender it has verified, so there is no address the module could fall back on. Until both the key and the from address are set, the module is idle and messages go to the log.
Where do I change what the emails say?
Under Notifications › Setup Email, where the engine’s templates are edited. A store that keeps its wording in code can pass Subjects and Bodies in Config instead; an edit in the panel wins over both.
Can I use it without writing Go?
Yes. The reference binary’s -sendgrid flag installs it, reading SENDGRID_API_KEY, SENDGRID_FROM and SENDGRID_FROM_NAME, and the panel can hold the same settings.
Is this a SendGrid partnership?
No. The module calls SendGrid’s public API with a key you supply. SendGrid does not endorse GoCommerce, and no store is known to run this module in production yet.
Source
Everything on this page is read from ext/notify-sendgrid in the GoCommerce repository, MIT licensed. When this page and the code disagree, the code is right and this page is out of date.SendGrid is a trademark of its owner; this module talks to its public API and implies no endorsement. See trademarks.
Try it against a store of your own
The one-command stack gives you GoCommerce’s API and admin on your own machine in minutes. Add this module to it and try it on test orders before a real one depends on it.