Skip to content

Documentation

Send email in Go.

Works with: Go 1.21 or newer, standard library only.

Install

Get a key under API keys in the dashboard and verify a sending domain first (how). Then add the library:

Terminal
go get github.com/mailtruss/mailtruss-go

Send an email

This sends one message and passes an idempotency key, so repeating the request can never deliver it twice. Reads, deletes and requests with an idempotency key are retried on 429 and 5xx; a send without a key is never retried.

main.go
import mailtruss "github.com/mailtruss/mailtruss-go"
​
client := mailtruss.New(os.Getenv("MAILTRUSS_API_KEY"))
res, err := client.Emails.Send(ctx, mailtruss.SendEmailParams{
From: "hello@yourdomain.com", To: []string{"ana@example.com"}, Subject: "Welcome", HTML: "<p>Hi Ana</p>",
}, &mailtruss.RequestOptions{IdempotencyKey: "welcome-ana"})

More on what you can put in a message (cc, bcc, attachments, tags, templates, scheduling) is under Sending.

Verify webhook signatures

Mailtruss signs every webhook with the Standard Webhooks scheme. Check the signature before you trust a request. Pass the raw body and the request headers.

webhook.go
event, err := mailtruss.VerifyWebhook(rawBody, r.Header, os.Getenv("MAILTRUSS_WEBHOOK_SECRET"))
if err != nil {
http.Error(w, "invalid signature", http.StatusBadRequest)
return
}

Events, payloads and retries are on the Webhooks page.

Handle errors

Failures are *mailtruss.Error with Name, StatusCode and RequestID. Every error name, with its cause and fix, is in the error catalogue.

Other languages

Last updated

Something wrong or unclear on this page?

Report an issue