Documentation ¶
Overview ¶
Package mail provides a simple mailer that sends emails using the SMTP protocol.
Index ¶
- Variables
- 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 gomail.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) ReplyTo(address string) *Builder
- func (b *Builder) Template(names ...string) *Builder
- func (b *Builder) To(addresses ...string) *Builder
- func (b *Builder) WithData(data any) *Builder
- func (b *Builder) WithTemplateData(data any) *Builder
- type Config
- type DefaultHTMLProcessor
- type HTMLProcessor
- type Mailer
- type Message
- type Module
- type SMTPClient
- type StringList
- type TemplateData
- type TemplateError
Constants ¶
This section is empty.
Variables ¶
var ( ErrNoContent = errors.New("email must have either plain text or HTML body") ErrNoSubject = errors.New("email must have a subject") )
Functions ¶
func OpenFileAttachment ¶ added in v0.0.18
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("template.tmpl").Attach(filename, reader).Build()
Types ¶
type Attachment ¶
type Attachment struct { Filename string Data io.Reader ContentType gomail.ContentType }
Attachment represents an email attachment
type Builder ¶ added in v0.0.18
type Builder struct {
// contains filtered or unexported fields
}
Builder provides a fluent interface for constructing emails
func (*Builder) Attach ¶ added in v0.0.18
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 ¶ added in v0.0.18
func (b *Builder) AttachWithContentType(filename string, data io.Reader, contentType gomail.ContentType) *Builder
AttachWithContentType adds an attachment to the email with a specific content type. The data is read from the provided reader.
func (*Builder) WithTemplateData ¶ added in v0.0.18
WithTemplateData is an alias for WithData for clarity
type Config ¶
type Config struct { // SMTP server configuration Host string // SMTP server host Port int // SMTP server port Username string // SMTP server username Password string // SMTP server password From string // From address AuthType string // Type of SMTP authentication (see the go-mail package for options). Default is LOGIN. TLSPolicy int // TLS policy for the SMTP connection (see the go-mail package for options). Default is opportunistic. // Template configuration TemplateFS fs.FS // File system for templates TemplatePath string // Path to the templates directory in the file system TemplateFuncMap template.FuncMap // Template function map that gets merged with the default function map from render // Retry configuration RetryCount int // Number of retry attempts for sending email RetryDelay time.Duration // Delay between retry attempts // HTML processor for processing HTML content HTMLProcessor HTMLProcessor // HTML processor for processing HTML content // 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. // Links SiteLinks map[string]string // Site links SocialMediaLinks map[string]string // Social media links }
Config holds the mailer configuration
type DefaultHTMLProcessor ¶
type DefaultHTMLProcessor struct{}
DefaultHTMLProcessor provides a pass-through implementation
type HTMLProcessor ¶
HTMLProcessor defines the interface for processing HTML content
type Mailer ¶
type Mailer struct {
// contains filtered or unexported fields
}
Mailer handles email sending operations
func NewMailer ¶
NewMailer creates a new Mailer instance using the provided configuration and the default SMTP client
func NewMailerWithClient ¶ added in v0.0.18
func NewMailerWithClient(cfg *Config, client SMTPClient) *Mailer
NewMailerWithClient creates a new Mailer with a provided SMTP client
func (*Mailer) NewTemplateData ¶
func (m *Mailer) NewTemplateData() TemplateData
NewTemplateData creates a new template data map with default values
type Message ¶ added in v0.0.18
type Message struct { To StringList // List of recipient email addresses Cc StringList // List of CC email addresses Bcc StringList // List of BCC email addresses Templates StringList // List of template names to proccess TemplateData any // Data to be passed to the templates Attachments []Attachment // List of attachments ReplyTo string // Reply-to email address }
Message represents the content and recipients of an email message
type Module ¶ added in v0.0.18
type Module struct {
// contains filtered or unexported fields
}
func NewMailerModule ¶ added in v0.0.18
type SMTPClient ¶ added in v0.0.18
SMTPClient defines the interface for an SMTP client, mainly used for testing
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 template 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 TemplateError ¶ added in v0.0.18
type TemplateError struct { TemplateName string OriginalErr error Phase string // "parse", "execute", "process" }
TemplateError provides context about template errors
func (*TemplateError) Error ¶ added in v0.0.18
func (e *TemplateError) Error() string