How to Use Notify with Go
Send email with Go and net/http.
Send email from Go using the standard library.
Prerequisites
- API key — authenticate every request with the
x-api-keyheader - Verified domain (optional) — required for production sends from a custom
fromaddress - Go 1.21+ recommended
Send an email
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"os"
)
func main() {
payload := map[string]string{
"to": "user@example.com",
"subject": "Welcome aboard!",
"message": "<h1>Welcome!</h1><p>Thanks for joining us.</p>",
}
body, err := json.Marshal(payload)
if err != nil {
panic(err)
}
req, err := http.NewRequest(
http.MethodPost,
"https://notify.cx/api/public/v1/email/send",
bytes.NewReader(body),
)
if err != nil {
panic(err)
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("x-api-key", os.Getenv("NOTIFY_API_KEY"))
res, err := http.DefaultClient.Do(req)
if err != nil {
panic(err)
}
defer res.Body.Close()
respBody, err := io.ReadAll(res.Body)
if err != nil {
panic(err)
}
if res.StatusCode < 200 || res.StatusCode >= 300 {
panic(fmt.Sprintf("Notify error (%d): %s", res.StatusCode, respBody))
}
fmt.Println(string(respBody))
}
API response:
{
"success": true,
"data": {
"messageId": "01000194b1dd1c04-b51e7343-6808-4a68-b2af-845feae57f8b-000000"
}
}
Plain text message
Use a plain string in message when you do not need HTML:
payload := map[string]string{
"to": "user@example.com",
"subject": "Welcome aboard!",
"message": "Thanks for joining us!",
}
See Sending Emails for what gets delivered for each format.