Documentation
¶
Overview ¶
Package mailpen provides a flexible email composition and sending system
Index ¶
- Constants
- Variables
- func DefaultFuncMap() template.FuncMap
- func DefaultTheme() map[string]any
- func GetThemeValue(theme map[string]any, path string) any
- func MergeFuncMaps(maps ...template.FuncMap) template.FuncMap
- func OpenFileAttachment(filepath string) (string, io.Reader, func() error, error)
- type Attachment
- type Builder
- func (b *Builder) Attach(filename string, data io.Reader) *Builder
- func (b *Builder) AttachWithContentType(filename string, data io.Reader, contentType ContentType) *Builder
- func (b *Builder) Bcc(addresses ...string) *Builder
- func (b *Builder) Build() (*Message, error)
- func (b *Builder) Cc(addresses ...string) *Builder
- func (b *Builder) From(address string) *Builder
- func (b *Builder) Layout(name string) *Builder
- func (b *Builder) Must() *Message
- func (b *Builder) ReplyTo(address string) *Builder
- func (b *Builder) Subject(subject string) *Builder
- func (b *Builder) Template(name string) *Builder
- func (b *Builder) To(addresses ...string) *Builder
- func (b *Builder) WithData(data map[string]any) *Builder
- type Capabilities
- type Card
- type CardGridData
- type Config
- type ContentType
- type DefaultProcessor
- type FooterData
- type HTMLProcessor
- type Mailpen
- type Manager
- func (m *Manager) AddFunc(name string, fn interface{}) error
- func (m *Manager) AddFuncs(funcs template.FuncMap) error
- func (m *Manager) AddSource(source TemplateSource) error
- func (m *Manager) ClearCache()
- func (m *Manager) RenderEmail(name string, data interface{}, layout string) (*RenderedEmail, error)
- type ManagerConfig
- type Message
- type Module
- type NotificationBoxData
- type NotificationButton
- type Option
- type Provider
- type RenderedEmail
- type SMTPClient
- type StringList
- type TableCell
- type TableData
- type TableHeader
- type TableRow
- type TemplateData
- type TemplateFormat
- type TemplateSource
- type TwoColumnData
- type TwoColumnRow
Constants ¶
const ( LayoutsDir = "layouts" PartialsDir = "partials" ComponentsDir = "components" EmailsDir = "emails" )
Variables ¶
var ( ErrNoContent = errors.New("email must have either plain text or HTML body") ErrNoSubject = errors.New("email must have a subject") )
Functions ¶
func DefaultFuncMap ¶
DefaultFuncMap returns the complete function map for the templates package.
func DefaultTheme ¶
DefaultTheme returns a theme map that works with built-in templates
func GetThemeValue ¶
GetThemeValue safely traverses a theme map using dot notation
func MergeFuncMaps ¶
MergeFuncMaps merges the provided function maps into a single function map.
func OpenFileAttachment ¶
OpenFileAttachment is a helper that returns a file reader and a cleanup function for an attachment file. The filename is extracted from the filepath. It returns the filename, a reader for the file, a cleanup function, and an error if the file cannot be opened. It is the caller's responsibility to close the file reader after sending the email using the cleanup function.
Example:
filename, reader, cleanup, err := OpenFileAttachment("path/to/file.txt")
if err != nil {
return err
}
defer cleanup()
msg, err := NewMessage().To("foo@example.com").Template("templates.tmpl").Attach(filename, reader).Build()
Types ¶
type Attachment ¶
type Attachment struct {
Filename string
Data io.Reader
ContentType ContentType
}
Attachment represents an email attachment
type Builder ¶
type Builder struct {
// contains filtered or unexported fields
}
Builder provides a fluent interface for constructing emails
func (*Builder) Attach ¶
Attach adds an attachment to the email. The data is read from the provided reader and the content type is inferred from the filename.
func (*Builder) AttachWithContentType ¶
func (b *Builder) AttachWithContentType(filename string, data io.Reader, contentType ContentType) *Builder
AttachWithContentType adds an attachment to the email with a specific content type. The data is read from the provided reader.
type Capabilities ¶
type Capabilities struct {
MaxRecipients int
MaxAttachmentSize int64
SupportsTemplates bool
SupportsHTMLOnly bool
SupportsScheduling bool
}
Capabilities defines what features a provider supports
type Card ¶
type Card struct {
ImageURL string
ImageAlt string
Title string
Description string
LinkURL string
LinkText string
}
Card represents a card in a card grid
type CardGridData ¶
type CardGridData struct {
Cards []Card
}
CardGridData represents the data needed to render a card grid
type Config ¶
type Config struct {
// From address
From string // From address
ReplyTo string // Reply-to address
// Company/Branding
BaseURL string // Base URL of the website
CompanyAddress1 string // The first line of the company address (usually the street address)
CompanyAddress2 string // The second line of the company address (usually the city, state, and ZIP code)
CompanyName string // Company name
LogoURL string // URL to the company logo
SupportEmail string // Support email address
SupportPhone string // Support phone number
WebsiteName string // Name of the website
WebsiteURL string // URL to the company website.
// HTML processor for processing HTML content
HTMLProcessor HTMLProcessor // HTML processor for processing HTML content
// Links
SiteLinks map[string]string // Site links
SocialMediaLinks map[string]string // Social media links
// Template configuration
FuncMap template.FuncMap // Additional template functions to add to the template engine. These will be merged with the default functions.
Sources []TemplateSource // Template sources
Theme map[string]any // Theme configuration
DefaultLayout string // Default layout to use for emails (defaults to "base")
}
Config holds the mailpen configuration
type ContentType ¶
type ContentType string
ContentType is a type wrapper for a string and represents the MIME type of the content being handled.
const ( // TypeAppOctetStream represents the MIME type for arbitrary binary data. TypeAppOctetStream ContentType = "application/octet-stream" // TypeMultipartAlternative represents the MIME type for a message body that can contain multiple alternative // formats. TypeMultipartAlternative ContentType = "multipart/alternative" // TypeMultipartMixed represents the MIME type for a multipart message containing different parts. TypeMultipartMixed ContentType = "multipart/mixed" // TypeMultipartRelated represents the MIME type for a multipart message where each part is a related file // or resource. TypeMultipartRelated ContentType = "multipart/related" // TypePGPSignature represents the MIME type for PGP signed messages. TypePGPSignature ContentType = "application/pgp-signature" // TypePGPEncrypted represents the MIME type for PGP encrypted messages. TypePGPEncrypted ContentType = "application/pgp-encrypted" // TypeTextHTML represents the MIME type for HTML text content. TypeTextHTML ContentType = "text/html" // TypeTextPlain represents the MIME type for plain text content. TypeTextPlain ContentType = "text/plain" )
func (ContentType) String ¶
func (c ContentType) String() string
String returns the string representation of the ContentType and implements the Stringer interface.
type DefaultProcessor ¶
type DefaultProcessor struct{}
DefaultProcessor provides a pass-through implementation
type FooterData ¶
type FooterData struct {
}
FooterData represents the data needed to render a footer
type HTMLProcessor ¶
HTMLProcessor defines the interface for processing HTML content
type Mailpen ¶
type Mailpen struct {
// contains filtered or unexported fields
}
Mailpen handles email sending operations
func New ¶
New creates a new Mailpen instance using the provided configuration and the default SMTP client
func (*Mailpen) NewTemplateData ¶
func (m *Mailpen) NewTemplateData() TemplateData
NewTemplateData creates a new templates data map with default values
type Manager ¶
type Manager struct {
// contains filtered or unexported fields
}
Manager handles templates loading, caching, and rendering
func NewManager ¶
func NewManager(config *ManagerConfig) (*Manager, error)
NewManager creates a new templates manager
func (*Manager) AddSource ¶
func (m *Manager) AddSource(source TemplateSource) error
AddSource adds a new template source to the manager
func (*Manager) ClearCache ¶
func (m *Manager) ClearCache()
ClearCache clears the email template cache
func (*Manager) RenderEmail ¶
func (m *Manager) RenderEmail(name string, data interface{}, layout string) (*RenderedEmail, error)
RenderEmail renders an email template with optional layout
type ManagerConfig ¶
type ManagerConfig struct {
FuncMap template.FuncMap
Processor HTMLProcessor
Sources []TemplateSource
Theme map[string]any
DefaultLayout string
}
ManagerConfig configures the templates manager
type Message ¶
type Message struct {
From string // Sender email address
To []string // List of recipient email addresses
Cc []string // List of CC email addresses
Bcc []string // List of BCC email addresses
ReplyTo string // Reply-to email address
Subject string // Email subject
Data map[string]any // Data to be passed to the templates
Layout string // Layout name to process
Template string // Template name to process
TextBody string // Text body of the email
HTMLBody string // HTML body of the email
Attachments []Attachment // List of attachments
}
Message represents the content and recipients of an email message
type NotificationBoxData ¶
type NotificationBoxData struct {
BgColor string // e.g., "#FFF3CD" for warning
BorderColor string // e.g., "#FFA500" for warning
Icon string // Optional icon URL
IconAlt string
Title string
TitleColor string
Message string
TextColor string
Button *NotificationButton
}
NotificationBoxData represents the data needed to render a notification box
type NotificationButton ¶
type NotificationButton struct {
BgColor string
BorderColor string
TextColor string
Text string
URL string
}
NotificationButton represents the type of button to render in a notification box
type Provider ¶
type Provider interface {
// Send sends an email message
Send(ctx context.Context, msg *Message) error
// Name returns the provider name
Name() string
// Validate validates a message before sending
Validate(msg *Message) error
// Capabilities returns the provider capabilities
Capabilities() Capabilities
}
Provider defines the interface for email providers
type RenderedEmail ¶
RenderedEmail represents a rendered email
type SMTPClient ¶
SMTPClient defines the interface for an SMTP client, mainly used for testing
type TableData ¶
type TableData struct {
Headers []TableHeader
Rows []TableRow
}
TableData represents the data needed to render a table
type TableHeader ¶
TableHeader represents a header in a table
type TemplateData ¶
func NewTemplateData ¶
func NewTemplateData(cfg *Config) TemplateData
func (TemplateData) Merge ¶
func (td TemplateData) Merge(data map[string]any) TemplateData
Merge combines the current TemplateData with the provided data map.
func (TemplateData) MergeKeys ¶
func (td TemplateData) MergeKeys(data map[string]any) TemplateData
MergeKeys merges data into the templates data, combining maps for existing keys instead of overwriting the entire map. This allows for more granular updates. It can merge maps for existing keys as well as add new keys.
type TemplateFormat ¶
type TemplateFormat string
TemplateFormat represents the format of a template
const ( FormatText TemplateFormat = "text" FormatHTML TemplateFormat = "html" )
func (TemplateFormat) Extension ¶
func (f TemplateFormat) Extension() string
Extension returns the file extension for a template format
type TemplateSource ¶
type TemplateSource struct {
Name string // Name of the template source
FS fs.FS // File system for the templates
}
TemplateSource represents a source of templates
type TwoColumnData ¶
type TwoColumnData struct {
Rows []TwoColumnRow
}
TwoColumnData represents the data needed to render a two-column layout
type TwoColumnRow ¶
TwoColumnRow represents a row in a two-column layout