Skip to content

High performance, self-hosted, newsletter and mailing list manager with a modern dashboard. Single binary app.

Make 'from' address setting an array to support multiple configured addresses. Closes #2258.

Kailash Nadh33e2ee5f61b8
diff --git a/cmd/admin.go b/cmd/admin.go
index 478f418b..de7951f0 100644
--- a/cmd/admin.go
+++ b/cmd/admin.go
@@ -13,8 +13,9 @@ import (
)
type serverConfig struct {
- RootURL string `json:"root_url"`
- FromEmail string `json:"from_email"`
+ RootURL string `json:"root_url"`
+ FromEmail string `json:"from_email"`
+ FromEmails []string `json:"from_emails"`
PublicSubscription struct {
Enabled bool `json:"enabled"`
CaptchaEnabled bool `json:"captcha_enabled"`
@@ -42,6 +43,7 @@ func (a *App) makeServerConfig() (serverConfig, error) {
out := serverConfig{
RootURL: a.urlCfg.RootURL,
FromEmail: a.cfg.FromEmail,
+ FromEmails: a.cfg.FromEmails,
Lang: a.cfg.Lang,
Permissions: a.cfg.PermissionsRaw,
HasLegacyUser: a.cfg.HasLegacyUser,
diff --git a/cmd/campaign_analytics.go b/cmd/campaign_analytics.go
index b53adee6..19dcf4e4 100644
--- a/cmd/campaign_analytics.go
+++ b/cmd/campaign_analytics.go
@@ -83,10 +83,10 @@ func (a *App) viewAnalytics(c echo.Context, resource string) error {
to = time.Now()
from = to.AddDate(0, 0, -7)
)
- if t, ok := ParseDateTime(c.QueryParam("from")); ok {
+ if t, ok := parseDateTime(c.QueryParam("from")); ok {
from = t
}
- if t, ok := ParseDateTime(c.QueryParam("to")); ok {
+ if t, ok := parseDateTime(c.QueryParam("to")); ok {
to = t
}
diff --git a/cmd/init.go b/cmd/init.go
index 5c66431b..8e112b72 100644
--- a/cmd/init.go
+++ b/cmd/init.go
@@ -85,7 +85,8 @@ type UrlConfig struct {
// Config contains static, constant config values required by arbitrary handlers and functions.
type Config struct {
SiteName string `koanf:"site_name"`
- FromEmail string `koanf:"from_email"`
+ FromEmail string `koanf:"-"`
+ FromEmails []string `koanf:"from_email"`
NotifyEmails []string `koanf:"notify_emails"`
EnablePublicSubPage bool `koanf:"enable_public_subscription_page"`
EnablePublicArchive bool `koanf:"enable_public_archive"`
@@ -545,6 +546,7 @@ func initConstConfig(ko *koanf.Koanf) *Config {
lo.Fatalf("error loading app.appearance config: %v", err)
}
+ c.FromEmail = getDefaultFromEmail(ko)
c.Lang = ko.String("app.lang")
c.Privacy.Exportable = koanfmaps.StringSliceToLookupMap(ko.Strings("privacy.exportable"))
c.MediaUpload.Provider = ko.String("upload.provider")
@@ -659,7 +661,7 @@ func initCampaignManager(msgrs []manager.Messenger, q *models.Queries, u *UrlCon
Concurrency: ko.Int("app.concurrency"),
MessageRate: ko.Int("app.message_rate"),
MaxSendErrors: ko.Int("app.max_send_errors"),
- FromEmail: ko.String("app.from_email"),
+ FromEmail: getDefaultFromEmail(ko),
IndividualTracking: ko.Bool("privacy.individual_tracking"),
DisableTracking: ko.Bool("privacy.disable_tracking"),
UnsubURL: u.UnsubURL,
@@ -882,7 +884,7 @@ func initNotifs(fs stuffbin.FileSystem, i *i18n.I18n, em *email.Emailer, u *UrlC
}
notifs.Initialize(notifs.Opt{
- FromEmail: ko.String("app.from_email"),
+ FromEmail: getDefaultFromEmail(ko),
SystemEmails: ko.Strings("app.notify_emails"),
ContentType: contentType,
}, tpls, txtTpls, em, lo)
@@ -1565,3 +1567,12 @@ func parseCronString(spec string) (cron.Schedule, error) {
}
return p.Parse(spec)
}
+
+// getDefaultFromEmail returns the first address from `app.from_email[]` as the global default.
+func getDefaultFromEmail(ko *koanf.Koanf) string {
+ if addresses := ko.Strings("app.from_email"); len(addresses) > 0 {
+ return addresses[0]
+ }
+
+ return ""
+}
diff --git a/cmd/settings.go b/cmd/settings.go
index 57716ed2..469eb2f9 100644
--- a/cmd/settings.go
+++ b/cmd/settings.go
@@ -3,8 +3,10 @@ package main
import (
"bytes"
"encoding/json"
+ "fmt"
"io"
"net/http"
+ "net/mail"
"net/url"
"regexp"
"runtime"
@@ -151,6 +153,13 @@ func (a *App) UpdateSettings(c echo.Context) error {
return err
}
+ // Sanitize `from` addresses.
+ addr, err := sanitizeFromAddresses(set.AppFromEmail)
+ if err != nil {
+ return echo.NewHTTPError(http.StatusBadRequest, err.Error())
+ }
+ set.AppFromEmail = addr
+
// Get the existing settings.
cur, err := a.core.GetSettings()
if err != nil {
@@ -543,3 +552,44 @@ func (a *App) GetAboutInfo(c echo.Context) error {
return c.JSON(http.StatusOK, out)
}
+
+func sanitizeFromAddresses(addresses []string) ([]string, error) {
+ if len(addresses) > 250 {
+ return nil, fmt.Errorf("omax 250 `from` addresses are allowed")
+ }
+
+ out := make([]string, 0, len(addresses))
+ seen := make(map[string]struct{}, len(addresses))
+ for _, a := range addresses {
+ a = strings.TrimSpace(a)
+ if a == "" {
+ continue
+ }
+ if utf8.RuneCountInString(a) > stdInputMaxLen {
+ return nil, fmt.Errorf("`from` address cannot exceed %d characters", stdInputMaxLen)
+ }
+
+ // The first from email address must not contain template expressions as that's
+ // used in non-campaign emails also.
+ if models.HasTplExpr(a) {
+ if len(out) == 0 {
+ return nil, fmt.Errorf("default (first) `from` address cannot contain template expressions")
+ }
+ } else {
+ if _, err := mail.ParseAddress(a); err != nil {
+ return nil, fmt.Errorf("invalid from email address: %s", a)
+ }
+ }
+ if _, ok := seen[a]; ok {
+ continue
+ }
+ seen[a] = struct{}{}
+ out = append(out, a)
+ }
+
+ if len(out) == 0 {
+ return nil, fmt.Errorf("at least one `from` address is required")
+ }
+
+ return out, nil
+}
diff --git a/cmd/utils.go b/cmd/utils.go
index 49588e6f..ebdf4d14 100644
--- a/cmd/utils.go
+++ b/cmd/utils.go
@@ -194,8 +194,8 @@ func formatTimestamps(from, to, now time.Time) string {
return from.Format(startLayout) + " - " + to.Format(endLayout)
}
-// ParseDateTime parses datetime formats.
-func ParseDateTime(s string) (time.Time, bool) {
+// parseDateTime parses datetime formats.
+func parseDateTime(s string) (time.Time, bool) {
if s == "" {
return time.Time{}, false
}
diff --git a/docs/swagger/collections.yaml b/docs/swagger/collections.yaml
index f12732ee..1eeff593 100644
--- a/docs/swagger/collections.yaml
+++ b/docs/swagger/collections.yaml
@@ -3472,7 +3472,13 @@ components:
app.favicon_url:
type: string
app.from_email:
- type: string
+ type: array
+ description: Sender addresses. The first must be a literal email address and is the default for campaigns and system emails.
+ minItems: 1
+ maxItems: 100
+ items:
+ type: string
+ maxLength: 2000
app.notify_emails:
type: array
items:
diff --git a/i18n/en.json b/i18n/en.json
index f4f00ba6..6fb62fc7 100644
--- a/i18n/en.json
+++ b/i18n/en.json
@@ -473,8 +473,8 @@
"settings.general.enablePublicSubPageHelp": "Show a public subscription page with all the public lists for people to subscribe.",
"settings.general.faviconURL": "Favicon URL",
"settings.general.faviconURLHelp": "(Optional) full URL to the static favicon to be displayed on user facing view such as the unsubscription page.",
- "settings.general.fromEmail": "Default `from` email",
- "settings.general.fromEmailHelp": "Default `from` e-mail to show on outgoing campaign e-mails. This can be changed per campaign.",
+ "settings.general.fromEmail": "'From' emails",
+ "settings.general.fromEmailHelp": "One address per line. At least one is required. The first is the default for campaigns and system emails and cannot contain template expressions.",
"settings.general.language": "Language",
"settings.general.logoURL": "Logo URL",
"settings.general.logoURLHelp": "(Optional) full URL to the static logo to be displayed on user facing view such as the unsubscription page.",
diff --git a/internal/core/settings.go b/internal/core/settings.go
index eaa9e515..5368b415 100644
--- a/internal/core/settings.go
+++ b/internal/core/settings.go
@@ -36,6 +36,7 @@ func (c *Core) GetSettings() (models.Settings, error) {
// UpdateSettings updates settings.
func (c *Core) UpdateSettings(s models.Settings) error {
// Persist empty collections as arrays, not JSON null.
+ s.AppFromEmail = utils.NilToSlice(s.AppFromEmail)
s.AppNotifyEmails = utils.NilToSlice(s.AppNotifyEmails)
s.PrivacyExportable = utils.NilToSlice(s.PrivacyExportable)
s.DomainBlocklist = utils.NilToSlice(s.DomainBlocklist)
@@ -65,7 +66,8 @@ func (c *Core) UpdateSettings(s models.Settings) error {
// UpdateSettingsByKey updates a single setting by key.
func (c *Core) UpdateSettingsByKey(key string, value json.RawMessage) error {
switch key {
- case "app.notify_emails",
+ case "app.from_email",
+ "app.notify_emails",
"privacy.exportable",
"privacy.domain_blocklist",
"privacy.domain_allowlist",
diff --git a/internal/migrations/v7.0.0.go b/internal/migrations/v7.0.0.go
index 28f41009..6d7f0d15 100644
--- a/internal/migrations/v7.0.0.go
+++ b/internal/migrations/v7.0.0.go
@@ -9,6 +9,11 @@ import (
)
func V7_0_0(db *sqlx.DB, fs stuffbin.FileSystem, ko *koanf.Koanf, lo *log.Logger) error {
+ // Migrate existing `from_email` to `from_email[]`.
+ if _, err := db.Exec(`UPDATE settings SET value = JSONB_BUILD_ARRAY(value) WHERE key = 'app.from_email' AND JSONB_TYPEOF(value) = 'string';`); err != nil {
+ return err
+ }
+
// Login session duration.
if _, err := db.Exec(`INSERT INTO settings (key, value) VALUES ('security.session_duration', '"7d"') ON CONFLICT (key) DO NOTHING;`); err != nil {
return err
diff --git a/models/settings.go b/models/settings.go
index 50115f87..a936badd 100644
--- a/models/settings.go
+++ b/models/settings.go
@@ -17,7 +17,7 @@ type Settings struct {
AppRootURL string `json:"app.root_url"`
AppLogoURL string `json:"app.logo_url"`
AppFaviconURL string `json:"app.favicon_url"`
- AppFromEmail string `json:"app.from_email"`
+ AppFromEmail []string `json:"app.from_email"`
AppNotifyEmails []string `json:"app.notify_emails"`
EnablePublicSubPage bool `json:"app.enable_public_subscription_page"`
EnablePublicArchive bool `json:"app.enable_public_archive"`
diff --git a/schema.sql b/schema.sql
index 89ce749c..233ccf98 100644
--- a/schema.sql
+++ b/schema.sql
@@ -230,7 +230,7 @@ INSERT INTO settings (key, value) VALUES
('app.site_name', '"Mailing list"'),
('app.root_url', '"http://localhost:9000"'),
('app.favicon_url', '""'),
- ('app.from_email', '"listmonk <[email protected]>"'),
+ ('app.from_email', '["listmonk <[email protected]>"]'),
('app.logo_url', '""'),
('app.concurrency', '10'),
('app.message_rate', '10'),
diff --git a/static/admin/assets/js/views/settings.js b/static/admin/assets/js/views/settings.js
index 97e8bb38..e61b408d 100644
--- a/static/admin/assets/js/views/settings.js
+++ b/static/admin/assets/js/views/settings.js
@@ -67,6 +67,9 @@ function component(settings, userRoles, listRoles) {
// Messengers render as collapsible accordions too.
(d.messengers || []).forEach((m, i) => { m.open = !!openState.messengers?.[i]; });
+ // One sender per line.
+ d['app.from_email'] = (d['app.from_email'] || []).join('\n');
+
// Domain block/allow lists, convert array to multi-line string.
d['privacy.domain_blocklist'] = (d['privacy.domain_blocklist'] || []).join('\n');
d['privacy.domain_allowlist'] = (d['privacy.domain_allowlist'] || []).join('\n');
@@ -414,6 +417,8 @@ function component(settings, userRoles, listRoles) {
return;
}
+ form['app.from_email'] = form['app.from_email'].split('\n');
+
// Convert domain lists multi-line string back to arrays.
form['privacy.domain_blocklist'] = form['privacy.domain_blocklist'].split('\n').map((v) => v.trim().toLowerCase()).filter((v) => v !== '');
form['privacy.domain_allowlist'] = form['privacy.domain_allowlist'].split('\n').map((v) => v.trim().toLowerCase()).filter((v) => v !== '');
diff --git a/static/admin/partials/campaign-form.html b/static/admin/partials/campaign-form.html
index c5fe0a73..82af95ee 100644
--- a/static/admin/partials/campaign-form.html
+++ b/static/admin/partials/campaign-form.html
@@ -32,10 +32,13 @@
<fieldset class="card">
<legend>{{ $root.L.Tc "globals.terms.messenger" 1 }}</legend>
- <label data-field>
- {{ $root.L.T "campaigns.fromAddress" }}
- <input name="from_email" maxlength="200" x-model="form.from_email" :disabled="!canEdit" placeholder="{{ $root.L.T "campaigns.fromAddressPlaceholder" }}" required />
- </label>
+ <div data-field>
+ <label for="campaign-from-email">{{ $root.L.T "campaigns.fromAddress" }}</label>
+ <input id="campaign-from-email" name="from_email" list="campaign-from-emails" maxlength="2000" x-model="form.from_email" :disabled="!canEdit" placeholder="{{ $root.L.T "campaigns.fromAddressPlaceholder" }}" required />
+ <datalist id="campaign-from-emails">
+ {{- range $root.ConfigJS.FromEmails }}<option value="{{ . }}"></option>{{ end }}
+ </datalist>
+ </div>
<label data-field for="messenger">
{{ $root.L.Tc "globals.terms.messenger" 1 }}
diff --git a/static/admin/partials/settings/general.html b/static/admin/partials/settings/general.html
index 2d5f45da..d9bb0712 100644
--- a/static/admin/partials/settings/general.html
+++ b/static/admin/partials/settings/general.html
@@ -44,8 +44,8 @@
<legend>{{ .L.T "subscribers.email" }}</legend>
<label data-field>
{{ .L.T "settings.general.fromEmail" }}
- <input name="app.from_email" pattern="((.+?)\s)?<(.+?)@(.+?)>" maxlength="300"
- placeholder="Listmonk <[email protected]>" x-model="form['app.from_email']" />
+ <textarea name="app.from_email" rows="4"
+ placeholder="Listmonk <[email protected]>" x-model="form['app.from_email']"></textarea>
<small data-hint>{{ .L.T "settings.general.fromEmailHelp" }}</small>
</label>
diff --git a/static/admin/partials/settings/smtp.html b/static/admin/partials/settings/smtp.html
index c66479b0..fe6c50a7 100644
--- a/static/admin/partials/settings/smtp.html
+++ b/static/admin/partials/settings/smtp.html
@@ -30,7 +30,7 @@
<div class="accordion-body">
<template x-if="smtpTestItem === n">
<fieldset class="card mb-8 mt-8">
- <p><strong>{{ .L.T "settings.general.fromEmail" }}</strong>: <span x-text="form['app.from_email']"></span></p>
+ <p><strong>{{ .L.T "settings.general.fromEmail" }}</strong>: <span x-text="form['app.from_email'].split('\n')[0]"></span></p>
<fieldset class="group">
<legend>{{ .L.T "settings.smtp.toEmail" }}</legend>
<input type="email" required placeholder="[email protected]" :class="`test-email-${n}`" x-model="testEmail" />
diff --git a/tests/specs/settings.spec.js b/tests/specs/settings.spec.js
index 6718ef1f..e37358f2 100644
--- a/tests/specs/settings.spec.js
+++ b/tests/specs/settings.spec.js
@@ -34,6 +34,53 @@ test.describe('Settings', () => {
test.beforeEach(async ({ browser }) => { await resetDB(browser); });
test.beforeEach(async ({ page }) => { await page.goto(SETTINGS); });
+ test('validates sender defaults and preserves configured and custom campaign senders', async ({ page }) => {
+ const first = 'Default <[email protected]>';
+ const second = 'News <[email protected]>';
+ const custom = 'Custom <[email protected]>';
+ const templated = '{{ .Subscriber.Attribs.sender }}';
+ const current = await settings(page);
+ const longSender = `${'a'.repeat(2000)} <[email protected]>`;
+ for (const addresses of [null, [], [' '], ['invalid'], ['{{ .Subscriber.Email }}'],
+ [' ', '{{ .Subscriber.Email }}'], ['{{ .Subscriber.Name }} <[email protected]>'], [first, 'invalid'],
+ [longSender], [first, `${templated}${longSender}`], Array(101).fill(first)]) {
+ const res = await page.request.put('/api/settings', {
+ data: { ...current, 'app.from_email': addresses },
+ });
+ expect(res.status()).toBe(400);
+ }
+
+ await page.locator('[name="app.from_email"]').fill(`\n ${first} \n${second}\n${first}\n${templated}\n`);
+ await save(page);
+ expect((await settings(page))['app.from_email']).toEqual([first, second, templated]);
+ await page.reload();
+ await expect(page.locator('[name="app.from_email"]')).toHaveValue(`${first}\n${second}\n${templated}`);
+
+ await page.goto('/admin/campaigns');
+ await page.getByRole('link', { name: 'New', exact: true }).click();
+ const input = page.locator('input[name="from_email"]');
+ await expect(input).toHaveValue(first);
+ await expect(input).toHaveAttribute('maxlength', '2000');
+ await expect(input).toHaveAttribute('list', 'campaign-from-emails');
+ const options = page.locator('#campaign-from-emails option');
+ await expect(options).toHaveCount(3);
+ await expect(options.nth(0)).toHaveAttribute('value', first);
+ await expect(options.nth(1)).toHaveAttribute('value', second);
+ await expect(options.nth(2)).toHaveAttribute('value', templated);
+
+ await page.goto('/admin/campaigns/1');
+ // Native datalist selections set the input value, just like typing it.
+ for (const address of [second, custom]) {
+ await input.fill(address);
+ const response = page.waitForResponse((r) => new URL(r.url()).pathname === '/api/campaigns/1'
+ && r.request().method() === 'PUT');
+ await page.getByRole('button', { name: 'Save changes', exact: true }).click();
+ expect((await response).ok()).toBeTruthy();
+ await page.reload();
+ await expect(input).toHaveValue(address);
+ }
+ });
+
test('saves edits across tabs, enables SMTP and deletes a server', async ({ page }) => {
const root = 'http://127.0.0.1:9000';
const favicon = `${root}/public/static/logo.png`;