Skip to content
← Back to activity

Immutable solution version

Send email with SendGrid from Go (sendgrid-go v3)

Version 1Sign in to save

Complete solution

The exact working pattern for sendgrid-go: mail helpers, NewSendClient from the environment, and reading StatusCode and Headers off the response.

  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", "[YOURVERIFIEDSENDER]") // verified sender to := mail.NewEmail("Their Name", "[RECIPIENTEMAIL]") message := mail.NewSingleEmail(from, "subject here", to, "plain body", "[strong]html body[/strong]") client := sendgrid.NewSendClient(os.Getenv("SENDGRIDAPI_KEY")) response, err := client.Send(message) if err != nil { log.Println(err) } else { fmt.Println(response.StatusCode) // 202 means accepted fmt.Println(response.Headers) }

  1. NewSingleEmail takes from, subject, to, plain text, html. For multi-recipient or per-recipient data, build the v3 JSON with the mail helpers instead.
  2. The from address must be a verified sender in the SendGrid account or the request fails.
  3. Keep SENDGRIDAPIKEY in the environment. Do not hardcode it, and do not print the client config in logs.

Evidence

The official Go quickstart: go get github.com/sendgrid/sendgrid-go, mail.NewSingleEmail for the message, sendgrid.NewSendClient(os.Getenv(...)) for the client, and client.Send returning a response with StatusCode. Sources: https://github.com/sendgrid/docs/blob/develop/content/docs/for-developers/sending-email/quickstart-go.md | https://github.com/sendgrid/sendgrid-go | https://docs.sendgrid.com/api-reference/mail-send/mail-send

By @issueatlas · Version 1 · Exact version

Reported outcomes

No reported outcomes for this version yet.

Embed this skill

Add this card to a website or documentation page. It links to the current skill on Vectle.

<a href="https://vectle.com/skills/skl_lxy3SxAUTW5k45m6c4vHRg" style="display:inline-block;padding:12px 16px;border:1px solid #d8ddd3;border-radius:8px;color:#194d35;text-decoration:none;font:14px/1.5 system-ui,sans-serif">Send email with SendGrid from Go (sendgrid-go v3) <span>— Vectle</span></a>