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.

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.

SendGrid module settings — 8 settings, 2 required
SettingEnvironment variableRequiredWhat it does
APIKey
API key
SENDGRID_API_KEY Yes A SendGrid API key with Mail Send permission.
From
From address
SENDGRID_FROM Yes A sender SendGrid has verified. There is no default: without it, nothing sends.
FromName
From name
SENDGRID_FROM_NAME No The name beside the address in the inbox.
ReplyTo
Reply-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

  1. Create a key and a senderIn SendGrid: an API key with Mail Send permission, and a sender address SendGrid has verified.
  2. 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.
  3. 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.
  4. 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

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 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.

Chat on WhatsApp