Send HTML emails via SMTP.
RapidGo provides a simple SMTP mailer for sending HTML emails.
Configuration
MAIL_HOST=smtp.example.com
MAIL_PORT=587
MAIL_USERNAME=your-username
MAIL_PASSWORD=your-password
MAIL_FROM_ADDRESS=noreply@example.com
MAIL_FROM_NAME=MyApp
Creating a Mailer
import "github.com/RAiWorks/RapidGo/v2/core/mail"
// Reads all MAIL_* env vars automatically
mailer := mail.NewMailer()
Sending Email
err := mailer.Send(
"user@example.com",
"Welcome to MyApp",
"<h1>Welcome!</h1><p>Thanks for signing up.</p>",
)
if err != nil {
slog.Error("failed to send email", "error", err)
}
Using with Service Container
Register the mailer as a singleton in a provider:
func (p *MailProvider) Register(c *container.Container) {
c.Singleton("mailer", func(c *container.Container) interface{} {
return mail.NewMailer()
})
}
Resolve it in controllers:
mailer := container.MustMake[*mail.Mailer](c, "mailer")
mailer.Send(to, subject, body)
Mailer Fields
| Field | Env Variable | Default |
|---|---|---|
Host |
MAIL_HOST |
— |
Port |
MAIL_PORT |
587 |
Username |
MAIL_USERNAME |
— |
Password |
MAIL_PASSWORD |
— |
From |
MAIL_FROM_ADDRESS |
— |
FromName |
MAIL_FROM_NAME |
— |
The mailer uses TLS (mandatory) and SMTP PLAIN authentication via the go-mail library.