go-notificationgo-notification
Features

Email Templates

Render your own Go html/template into a mail message, or use the built-in Laravel-style builder.

There are two ways to produce HTML email with go-notification:

  1. The built-in builderSetGreeting/Line/Action auto-render to a clean default HTML template (and a plain-text fallback) when you don't set HTML yourself.
  2. Your own templates — render any Go html/template into the message with mail.TemplateRenderer.

There is no global template registry and no Config.TemplateRegistry; templates are wired up in your ToMail method.

Built-in builder (no template needed)

If you only need a heading, some paragraphs, and a button, skip templates entirely. When HTML is empty, the mail driver renders these fields into a styled default template at send time:

main.go
func (n Welcome) ToMail(u notification.Notifiable) *mail.Message {
    return mail.NewMessage().
        SetSubject("Welcome!").
        SetGreeting("Hi " + u.(User).Name + "!").
        Line("Thanks for signing up.").
        Action("Open dashboard", "https://app.example.com").
        SetSalutation("— The team")
}

Your own templates with TemplateRenderer

For full control, compile a Go html/template at boot and render it into the message's HTML inside ToMail:

main.go
import (
    "embed"
    "html/template"

    "github.com/gopackx/go-notification/channel/mail"
)

//go:embed templates/*.html
var templatesFS embed.FS

// Parse once at boot.
var welcomeTmpl = template.Must(template.ParseFS(templatesFS, "templates/*.html"))

mail.TemplateRenderer executes a template with your data and stores the result in Message.HTML:

main.go
func (n Welcome) ToMail(u notification.Notifiable) *mail.Message {
    user := u.(User)
    msg := mail.NewMessage().SetSubject("Welcome, " + user.Name)

    renderer := mail.TemplateRenderer{Template: welcomeTmpl, Name: "welcome.html"}
    if err := renderer.Render(msg, map[string]any{
        "name":      user.Name,
        "dashboard": "https://app.example.com/dashboard",
        "year":      time.Now().Year(),
    }); err != nil {
        // handle/log; msg.HTML is left unset on error
    }
    return msg
}

TemplateRenderer.Render(m *mail.Message, data any) error calls Template.ExecuteTemplate(.., Name, data) (or Execute when Name is empty) and assigns the output to m.HTML.

Template file example

index.html
<!-- templates/welcome.html -->
{{ define "welcome.html" }}
<!DOCTYPE html>
<html>
<body style="font-family: sans-serif">
  <h1>Welcome, {{ .name }}!</h1>
  <p>Click below to get started.</p>
  <p><a href="{{ .dashboard }}">Open dashboard</a></p>
  <hr>
  <small>© {{ .year }} Example Inc.</small>
</body>
</html>
{{ end }}

Plain-text alongside HTML

TemplateRenderer only fills HTML. Set the plain-text body explicitly with SetText so providers send a multipart/alternative message:

main.go
msg.SetText("Welcome, " + user.Name + "! Open your dashboard: https://app.example.com")

Layouts / partials

Standard Go template composition works — parse layouts and content together, then render the entry template by name:

main.go
var tmpl = template.Must(template.ParseFS(templatesFS,
    "templates/layouts/*.html",
    "templates/emails/*.html",
))

renderer := mail.TemplateRenderer{Template: tmpl, Name: "welcome.html"}

Internationalization

Pass the locale into the data map and branch inside the template, or parse per-locale templates (welcome.en.html, welcome.id.html) and pick the Name in ToMail:

main.go
renderer := mail.TemplateRenderer{Template: tmpl, Name: "welcome." + user.Locale + ".html"}

External template engines

TemplateRenderer is specific to Go's html/template. To use Handlebars, MJML, or React Email, render to an HTML string with that tool and assign it directly:

main.go
html, _ := myEngine.Render("welcome", data)
msg.SetHTML(html)