Quick setup examples
Go
Send your first email from Go using net/http against the MailBlastr API.
This guide sends email by POSTing JSON to https://www.mailblastr.com/api/emails using only the standard-library net/http and encoding/json, so there are no third-party dependencies. Want the typed client instead? Send emails with Go installs the official module.
Prerequisites
- A MailBlastr API key.
- A verified domain to send from.
- Go 1.22 or newer if you use the official module (that is the floor in its
go.mod). The standard-library example below builds on Go 1.16 and up.
1. Set your API key
.env
MAILBLASTR_API_KEY=mb_xxxxxxxxx2. Send email using HTML
The easiest way to send an email is with the html field.
main.go
package main
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"os"
)
func main() {
payload, _ := json.Marshal(map[string]any{
"from": "Acme <onboarding@yourdomain.com>",
"to": []string{"delivered@mailblastr.dev"},
"subject": "Hello from Go",
"html": "<strong>hello world</strong>",
})
req, _ := http.NewRequest("POST", "https://www.mailblastr.com/api/emails", bytes.NewReader(payload))
req.Header.Set("Authorization", "Bearer "+os.Getenv("MAILBLASTR_API_KEY"))
req.Header.Set("Content-Type", "application/json")
// Required: the API answers a missing User-Agent with 403 validation_error.
req.Header.Set("User-Agent", "acme-app/1.0")
res, err := http.DefaultClient.Do(req)
if err != nil {
fmt.Println(err.Error())
return
}
defer res.Body.Close()
var out struct {
ID string `json:"id"`
}
json.NewDecoder(res.Body).Decode(&out)
fmt.Println(out.ID)
}Drop this into a Chi or Gin handler to send from a web request. See Send an email for
cc, bcc, reply_to, and more.