A powerful email management plugin for the Go Bolo framework that provides template-based email delivery with support for SMTP, message queue integration, and a REST API for managing emails and templates.
- Template-Based Emails: Create reusable email templates with Handlebars syntax
- SMTP Integration: Send emails via SMTP with TLS support
- Message Queue Support: Queue emails for delivery using RabbitMQ (via msgbroker plugin)
- CSS Inlining: Automatic CSS inlining for better email client compatibility using Premailer
- REST API: Full CRUD operations for emails and templates
- Database Storage: Persist email records and templates in MySQL/SQLite
- Retry Mechanism: Automatic retry logic for failed deliveries
- Multiple Recipients: Support for To, CC, BCC, and Reply-To fields
go get github.com/go-bolo/emails- Go 1.20+
- Go Bolo framework v1.1.0+
- Database (MySQL or SQLite)
- SMTP server credentials (for email delivery)
- (Optional) RabbitMQ via msgbroker plugin for queued delivery
Set the following environment variables:
# SMTP Configuration
SMTP_HOST=smtp.example.com
SMTP_PORT=587
SMTP_USER=your-username
SMTP_PASSWORD=your-password
SMTP_FROM=noreply@example.com
SMTP_REPLY_TO=support@example.com
# Enable email delivery (leave empty to disable)
ENABLE_EMAIL_DELIVERY=true
# Environment
GO_ENV=productionpackage main
import (
"github.com/go-bolo/bolo"
"github.com/go-bolo/emails"
)
func main() {
app := bolo.Init(&bolo.AppOptions{})
// Create and register the email plugin
app.RegisterPlugin(emails.NewPlugin(&emails.PluginCfg{}))
err = app.Bootstrap()
// app is ready to be used like:
err := app.StartHTTPServer()
if err != nil {
logrus.WithFields(logrus.Fields{
"error": fmt.Sprintf("%+v\n", err),
}).Error("error on StartHTTPServer")
}
}Define email types with default templates and variables:
emailPlugin.AddEmailTemplate("welcome-email", &emails.EmailType{
Label: "Welcome Email",
DefaultSubject: "Welcome to {{appName}}!",
DefaultHTML: "<h1>Hello {{userName}}!</h1><p>Welcome to our platform.</p>",
DefaultText: "Hello {{userName}}! Welcome to our platform.",
TemplateVariables: map[string]*emails.TemplateVariable{
"userName": {
Example: "John Doe",
Description: "The name of the user",
},
"appName": {
Example: "MyApp",
Description: "The application name",
},
},
})email, err := emails.NewEmailWithTemplate(&emails.EmailOpts{
To: "user@example.com",
TemplateName: "welcome-email",
Variables: emails.TemplateVariables{
"userName": "John Doe",
"appName": "MyApp",
},
})
if err != nil {
log.Fatal(err)
}
// Send immediately
err = email.Send()
// Or queue for background delivery
err = email.QueueToSend()emails.SendEmailAsync(&emails.EmailOpts{
To: "user@example.com",
TemplateName: "welcome-email",
Variables: emails.TemplateVariables{
"userName": "John Doe",
},
})email := &emails.Email{
To: "user@example.com",
From: "noreply@example.com",
Subject: "Custom Email",
HTML: "<p>This is a custom email</p>",
Text: "This is a custom email",
}
err := email.Send()The plugin automatically registers the following REST API endpoints:
GET /api/email- List all emails (with pagination and search)GET /api/email/:id- Get a specific emailPOST /api/email- Create a new email recordPUT /api/email/:id- Update an emailDELETE /api/email/:id- Delete an emailGET /api/email/count- Count emails
GET /api/email-template- List all email templatesGET /api/email-template/:id- Get a specific templatePOST /api/email-template- Create a new templatePUT /api/email-template/:id- Update a templateDELETE /api/email-template/:id- Delete a templateGET /api/email-template-types- Get all registered email types
q- Search term (searches subject and text)order/sort- Column to sort bysortDirection- Sort direction (ascordesc)limit- Number of results per pageoffset- Pagination offset
Templates use Handlebars syntax:
When the msgbroker plugin is available, emails are automatically queued to the notification-email-delivery queue instead of being sent immediately. This provides:
- Async Processing: Non-blocking email delivery
- Retry Logic: Automatic retry on failure (up to 3 attempts)
- Scalability: Distribute email sending across multiple workers
The plugin uses the following permissions (integrate with your ACL system):
create_email- Create email recordsfind_email- View emailsupdate_email- Update email recordsdelete_email- Delete emailscreate_email-template- Create templatesupdate_email-template- Update templatesdelete_email-template- Delete templates
go test -v ./....
├── Email.go # Core email sending logic
├── EmailModel.go # Email database model
├── EmailController.go # Email REST API controller
├── EmailTemplateModel.go # Template database model
├── EmailTemplateController.go # Template REST API controller
├── EmailType.go # Email type definitions
├── Plugin.go # Plugin initialization
├── emails.go # Helper functions
├── email-templates.go # Template utilities
├── migrations/ # Database migrations
│ └── 00001_init.go
└── *_test.go # Test files
- go-bolo/bolo - Core framework
- go-bolo/msgbroker - Message queue integration
- labstack/echo - HTTP router
- gorm.io/gorm - ORM
- gopkg.in/mail.v2 - SMTP client
- aymerick/raymond - Handlebars templates
- vanng822/go-premailer - CSS inlining
This project is part of the Go Bolo framework.
Contributions are welcome! Please feel free to submit a Pull Request.
For issues and questions:
- Create an issue on GitHub
- Check the Go Bolo documentation