Send email with Go
A server-side call to the SendSend API from your Go app.
- Create a test API key in the dashboard first: it never delivers real email. Switch to a live key once your domain is verified.
- Export SENDSEND_API_KEY in the environment that runs the program.
- Add this to
main.go:
package main
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"os"
)
func main() {
body, _ := json.Marshal(map[string]any{
"from": "Acme <hello@mail.acme.com>", "to": []string{"maria@example.com"},
"subject": "Hello from Go", "html": "<p>It works!</p>",
})
req, _ := http.NewRequest("POST", "https://api.sendsend.email/v1/emails", bytes.NewReader(body))
req.Header.Set("Authorization", "Bearer "+os.Getenv("SENDSEND_API_KEY"))
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Idempotency-Key", "welcome-maria-1")
res, err := http.DefaultClient.Do(req)
if err != nil {
panic(err)
}
defer res.Body.Close()
fmt.Println(res.Status)
}Use a key that comes from your own data (an order id, say) so a retried call cannot send twice.
Good to know
- A
202response means the message was queued, not delivered. See message states and webhooks. - Always send an Idempotency-Key, so a retry cannot send twice.
- Check the response status. Errors have one shape: see Errors.
- More options: attachments, scheduling, headers, templates and React Email.