Documentation
¶
Overview ¶
Package sendgrid provides a SendGrid provider adapter for the goemail library.
It implements the email.Sender interface using the SendGrid v3 Web API, allowing you to send emails through SendGrid without pulling in any external SDK dependencies.
Usage ¶
sender, err := sendgrid.New(sendgrid.Config{
APIKey: os.Getenv("SENDGRID_API_KEY"),
})
if err != nil {
log.Fatal(err)
}
defer sender.Close()
e, _ := email.NewEmail().
SetFrom("sender@example.com").
AddTo("recipient@example.com").
SetSubject("Hello from SendGrid").
SetBody("Plain text body").
Build()
err = sender.Send(context.Background(), e)
Example ¶
package main
import (
"context"
"fmt"
"net/http"
"net/http/httptest"
email "github.com/KARTIKrocks/goemail"
"github.com/KARTIKrocks/goemail/providers/sendgrid"
)
func main() {
// Create a test server to stand in for the SendGrid API.
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusAccepted)
}))
defer srv.Close()
sender, err := sendgrid.New(sendgrid.Config{
APIKey: "test-api-key",
BaseURL: srv.URL,
})
if err != nil {
fmt.Printf("error: %v\n", err)
return
}
defer sender.Close()
e, err := email.NewEmail().
SetFrom("sender@example.com").
AddTo("recipient@example.com").
SetSubject("Hello from SendGrid").
SetBody("This is a test email.").
Build()
if err != nil {
fmt.Printf("failed to build email: %v\n", err)
return
}
if err = sender.Send(context.Background(), e); err != nil {
fmt.Printf("send error: %v\n", err)
return
}
fmt.Println("email sent successfully")
}
Output: email sent successfully
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config struct {
// APIKey is the SendGrid API key (required).
APIKey string
// BaseURL is the SendGrid API base URL.
// Default: "https://api.sendgrid.com".
BaseURL string
// HTTPClient is the HTTP client used for API calls.
// Default: http.DefaultClient.
HTTPClient *http.Client
}
Config holds the SendGrid provider configuration.
Click to show internal directories.
Click to hide internal directories.