# Send email from Go

`go get mailset.app/go`, then `client.Send(ctx, mailset.Message{...})`. The module needs nothing outside the standard library, and every input and answer is a struct.

## Install the Go SDK

One module, for Go 1.22 and later. Nothing else lands in your go.sum because of it.

go get:

```bash
go get mailset.app/go@v0.7.0
```

## Send your first email from Go

`mailset.NewClient()` reads `MAILSET_KEY` and returns an error when it finds no credential. `Send` takes a context and a `mailset.Message`; `mailset.Within(10 * time.Minute)` sets its deadline, so a code that can’t go out in time is refused. The receipt carries `ID` and `Status`.

```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 <hello@example.com>",
	To:        []string{"ana@example.org"},
	Subject:   "Your Acme code",
	Text:      "Your code is 123456.",
	HTML:      "<p>Your code is <b>123456</b>.</p>",
	DeliverBy: mailset.Within(10 * time.Minute),
})
if err != nil {
	log.Fatal(err)
}

fmt.Println(sent.ID, sent.Status)
```

## Delivery events with net/http

`hook` is a plain handler. It reads the body with `io.ReadAll` before anything decodes it, since the signature covers those bytes, and `mailset.VerifyWebhookHeader` checks them against `r.Header`. A failed check is a `*mailset.Error`, answered 401. 204 tells Mailset the batch is kept; until then it posts the batch again.

```go
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)
		return
	}
	secret := os.Getenv("MAILSET_HOOK_SECRET")
	events, err := mailset.VerifyWebhookHeader(
		body, r.Header, secret,
	)
	var refused *mailset.Error
	if errors.As(err, &refused) {
		http.Error(w, refused.Code, http.StatusUnauthorized)
		return
	}
	for _, event := range events {
		if event.Type != mailset.EventBounced {
			continue
		}
		if event.Bounce.Kind == mailset.BounceHard {
			log.Println("Stop writing to", event.Recipient)
		}
	}
	// 2xx once the events are kept: until then
	// Mailset posts the batch again.
	w.WriteHeader(http.StatusNoContent)
}

func main() {
	http.HandleFunc("POST /hooks/mailset", hook)
	log.Fatal(http.ListenAndServe(":8080", nil))
}
```

In the SDK’s tests: run with net/http/httptest.

## Send from your own domain

Your product sends as your own domain either way. What changes is where your team’s mail lives.

- **Sending only.** Your team’s mail stays at Google, Microsoft or wherever it is now. Mailset asks for DKIM, a bounce host and DMARC, and leaves your MX alone. [Send from a domain whose mail stays elsewhere](https://mailset.app/docs/recipes/sending-only)
- **Mailboxes too.** Your team’s mail moves to Mailset, with a real mailbox for every address on the domain and your product’s email on the same bill. [Set up a domain](https://mailset.app/guide/onboarding)

```bash
mailset domains add example.com --sending
mailset domains apply example.com --sending
mailset domains check example.com
mailset keys create --send --domain example.com --from hello@example.com --name "Acme API"
```

## What the Go SDK’s build checks

Each line comes from the SDK’s own build, which fails when one stops being true.

- **Go 1.22+**: The oldest its manifest allows (its manifest)
- **No dependencies**: Nothing else to install beside it (its manifest)
- **Typed throughout**: Structs and typed enums, checked by vet (a test)

## Where to go from here

- [The Go SDK](https://mailset.app/docs/sdk/go): Every task, from the client’s options to errors and retries.
- [Webhooks](https://mailset.app/docs/webhooks): The events Mailset posts about your product’s email, and how to check a request’s signature.
- [Give sign-in codes a deadline](https://mailset.app/docs/recipes/deadlines): Use deliverBy so a code that would arrive late is never sent.
- [Retry without sending twice](https://mailset.app/docs/recipes/retries): Make a send safe to repeat with an idempotency key or a Message-ID.
- [Handle bounces](https://mailset.app/docs/recipes/bounces): Tell a dead address from a full inbox, and keep your list clean.
- [Send many messages at once](https://mailset.app/docs/recipes/batches): Up to 100 messages a request, each with its own answer.

## Product email on every plan

Both include 10,000 emails a month, your product’s and your team’s together. Past that it’s $0.40 per 1,000 with pay as you go. Every plan and price: https://mailset.app/pricing

## Sending from Go, asked and answered

### Can goroutines share one client?

Yes. Make it with `NewClient` when the server starts and pass it around: it is safe for concurrent use.

### Do I need net/smtp?

No. The client sends with `net/http` to Mailset’s HTTPS API, so there’s no SMTP dialer, TLS setting or password to manage.

### Can another router serve the handler?

Any router that mounts an `http.HandlerFunc`, since `hook` is one. Keep `io.ReadAll` ahead of any JSON binding. The `POST /hooks/mailset` pattern in `main` is the standard library’s own, new in Go 1.22.

### How do I tell a refusal from a network error?

`errors.As(err, &refused)` with a `*mailset.Error` catches Mailset’s answers, and `refused.Code` names each one with a word that never changes. Anything else is the network or your context.

## Try it on your own domain

7 days free, with everything on. Make a key, run the example above and watch the receipt come back. Start at https://mailset.app/start

---

© 2026 Intenseloop, Inc. Mailset’s site, docs and guides are copyrighted. You may read them and use their code to build with Mailset. Don’t republish them or use them to build a copy of Mailset. Terms: https://mailset.app/terms#content