Go SDK

Go SDK

Install the Mailset Go SDK, send email, verify webhooks and handle errors, with examples that run.

  • Every public command238 commands, each a typed method
  • No dependenciesNothing else to install beside it
  • Typed throughoutStructs and typed enums, checked by vet
  • Retries built inTwo more tries, and a send never doubles
  • Timeouts per try30 seconds a try, then tried again
  • Idempotency keysA send repeated with its key goes once
On this page
  1. Requirements
  2. Install
  3. Create a client
  4. Send
  5. Send many
  6. Promotional email
  7. Idempotency
  8. Check the key
  9. Delivery search
  10. Webhooks
  11. Errors
  12. Build the message yourself
  13. Names
  14. Version and licence

Requirements

Runs onGo 1.22+

Install

Add the SDK to your project.

go get
go get mailset.app/[email protected]

Create a client

One client holds one MAILSET_KEY. Make it when your server starts and use it for every call.

Pass the key in

Where your app keeps its secrets elsewhere, hand the key to the client yourself. A token goes the same way.

Options

Everything has a default. Name a credential, an org, how long a try may take and how often a call is tried again, only where yours differ.

Authentication

Go
import (
	"log"

	mailset "mailset.app/go"
)

// One client for the whole server.
client, err := mailset.NewClient() // reads MAILSET_KEY
if err != nil {
	log.Fatal(err)
}
// Nothing is sent until you call a method, and it
// prints the kind of credential, never its value.
log.Println(client)
Pass the key in
import (
	"log"

	mailset "mailset.app/go"
)

// From your secret store or config, never in the code.
// Or WithToken(cfg.MailsetToken) with an API token.
client, err := mailset.NewClient(
	mailset.WithKey(cfg.MailsetKey),
)
if err != nil {
	log.Fatal(err)
}
Options
import (
	"context"
	"fmt"
	"log"
	"os"
	"time"

	mailset "mailset.app/go"
)

// A command that needs a passkey waits for
// someone to approve it.
onApproval := func(
	ctx context.Context, approval *mailset.Approval,
) error {
	fmt.Println("Approve it:", approval.URL)
	return nil
}

Send

Send one message and get its receipt. A message Mailset refuses is an error with a code, never a silent drop.

Give sign-in codes a deadlineHonor unsubscribes and complaintsHandle skipped recipientsGuide for Go

Go
import (
	"context"
	"fmt"
	"log"
	"time"

	mailset "mailset.app/go"
)

client, err := mailset.NewClient() // reads MAILSET_KEY
if err != nil {
	log.Fatal(err)
}
ctx := context.Background()

sent, err := client.Send(ctx, mailset.Message{
	From:      "Acme <moc.elpmaxe@olleh>",
	To:        []string{"gro.elpmaxe@ana"},

Send many

Send several messages in one call and get one result per message, in the same order. A refused message never stops the rest.

Send many messages at once

Go
import (
	"context"
	"fmt"
	"log"

	mailset "mailset.app/go"
)

client, err := mailset.NewClient() // reads MAILSET_KEY
if err != nil {
	log.Fatal(err)
}
ctx := context.Background()

results, err := client.SendMany(ctx, []mailset.Message{
	{
		From:    "Acme <moc.elpmaxe@olleh>",
		To:      []string{"gro.elpmaxe@ana"},

Promotional email

Send a newsletter or an offer with mode: 'promotional'. {{mailset_unsubscribe}} is a placeholder Mailset fills in for each message, with a signed one-click unsubscribe link unique to that recipient and that message. You never generate the link, and Mailset adds the matching List-Unsubscribe and List-Unsubscribe-Post headers itself.

Honor unsubscribes and complaintsHandle skipped recipients

Go
import (
	"context"
	"fmt"
	"log"
	"strings"

	mailset "mailset.app/go"
)

client, err := mailset.NewClient() // reads MAILSET_KEY
if err != nil {
	log.Fatal(err)
}
ctx := context.Background()

text := strings.Join([]string{
	"Three new things this month.",
	"",
	"Leave this list: {{mailset_unsubscribe}}",
}, "\n")
What Ana receives
Headers Mailset adds
List-Unsubscribe: <https://mailset.app/v1/unsubscribe/REjwEgpOG_zgHOqoFABR-wIAnGDtBkNRV-G7ho9WVWEYl8wMTIzNDU2Nzg5YWJjZGVmMDEyMzQ1NjcKYW5hQGV4YW1wbGUub3JnCmRfMDEyMzQ1Njc4OWFiY2RlZjAxMjM0NTY3Cm91dF8wMTIzNDU2Nzg5YWJjZGVmMDEyMzQ1Njc>
List-Unsubscribe-Post: List-Unsubscribe=One-Click
Text part
Three new things this month.

Leave this list: https://mailset.app/v1/unsubscribe/REjwEgpOG_zgHOqoFABR-wIAnGDtBkNRV-G7ho9WVWEYl8wMTIzNDU2Nzg5YWJjZGVmMDEyMzQ1NjcKYW5hQGV4YW1wbGUub3JnCmRfMDEyMzQ1Njc4OWFiY2RlZjAxMjM0NTY3Cm91dF8wMTIzNDU2Nzg5YWJjZGVmMDEyMzQ1Njc
HTML part

Three new things this month.

Leave this list

A sample link. Mailset makes a new one for every message.

What Ana receives without the placeholder
Text part
Three new things this month.

Unsubscribe: https://mailset.app/v1/unsubscribe/REjwEgpOG_zgHOqoFABR-wIAnGDtBkNRV-G7ho9WVWEYl8wMTIzNDU2Nzg5YWJjZGVmMDEyMzQ1NjcKYW5hQGV4YW1wbGUub3JnCmRfMDEyMzQ1Njc4OWFiY2RlZjAxMjM0NTY3Cm91dF8wMTIzNDU2Nzg5YWJjZGVmMDEyMzQ1Njc
HTML part

Three new things this month.

Unsubscribe

Mailset adds the last line of each part. The headers are the same.

Idempotency

Name a send with IdempotencyKey, and sending it again after a timeout or a crash can’t send it twice.

Retry without sending twice

Go
import (
	"context"
	"fmt"
	"log"

	mailset "mailset.app/go"
)

client, err := mailset.NewClient() // reads MAILSET_KEY
if err != nil {
	log.Fatal(err)
}
ctx := context.Background()

receipt := mailset.Message{
	From:           "Acme <moc.elpmaxe@olleh>",
	To:             []string{"gro.elpmaxe@ana"},
	Subject:        "Your receipt",
	Text:           "Thanks for your order.",
	IdempotencyKey: "receipt-8842",
}

Check the key

Ask what the key may do now. Call it when your server starts and once a day: each call keeps the key alive.

Keep a key alive

Go
import (
	"context"
	"fmt"
	"log"
	"strings"
	"time"

	mailset "mailset.app/go"
)

client, err := mailset.NewClient() // reads MAILSET_KEY
if err != nil {
	log.Fatal(err)
}
ctx := context.Background()

Delivery search

Look up what became of the mail this key sent, one line per recipient. For a support question or a nightly check.

Handle bounces

Go
import (
	"context"
	"fmt"
	"log"

	mailset "mailset.app/go"
)

client, err := mailset.NewClient() // reads MAILSET_KEY
if err != nil {
	log.Fatal(err)
}
ctx := context.Background()

// Did Ana get her receipt?
search := &mailset.DeliverySearchParams{
	Recipient: "gro.elpmaxe@ana",
	Since:     mailset.Day("2026-11-30"),

Webhooks

Check that a request came from Mailset and read its events. It needs no key and no client, so a service that only receives events uses it alone.

In a web framework

The signature covers the body exactly as it arrived. Read the raw body before anything parses it, and hand that to the check: a body parsed as JSON and written out again no longer matches.

WebhooksHandle bounces

Go
import (
	"fmt"
	"io"
	"log"
	"os"

	mailset "mailset.app/go"
)

// handle checks one request from your org's hook:
// its body as it came, and its Mailset-Signature.
// It needs no client and no key.
func handle(body []byte, signature string) error {
	secret := os.Getenv("MAILSET_HOOK_SECRET")
	events, err := mailset.VerifyWebhook(
		body, signature, secret,
	)
	if err != nil {
In a web framework
import (
	"errors"
	"io"
	"log"
	"net/http"
	"os"

	mailset "mailset.app/go"
)

// hook is Mailset's webhook endpoint, a plain
// net/http handler.
func hook(w http.ResponseWriter, r *http.Request) {
	// The signature covers the body as it came: read
	// its bytes. A json.Decoder on r.Body loses them.
	body, err := io.ReadAll(r.Body)
	if err != nil {
		http.Error(w, "unreadable", http.StatusBadRequest)

Errors

Every refusal is one kind of error. Branch on its Code, which never changes, and show its Message to people.

Retries and timeouts

A network failure or a 5xx answer is tried again, twice unless you set MaxRetries, waiting longer each time. One try may take 30 seconds unless you set Timeout. A refusal is never tried again: the same request gets the same answer.

Errors and limits

Go
import (
	"context"
	"errors"
	"fmt"
	"log"
	"time"

	mailset "mailset.app/go"
)

client, err := mailset.NewClient() // reads MAILSET_KEY
if err != nil {
	log.Fatal(err)
}
ctx := context.Background()

Build the message yourself

Get the message a send would build, without sending it: for a queue of your own, or a call to the API from another service.

messages.submit

Go
import (
	"encoding/base64"
	"fmt"
	"log"

	mailset "mailset.app/go"
)

message := mailset.Message{
	From:    "Acme <moc.elpmaxe@olleh>",
	To:      []string{"gro.elpmaxe@ana"},
	Subject: "Your Acme code",
	Text:    "Your code is 123456.",
}
raw, messageID, err := mailset.BuildMessage(message)
if err != nil {
	log.Fatal(err)
}

// The data that messages.submit takes.
data := base64.StdEncoding.EncodeToString(raw)

fmt.Println(messageID, len(data))

Names

A field keeps its name in every SDK, in each language’s own case. The commands write idempotencyKey, and Go writes IdempotencyKey.

Version and licence

This page is for version 0.7.0 of the Go SDK.

You may use the SDK and change it in your own apps and services, to call Mailset. You may not publish it or pass it on, by itself or inside another library.

Something missing, or not working as this page says? Ask us.