feat(mail): add site URL handling and notification headers

* include site URL in Mailer for absolute URLs
* add header method for email notifications
This commit is contained in:
2026-08-23 11:53:07 +02:00
parent f96465b798
commit 143b2361f6
4 changed files with 72 additions and 38 deletions
+26 -6
View File
@@ -14,11 +14,31 @@ import (
)
type Mailer struct {
cfg config.EmailConfig
cfg config.EmailConfig
siteURL string
}
func New(cfg config.EmailConfig) *Mailer {
return &Mailer{cfg: cfg}
func New(cfg config.EmailConfig, siteURL string) *Mailer {
return &Mailer{cfg: cfg, siteURL: strings.TrimSuffix(siteURL, "/")}
}
// absoluteURL turns a site-relative path (e.g. "/uploads/1/foo.jpg") into a
// full URL so it's clickable straight from an email client.
func (m *Mailer) absoluteURL(path string) string {
if m.siteURL == "" {
return path
}
return m.siteURL + path
}
// header prefixes every notification with a one-line explainer of where it
// came from and what triggered it, so it's never a mystery in an inbox.
func (m *Mailer) header(event string) string {
site := m.siteURL
if site == "" {
site = "the wedding site"
}
return fmt.Sprintf("This is an automated email that gets triggered for %s when %s.\n\n", site, event)
}
// newMessageID builds an RFC 5322 Message-ID, scoped to the sender's own
@@ -93,7 +113,7 @@ func (m *Mailer) SendRSVP(r store.RSVP, allGuests []store.Guest) error {
fmt.Fprintf(&b, "\n%d adult(s), %d child(ren) total\n", totalAdults, totalChildren)
}
return m.send(fmt.Sprintf("RSVP from %s", joined), b.String())
return m.send(fmt.Sprintf("RSVP from %s", joined), m.header("an RSVP is made")+b.String())
}
func (m *Mailer) SendPhotoUpload(u store.PhotoUpload, files []store.PhotoFile) error {
@@ -110,7 +130,7 @@ func (m *Mailer) SendPhotoUpload(u store.PhotoUpload, files []store.PhotoFile) e
}
b.WriteString("\nPhotos:\n")
for _, f := range files {
fmt.Fprintf(&b, "- %s\n", f.URL)
fmt.Fprintf(&b, "- %s\n", m.absoluteURL(f.URL))
}
return m.send(fmt.Sprintf("%s uploaded photos", u.Name), b.String())
return m.send(fmt.Sprintf("%s uploaded photos", u.Name), m.header("photos are uploaded")+b.String())
}