Documentation
¶
Overview ¶
Package gomail provides a simple and efficient way to send emails using SMTP protocol. It supports various features including HTML templates, attachments, connection pooling, rate limiting, TLS configuration, and asynchronous sending.
Basic Usage:
mail := &gomail.Mail{
From: "[email protected]",
Name: "Sender Name",
Host: "smtp.example.com",
Port: "587",
User: "username",
Pass: "password",
}
err := mail.SetSubject("Test Email").
SetContent("This is a test email.").
SetTo("[email protected]").
Send()
Features:
- Connection pooling for better performance
- HTML template support with caching
- File attachments (regular and streaming)
- Asynchronous sending
- TLS support (STARTTLS and Direct TLS)
- CC and BCC recipients
- Rate limiting
- Email preview
- Configurable timeouts and keep-alive
- Custom template functions
Connection Pooling:
The package implements connection pooling to reuse SMTP connections:
mail.SetPoolSize(5) // Set pool size to 5 connections
HTML Templates:
Send emails using HTML templates with support for custom functions:
engine := &TemplateEngine{
BaseDir: "templates",
DefaultExt: ".html",
FuncMap: template.FuncMap{
"upper": strings.ToUpper,
},
}
mail.SetTemplateEngine(engine)
mail.RenderTemplate("welcome", data)
Rate Limiting:
Control email sending rate:
mail.SetRateLimit(&RateLimit{
Enabled: true,
PerSecond: 2, // 2 emails per second
})
TLS Configuration:
Configure TLS settings:
mail.SetTLSConfig(&TLSConfig{
StartTLS: true,
InsecureSkipVerify: false,
ServerName: "smtp.example.com",
})
Asynchronous Sending:
Send emails asynchronously:
result := mail.SetSubject("Async Email").
SetContent("Content").
SetTo("[email protected]").
SendAsync()
if err := <-result; err != nil {
log.Printf("Failed to send: %v", err)
}
Attachments:
Add file attachments:
attachments := map[string][]byte{
"file.txt": []byte("content"),
}
mail.SetAttachment(attachments)
For large files, use streaming attachments:
file, _ := os.Open("large-file.zip")
defer file.Close()
attachments := []AttachmentReader{
{
Name: "large-file.zip",
Reader: file,
Size: fileInfo.Size(),
},
}
mail.SetStreamAttachment(attachments)
Email Preview:
Preview email content before sending:
preview, err := mail.PreviewEmail()
if err != nil {
log.Printf("Preview error: %v", err)
}
fmt.Println(preview)
Index ¶
- Constants
- func SimpleRenderTemplate(filePath string, data map[string]any) (string, error)
- type Attachment
- type AttachmentReader
- type ContentType
- type Mail
- func (m *Mail) PreviewEmail() (string, error)
- func (m *Mail) RenderTemplate(name string, data any) error
- func (m *Mail) Send() error
- func (m *Mail) SendAsync() chan error
- func (m *Mail) SendHtml(filePath string, data map[string]any) error
- func (m *Mail) SetAttachment(attachments map[string][]byte) *Mail
- func (m *Mail) SetBcc(bcc ...string) *Mail
- func (m *Mail) SetCc(cc ...string) *Mail
- func (m *Mail) SetContent(content string) *Mail
- func (m *Mail) SetContentType(contentType ContentType) *Mail
- func (m *Mail) SetFrom(from string) *Mail
- func (m *Mail) SetHost(host string) *Mail
- func (m *Mail) SetKeepAlive(keepAlive time.Duration) *Mail
- func (m *Mail) SetName(name string) *Mail
- func (m *Mail) SetPass(pass string) *Mail
- func (m *Mail) SetPoolSize(size int) *Mail
- func (m *Mail) SetPort(port string) *Mail
- func (m *Mail) SetRateLimit(limit *RateLimit) *Mail
- func (m *Mail) SetStreamAttachment(attachments []AttachmentReader) *Mail
- func (m *Mail) SetSubject(subject string) *Mail
- func (m *Mail) SetTLSConfig(config *TLSConfig) *Mail
- func (m *Mail) SetTemplateEngine(engine *TemplateEngine) *Mail
- func (m *Mail) SetTimeout(timeout time.Duration) *Mail
- func (m *Mail) SetTo(to ...string) *Mail
- func (m *Mail) SetUser(user string) *Mail
- type Pool
- type RateLimit
- type TLSConfig
- type TemplateEngine
Constants ¶
const ( DefaultTimeout = 30 * time.Second DefaultKeepAlive = 60 * time.Second DefaultPoolSize = 10 )
Default configuration
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Attachment ¶ added in v1.2.0
Attachment represents an email attachment with metadata
type AttachmentReader ¶ added in v1.2.0
AttachmentReader represents a streaming attachment
type ContentType ¶ added in v1.2.0
type ContentType string
ContentType represents email content type
const ( TextPlain ContentType = "text/plain" TextHTML ContentType = "text/html" TextMarkdown ContentType = "text/markdown" )
type Mail ¶
type Mail struct {
From string
Name string
Host string
Port string
User string
Pass string `json:"-"` // Password will be omitted from JSON
Subject string
Content string
To []string
Cc []string
Bcc []string
Attachments map[string][]byte
Timeout time.Duration
KeepAlive time.Duration
ContentType ContentType
TemplateEngine *TemplateEngine
// contains filtered or unexported fields
}
Mail represents an email message with all its configuration
func (*Mail) PreviewEmail ¶ added in v1.2.0
PreviewEmail returns a preview of the email content
func (*Mail) RenderTemplate ¶ added in v1.2.0
RenderTemplate renders a template with the given data
func (*Mail) SendAsync ¶ added in v1.2.0
SendAsync sends the email asynchronously and returns a channel for the result
func (*Mail) SendHtml ¶ added in v1.2.0
SendFile loads an HTML file and renders it with dynamic data
func (*Mail) SetAttachment ¶
SetAttachment sets the email attachments
func (*Mail) SetContent ¶
SetContent sets the email content
func (*Mail) SetContentType ¶ added in v1.2.0
func (m *Mail) SetContentType(contentType ContentType) *Mail
SetContentType sets the content type of the email
func (*Mail) SetKeepAlive ¶
SetKeepAlive sets the keep-alive duration
func (*Mail) SetPoolSize ¶ added in v1.2.0
SetPoolSize sets the connection pool size
func (*Mail) SetRateLimit ¶ added in v1.2.0
SetRateLimit configures rate limiting
func (*Mail) SetStreamAttachment ¶ added in v1.2.0
func (m *Mail) SetStreamAttachment(attachments []AttachmentReader) *Mail
SetStreamAttachment sets streaming attachments for the email
func (*Mail) SetSubject ¶
SetSubject sets the email subject
func (*Mail) SetTLSConfig ¶ added in v1.2.0
SetTLSConfig sets the TLS configuration
func (*Mail) SetTemplateEngine ¶ added in v1.2.0
func (m *Mail) SetTemplateEngine(engine *TemplateEngine) *Mail
SetTemplateEngine configures the template engine
func (*Mail) SetTimeout ¶
SetTimeout sets the timeout duration
type Pool ¶ added in v1.2.0
type Pool struct {
// contains filtered or unexported fields
}
Pool structure