resend.go

100 lines
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
package emailers

import (
	"bytes"
	"embed"
	"encoding/json"
	"fmt"
	"html/template"
	"io"
	"net/http"

	"congo.gg/pkg/application"
)

// Resend sends emails via the Resend API
type Resend struct {
	application.BaseEmailer
	apiKey string
	from   string
	client *http.Client
}

// ResendOption configures the Resend emailer.
type ResendOption func(*Resend)

// WithHTTPClient sets a custom HTTP client for the Resend emailer.
// Useful for testing — inject a client that routes to a test server.
func WithHTTPClient(client *http.Client) ResendOption {
	return func(r *Resend) {
		r.client = client
	}
}

// NewResend creates a new Resend emailer
//
// Example:
//
//	//go:embed all:emails
//	var emails embed.FS
//
//	emailer := emailers.NewResend(emails, apiKey, "noreply@example.com", nil)
func NewResend(emails embed.FS, apiKey, from string, funcs template.FuncMap, opts ...ResendOption) *Resend {
	r := &Resend{
		apiKey: apiKey,
		from:   from,
		client: http.DefaultClient,
	}
	r.Init(emails, funcs)
	for _, opt := range opts {
		opt(r)
	}
	return r
}

// Send renders a template and sends it via Resend
func (r *Resend) Send(to, subject, templateName string, data map[string]any) error {
	body, err := r.Render(templateName, data)
	if err != nil {
		return fmt.Errorf("render email: %w", err)
	}

	return r.send(to, subject, body)
}

// send sends an email via the Resend API
func (r *Resend) send(to, subject, html string) error {
	payload := map[string]any{
		"from":    r.from,
		"to":      []string{to},
		"subject": subject,
		"html":    html,
	}

	jsonBody, err := json.Marshal(payload)
	if err != nil {
		return fmt.Errorf("marshal payload: %w", err)
	}

	req, err := http.NewRequest("POST", "https://api.resend.com/emails", bytes.NewReader(jsonBody))
	if err != nil {
		return fmt.Errorf("create request: %w", err)
	}

	req.Header.Set("Authorization", "Bearer "+r.apiKey)
	req.Header.Set("Content-Type", "application/json")

	resp, err := r.client.Do(req)
	if err != nil {
		return fmt.Errorf("send request: %w", err)
	}
	defer resp.Body.Close()

	if resp.StatusCode >= 400 {
		var respBody bytes.Buffer
		respBody.ReadFrom(io.LimitReader(resp.Body, 1<<20)) // 1MB cap
		return fmt.Errorf("resend API error: %s — %s", resp.Status, respBody.String())
	}

	return nil
}