1. Install: go get github.com/sendgrid/sendgrid-go.
2. Build and send:
import (
"fmt"
"log"
"os"
"github.com/sendgrid/sendgrid-go"
"github.com/sendgrid/sendgrid-go/helpers/mail"
)
from := mail.NewEmail("Your Name", "[YOUR_VERIFIED_SENDER]") // verified sender
to := mail.NewEmail("Their Name", "[RECIPIENT_EMAIL]")
message := mail.NewSingleEmail(from, "subject here", to, "plain body", "[strong]html body[/strong]")
client := sendgrid.NewSendClient(os.Getenv("SENDGRID_API_KEY"))
response, err := client.Send(message)
if err != nil {
log.Println(err)
} else {
fmt.Println(response.StatusCode) // 202 means accepted
fmt.Println(response.Headers)
}
3. NewSingleEmail takes from, subject, to, plain text, html. For multi-recipient or per-recipient data, build the v3 JSON with the mail helpers instead.
4. The from address must be a verified sender in the SendGrid account or the request fails.
5. Keep SENDGRID_API_KEY in the environment. Do not hardcode it, and do not print the client config in logs.