feat(04-03): select memory, log, and SMTP mail drivers from config
- Load mail.* and SUMMER_MAIL__ overrides to pick memory, log, or smtp - Log headers and text without credentials; SMTP uses explicit TLS and go-mail - Reject CR/LF headers and invalid addresses and surface driver errors once Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -3,9 +3,12 @@ package postcard
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"git.golem15.com/golem15/summercms/backpack"
|
||||
"git.golem15.com/golem15/summercms/compass"
|
||||
"git.golem15.com/golem15/summercms/pact"
|
||||
)
|
||||
|
||||
@@ -69,6 +72,10 @@ func (m *mailer) Send(ctx context.Context, msg Message) error {
|
||||
return err
|
||||
}
|
||||
rendered.From = m.opts.From
|
||||
_, smtp := m.driver.(*SMTPDriver)
|
||||
if err := validateRendered(rendered, smtp); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := m.driver.Send(ctx, rendered); err != nil {
|
||||
return fmt.Errorf("postcard: %w", err)
|
||||
}
|
||||
@@ -81,15 +88,13 @@ func Activate[P interface{ ID() string }](app *backpack.App, _ []P) error {
|
||||
if app == nil {
|
||||
return fmt.Errorf("postcard: app is nil")
|
||||
}
|
||||
opts := Options{}
|
||||
if app.Config != nil {
|
||||
opts.From = strings.TrimSpace(app.Config.String("mail.from"))
|
||||
opts.CSS = app.Config.String("mail.css")
|
||||
opts.BrandCSS = app.Config.String("mail.brandCss")
|
||||
driver, opts, err := driverFromApp(app)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
m := &mailer{
|
||||
catalog: NewCatalog(),
|
||||
driver: NewMemoryDriver(),
|
||||
driver: driver,
|
||||
opts: opts,
|
||||
}
|
||||
if err := app.Publish[Mailer](m); err != nil {
|
||||
@@ -98,6 +103,64 @@ func Activate[P interface{ ID() string }](app *backpack.App, _ []P) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func driverFromApp(app *backpack.App) (Driver, Options, error) {
|
||||
opts := Options{}
|
||||
name := "memory"
|
||||
smtp := SMTPConfig{Port: 587, Timeout: 10 * time.Second}
|
||||
if app != nil && app.Config != nil {
|
||||
c := app.Config
|
||||
if v := strings.TrimSpace(c.String("mail.driver")); v != "" {
|
||||
name = strings.ToLower(v)
|
||||
}
|
||||
opts.From = strings.TrimSpace(c.String("mail.from"))
|
||||
opts.CSS = c.String("mail.css")
|
||||
opts.BrandCSS = c.String("mail.brandCss")
|
||||
smtp = smtpFromConfig(c)
|
||||
}
|
||||
switch name {
|
||||
case "memory":
|
||||
return NewMemoryDriver(), opts, nil
|
||||
case "log":
|
||||
return NewLogDriver(loggerFromApp(app)), opts, nil
|
||||
case "smtp":
|
||||
d, err := NewSMTPDriver(smtp)
|
||||
if err != nil {
|
||||
return nil, opts, err
|
||||
}
|
||||
return d, opts, nil
|
||||
default:
|
||||
return nil, opts, fmt.Errorf("postcard: unknown mail.driver %q (use memory, log, or smtp)", name)
|
||||
}
|
||||
}
|
||||
|
||||
func smtpFromConfig(c *compass.Config) SMTPConfig {
|
||||
cfg := SMTPConfig{
|
||||
Host: strings.TrimSpace(c.String("mail.smtp.host")),
|
||||
Port: c.Int("mail.smtp.port"),
|
||||
Username: c.String("mail.smtp.username"),
|
||||
Password: c.String("mail.smtp.password"),
|
||||
TLS: strings.TrimSpace(c.String("mail.smtp.tls")),
|
||||
Timeout: 10 * time.Second,
|
||||
}
|
||||
if raw := strings.TrimSpace(c.String("mail.smtp.timeout")); raw != "" {
|
||||
if d, err := time.ParseDuration(raw); err == nil {
|
||||
cfg.Timeout = d
|
||||
} else if n := c.Int("mail.smtp.timeout"); n > 0 {
|
||||
cfg.Timeout = time.Duration(n) * time.Second
|
||||
}
|
||||
}
|
||||
return cfg
|
||||
}
|
||||
|
||||
func loggerFromApp(app *backpack.App) *slog.Logger {
|
||||
if app != nil {
|
||||
if log, ok := app.Lookup[*slog.Logger](); ok && log != nil {
|
||||
return log
|
||||
}
|
||||
}
|
||||
return slog.Default()
|
||||
}
|
||||
|
||||
// BootPlugin registers a plugin's declared mail templates and layouts.
|
||||
// Missing files and unknown layout aliases fail with the dotted name.
|
||||
func BootPlugin[P interface{ ID() string }](app *backpack.App, p P) error {
|
||||
|
||||
Reference in New Issue
Block a user