sendgrid

package module
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 8, 2026 License: MIT Imports: 10 Imported by: 0

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.

type Sender

type Sender struct {
	// contains filtered or unexported fields
}

Sender sends emails through the SendGrid v3 API.

func New

func New(cfg Config) (*Sender, error)

New creates a new SendGrid Sender.

func (*Sender) Close

func (s *Sender) Close() error

Close is a no-op for SendGrid (HTTP is stateless).

func (*Sender) Send

func (s *Sender) Send(ctx context.Context, e *email.Email) error

Send sends an email through SendGrid. It implements email.Sender.