Skip to content

Commit

Permalink
Add List-Unsubscribe header to opt-in confirmation mails. Closes #2224
Browse files Browse the repository at this point in the history
.
  • Loading branch information
knadh committed Jan 19, 2025
1 parent 2e8a5ac commit e8fd12b
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
5 changes: 3 additions & 2 deletions cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ type constants struct {
AllowExport bool `koanf:"allow_export"`
AllowWipe bool `koanf:"allow_wipe"`
RecordOptinIP bool `koanf:"record_optin_ip"`
UnsubHeader bool `koanf:"unsubscribe_header"`
Exportable map[string]bool `koanf:"-"`
DomainBlocklist []string `koanf:"-"`
} `koanf:"privacy"`
Expand Down Expand Up @@ -483,7 +484,7 @@ func initI18n(lang string, fs stuffbin.FileSystem) *i18n.I18n {
// initCampaignManager initializes the campaign manager.
func initCampaignManager(q *models.Queries, cs *constants, app *App) *manager.Manager {
campNotifCB := func(subject string, data interface{}) error {
return app.sendNotification(cs.NotifyEmails, subject, notifTplCampaign, data)
return app.sendNotification(cs.NotifyEmails, subject, notifTplCampaign, data, nil)
}

if ko.Bool("passive") {
Expand Down Expand Up @@ -541,7 +542,7 @@ func initImporter(q *models.Queries, db *sqlx.DB, core *core.Core, app *App) *su
// Refresh cached subscriber counts and stats.
core.RefreshMatViews(true)

app.sendNotification(app.constants.NotifyEmails, subject, notifTplImport, data)
app.sendNotification(app.constants.NotifyEmails, subject, notifTplImport, data, nil)
return nil
},
}, db.DB, app.i18n)
Expand Down
4 changes: 3 additions & 1 deletion cmd/notifications.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"bytes"
"net/textproto"
"regexp"
"strings"

Expand All @@ -27,7 +28,7 @@ type notifData struct {
}

// sendNotification sends out an e-mail notification to admins.
func (app *App) sendNotification(toEmails []string, subject, tplName string, data interface{}) error {
func (app *App) sendNotification(toEmails []string, subject, tplName string, data interface{}, headers textproto.MIMEHeader) error {
if len(toEmails) == 0 {
return nil
}
Expand All @@ -48,6 +49,7 @@ func (app *App) sendNotification(toEmails []string, subject, tplName string, dat
m.Subject = subject
m.Body = body
m.Messenger = emailMsgr
m.Headers = headers
if err := app.manager.PushMessage(m); err != nil {
app.log.Printf("error sending admin notification (%s): %v", subject, err)
return err
Expand Down
14 changes: 13 additions & 1 deletion cmd/subscribers.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"errors"
"fmt"
"net/http"
"net/textproto"
"net/url"
"strconv"
"strings"
Expand Down Expand Up @@ -663,8 +664,19 @@ func sendOptinConfirmationHook(app *App) func(sub models.Subscriber, listIDs []i
out.OptinURL = fmt.Sprintf(app.constants.OptinURL, sub.UUID, qListIDs.Encode())
out.UnsubURL = fmt.Sprintf(app.constants.UnsubURL, dummyUUID, sub.UUID)

// Unsub headers.
h := textproto.MIMEHeader{}
h.Set(models.EmailHeaderSubscriberUUID, sub.UUID)

// Attach List-Unsubscribe headers?
if app.constants.Privacy.UnsubHeader {
unsubURL := fmt.Sprintf(app.constants.UnsubURL, dummyUUID, sub.UUID)
h.Set("List-Unsubscribe-Post", "List-Unsubscribe=One-Click")
h.Set("List-Unsubscribe", `<`+unsubURL+`>`)
}

// Send the e-mail.
if err := app.sendNotification([]string{sub.Email}, app.i18n.T("subscribers.optinSubject"), notifSubscriberOptin, out); err != nil {
if err := app.sendNotification([]string{sub.Email}, app.i18n.T("subscribers.optinSubject"), notifSubscriberOptin, out, h); err != nil {
app.log.Printf("error sending opt-in e-mail for subscriber %d (%s): %s", sub.ID, sub.UUID, err)
return 0, err
}
Expand Down

0 comments on commit e8fd12b

Please sign in to comment.