Documentation
¶
Overview ¶
Package email provides a flexible email service abstraction that supports multiple providers (SendGrid, SMTP, local development).
Index ¶
- Variables
- type Address
- type Attachment
- type Config
- type LocalService
- func (s *LocalService) ClearMessages()
- func (s *LocalService) GetMessages() []*Message
- func (s *LocalService) GetProvider() string
- func (s *LocalService) Send(ctx context.Context, message *Message) error
- func (s *LocalService) SendBatch(ctx context.Context, messages []*Message) error
- func (s *LocalService) SendTemplate(ctx context.Context, templateName string, data interface{}, ...) error
- func (s *LocalService) ValidateEmail(email string) error
- type Message
- type SendGridService
- func (s *SendGridService) GetProvider() string
- func (s *SendGridService) Send(ctx context.Context, message *Message) error
- func (s *SendGridService) SendBatch(ctx context.Context, messages []*Message) error
- func (s *SendGridService) SendTemplate(ctx context.Context, templateName string, data interface{}, ...) error
- func (s *SendGridService) ValidateEmail(email string) error
- type Service
Constants ¶
This section is empty.
Variables ¶
var StandardTemplates = map[string]string{
"welcome": `
<!DOCTYPE html>
<html>
<head>
<title>Welcome</title>
</head>
<body>
<h1>Welcome {{.Name}}!</h1>
<p>Thank you for joining us.</p>
</body>
</html>`,
"reset_password": `
<!DOCTYPE html>
<html>
<head>
<title>Reset Password</title>
</head>
<body>
<h1>Reset Your Password</h1>
<p>Click <a href="{{.ResetLink}}">here</a> to reset your password.</p>
<p>This link will expire in 1 hour.</p>
</body>
</html>`,
"notification": `
<!DOCTYPE html>
<html>
<head>
<title>Notification</title>
</head>
<body>
<h1>{{.Title}}</h1>
<p>{{.Message}}</p>
</body>
</html>`,
}
StandardTemplates provides standard email templates
Functions ¶
This section is empty.
Types ¶
type Attachment ¶
type Attachment struct {
Content string `json:"content"` // Base64 encoded content
Type string `json:"type"` // MIME type
Filename string `json:"filename"` // File name
Disposition string `json:"disposition"` // inline or attachment
ContentID string `json:"content_id,omitempty"` // For inline attachments
}
Attachment represents an email attachment
func AttachmentFromFile ¶
func AttachmentFromFile(filename string, contentType string) (*Attachment, error)
AttachmentFromFile creates an attachment from a file
type Config ¶
type Config struct {
Provider string // sendgrid, smtp, local
APIKey string // For API-based providers
FromEmail string // Default from email
FromName string // Default from name
SMTPHost string // For SMTP provider
SMTPPort int // For SMTP provider
SMTPUser string // For SMTP provider
SMTPPassword string // For SMTP provider
Templates map[string]string // Template name -> template content
IsDev bool // Development mode flag
}
Config contains email service configuration
type LocalService ¶
type LocalService struct {
// contains filtered or unexported fields
}
LocalService implements Service for local development
func NewLocalService ¶
func NewLocalService(config Config) *LocalService
NewLocalService creates a new local email service for development
func (*LocalService) ClearMessages ¶
func (s *LocalService) ClearMessages()
ClearMessages clears all stored messages (for testing)
func (*LocalService) GetMessages ¶
func (s *LocalService) GetMessages() []*Message
GetMessages returns all messages sent (for testing)
func (*LocalService) GetProvider ¶
func (s *LocalService) GetProvider() string
GetProvider returns the provider name
func (*LocalService) Send ¶
func (s *LocalService) Send(ctx context.Context, message *Message) error
Send logs the email locally
func (*LocalService) SendBatch ¶
func (s *LocalService) SendBatch(ctx context.Context, messages []*Message) error
SendBatch sends multiple emails locally
func (*LocalService) SendTemplate ¶
func (s *LocalService) SendTemplate(ctx context.Context, templateName string, data interface{}, recipients []string) error
SendTemplate sends a templated email locally
func (*LocalService) ValidateEmail ¶
func (s *LocalService) ValidateEmail(email string) error
ValidateEmail validates an email address
type Message ¶
type Message struct {
From Address `json:"from"`
To []Address `json:"to"`
CC []Address `json:"cc,omitempty"`
BCC []Address `json:"bcc,omitempty"`
ReplyTo *Address `json:"reply_to,omitempty"`
Subject string `json:"subject"`
Text string `json:"text,omitempty"`
HTML string `json:"html,omitempty"`
Attachments []Attachment `json:"attachments,omitempty"`
Headers map[string]string `json:"headers,omitempty"`
Metadata map[string]string `json:"metadata,omitempty"`
TemplateID string `json:"template_id,omitempty"`
TemplateData map[string]interface{} `json:"template_data,omitempty"`
}
Message represents an email message
type SendGridService ¶
type SendGridService struct {
// contains filtered or unexported fields
}
SendGridService implements Service using SendGrid
func NewSendGridService ¶
func NewSendGridService(config Config) (*SendGridService, error)
NewSendGridService creates a new SendGrid email service
func (*SendGridService) GetProvider ¶
func (s *SendGridService) GetProvider() string
GetProvider returns the provider name
func (*SendGridService) Send ¶
func (s *SendGridService) Send(ctx context.Context, message *Message) error
Send sends an email via SendGrid
func (*SendGridService) SendBatch ¶
func (s *SendGridService) SendBatch(ctx context.Context, messages []*Message) error
SendBatch sends multiple emails in batch
func (*SendGridService) SendTemplate ¶
func (s *SendGridService) SendTemplate(ctx context.Context, templateName string, data interface{}, recipients []string) error
SendTemplate sends a templated email via SendGrid
func (*SendGridService) ValidateEmail ¶
func (s *SendGridService) ValidateEmail(email string) error
ValidateEmail validates an email address
type Service ¶
type Service interface {
// Send sends an email message
Send(ctx context.Context, message *Message) error
// SendTemplate sends a templated email
SendTemplate(ctx context.Context, templateName string, data interface{}, recipients []string) error
// SendBatch sends multiple emails in batch
SendBatch(ctx context.Context, messages []*Message) error
// ValidateEmail validates an email address
ValidateEmail(email string) error
// GetProvider returns the current provider name
GetProvider() string
}
Service defines the email service interface
func NewService ¶
NewService creates a new email service based on configuration