Documentation
¶
Overview ¶
Package goat provides email delivery with templating capabilities.
go-at simplifies sending emails from Go applications with built-in templating and support for multiple delivery providers (SendGrid and Brevo).
Basic usage:
service := goat.NewSendgridService("api-key", "Your Name", "you@company.com")
restore := goat.SetSenderService(service)
defer restore()
template := goat.Template{
Name: "welcome",
ContentRaw: "Hello {{.Name}}!",
Data: map[string]string{"Name": "John"},
}
content, _ := template.Render()
err := goat.Send("user@example.com", "Welcome", content, content)
To use Brevo instead of SendGrid, create the service with NewBrevoService:
service := goat.NewBrevoService("api-key", "Your Name", "you@company.com")
Attach files (e.g. a PDF or calendar invite) with WithAttachment, or embed an image inline via WithInlineAttachment and reference it from HTML as cid:<ContentID>:
pdf, _ := os.ReadFile("invoice.pdf")
msg := goat.NewEmailMessage("user@example.com", "Your invoice", content, content).
WithAttachment("invoice.pdf", "application/pdf", pdf)
err := goat.Send(msg)
For more details, see README.md.
Index ¶
- func IsEmailValid(email string) bool
- func Send(message *EmailMessage) error
- func SetSenderService(service SenderService) func()
- type Attachment
- type BrevoClient
- type BrevoService
- type EmailMessage
- func (m *EmailMessage) WithAttachment(filename, contentType string, content []byte) *EmailMessage
- func (m *EmailMessage) WithAttachments(attachments ...Attachment) *EmailMessage
- func (m *EmailMessage) WithHeader(key, value string) *EmailMessage
- func (m *EmailMessage) WithHeaders(headers map[string]string) *EmailMessage
- func (m *EmailMessage) WithInlineAttachment(filename, contentType string, content []byte, contentID string) *EmailMessage
- func (m *EmailMessage) WithReplyTo(name, address string) *EmailMessage
- type ReplyTo
- type SendResult
- type SenderService
- type SendgridClient
- type SendgridService
- type Template
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func IsEmailValid ¶
func Send ¶
func Send(message *EmailMessage) error
Send directly exposes the current sender service Send function.
func SetSenderService ¶
func SetSenderService(service SenderService) func()
SetSenderService affect a new repository to the global service singleton
Types ¶
type Attachment ¶ added in v1.2.1
type Attachment struct {
Filename string
ContentType string // MIME type, e.g. "application/pdf", "text/calendar"
Content []byte // raw bytes (not base64)
ContentID string // optional; when set, the attachment is inline
}
Attachment represents a file attached to an email. Content holds the raw (un-encoded) bytes; the library base64-encodes it per provider. Set ContentID to embed the attachment inline (referenced from HTML as cid:<ContentID>). Note: Brevo (brevo-go v1.1.3) ignores ContentType and ContentID — it infers the MIME type from Filename and cannot embed inline; such attachments are sent as regular attachments.
type BrevoClient ¶ added in v1.1.0
type BrevoClient interface {
SendTransacEmail(ctx context.Context, sendSmtpEmail brevo.SendSmtpEmail) (brevo.CreateSmtpEmail, *http.Response, error)
}
BrevoClient is an interface for sending emails
type BrevoService ¶ added in v1.1.0
type BrevoService struct {
// contains filtered or unexported fields
}
BrevoService implements the SenderService interface using Brevo
func (*BrevoService) Send ¶ added in v1.1.0
func (s *BrevoService) Send(message *EmailMessage) error
Send sends an email using Brevo.
It returns only an error and is kept for backward compatibility; use SendWithResult when you need the message ID returned by Brevo.
func (*BrevoService) SendWithResult ¶ added in v1.2.2
func (s *BrevoService) SendWithResult(message *EmailMessage) (SendResult, error)
SendWithResult sends an email using Brevo and returns the message ID from the API response.
The returned SendResult.MessageID is the raw value of the response messageId field, kept verbatim including the surrounding chevrons (e.g. "<xxx@smtp-relay.mailin.fr>"). Brevo webhooks echo this same value in their message-id field, so it is the join key used to track delivery status.
type EmailMessage ¶ added in v1.2.0
type EmailMessage struct {
To string
Subject string
PlainTextContent string
HTMLContent string
ReplyTo *ReplyTo
Headers map[string]string
Attachments []Attachment
}
EmailMessage represents an email to be sent. Build one with NewEmailMessage and chain With* methods for optional fields.
func NewEmailMessage ¶ added in v1.2.0
func NewEmailMessage(to, subject, plainTextContent, htmlContent string) *EmailMessage
NewEmailMessage creates a new EmailMessage with the required fields.
func (*EmailMessage) WithAttachment ¶ added in v1.2.1
func (m *EmailMessage) WithAttachment(filename, contentType string, content []byte) *EmailMessage
WithAttachment adds a standard file attachment and returns the message for chaining.
func (*EmailMessage) WithAttachments ¶ added in v1.2.1
func (m *EmailMessage) WithAttachments(attachments ...Attachment) *EmailMessage
WithAttachments appends pre-built attachments and returns the message for chaining.
func (*EmailMessage) WithHeader ¶ added in v1.2.0
func (m *EmailMessage) WithHeader(key, value string) *EmailMessage
WithHeader adds a single custom header and returns the message for chaining.
func (*EmailMessage) WithHeaders ¶ added in v1.2.0
func (m *EmailMessage) WithHeaders(headers map[string]string) *EmailMessage
WithHeaders sets all custom headers at once and returns the message for chaining.
func (*EmailMessage) WithInlineAttachment ¶ added in v1.2.1
func (m *EmailMessage) WithInlineAttachment(filename, contentType string, content []byte, contentID string) *EmailMessage
WithInlineAttachment adds an inline attachment referenced from HTML as cid:<contentID> and returns the message for chaining.
func (*EmailMessage) WithReplyTo ¶ added in v1.2.0
func (m *EmailMessage) WithReplyTo(name, address string) *EmailMessage
WithReplyTo sets the reply-to address and returns the message for chaining.
type SendResult ¶ added in v1.2.2
type SendResult struct {
// MessageID is the provider message identifier, kept verbatim
// (e.g. Brevo returns it wrapped in chevrons: "<xxx@smtp-relay.mailin.fr>").
MessageID string
}
SendResult holds metadata returned by a provider after sending an email.
func SendWithResult ¶ added in v1.2.2
func SendWithResult(message *EmailMessage) (SendResult, error)
SendWithResult directly exposes the current sender service SendWithResult function.
type SenderService ¶
type SenderService interface {
Send(message *EmailMessage) error
SendWithResult(message *EmailMessage) (SendResult, error)
}
SenderService defines the interface for handling emails
func GetSenderService ¶
func GetSenderService() SenderService
GetSenderService is used to access the global service singleton
func NewBrevoService ¶ added in v1.1.0
func NewBrevoService(apiKey, senderName, senderEmail string) SenderService
NewBrevoService returns a new instance of BrevoService
func NewSendgridService ¶
func NewSendgridService(apiKey, senderName, senderEmail string) SenderService
NewSendgridService returns a new instance of SendgridService
type SendgridClient ¶
type SendgridClient interface {
Send(email *mail.SGMailV3) (*rest.Response, error)
SendWithContext(ctx context.Context, email *mail.SGMailV3) (*rest.Response, error)
}
SendgridClient is an interface for sending emails
type SendgridService ¶
type SendgridService struct {
// contains filtered or unexported fields
}
SendgridService implements the SenderService interface using SendGrid
func (*SendgridService) Send ¶
func (s *SendgridService) Send(message *EmailMessage) error
Send sends an email using SendGrid.
It returns only an error and is kept for backward compatibility; use SendWithResult when you need the message ID returned by SendGrid.
func (*SendgridService) SendWithResult ¶ added in v1.2.2
func (s *SendgridService) SendWithResult(message *EmailMessage) (SendResult, error)
SendWithResult sends an email using SendGrid and returns the message ID from the response.
SendGrid returns the message ID in the X-Message-Id response header rather than the body; the returned SendResult.MessageID holds that value (empty if the header is absent).