Skip to content

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

Add ability to exclude SMTP servers from the default messenger.

Kailash Nadhf2dbcb3cef5e
diff --git a/cmd/init.go b/cmd/init.go
index f2507a7d..58e4edaf 100644
--- a/cmd/init.go
+++ b/cmd/init.go
@@ -744,7 +744,13 @@ func initSMTPMessengers() []manager.Messenger {
lo.Fatalf("error reading SMTP config: %v", err)
}
- servers = append(servers, s)
+ if item.Bool("exclude_from_default") {
+ if s.Name == "" {
+ lo.Println("warning: SMTP server excluded from default has no name and cannot be selected")
+ }
+ } else {
+ servers = append(servers, s)
+ }
lo.Printf("initialized email (SMTP) messenger: %s@%s", item.String("username"), item.String("host"))
// If the server has a name, initialize it as a standalone e-mail messenger
@@ -758,18 +764,17 @@ func initSMTPMessengers() []manager.Messenger {
}
}
- // Initialize the 'email' messenger with all SMTP servers.
+ if len(servers) == 0 {
+ lo.Println("warning: no SMTP servers available for the default email messenger")
+ }
+
+ // Initialize the 'email' messenger with the default SMTP servers.
msgr, err := email.New(email.MessengerName, servers...)
if err != nil {
lo.Fatalf("error initializing e-mail messenger: %v", err)
}
- // If it's just one server, return the default "email" messenger.
- if len(servers) == 1 {
- return []manager.Messenger{msgr}
- }
-
- // If there are multiple servers, prepend the group "email" to be the first one.
+ // Prepend the default "email" messenger before the named servers.
out = append([]manager.Messenger{msgr}, out...)
return out
diff --git a/cmd/settings.go b/cmd/settings.go
index 5592e27e..57716ed2 100644
--- a/cmd/settings.go
+++ b/cmd/settings.go
@@ -168,10 +168,10 @@ func (a *App) UpdateSettings(c echo.Context) error {
// Duplicates are disallowed and "email" is a reserved name.
names := map[string]bool{email.MessengerName: true}
- // There should be at least one SMTP block that's enabled.
+ // There should be at least one enabled SMTP server for the default messenger.
has := false
for i, s := range set.SMTP {
- if s.Enabled {
+ if s.Enabled && !s.ExcludeFromDefault {
has = true
}
@@ -189,6 +189,9 @@ func (a *App) UpdateSettings(c echo.Context) error {
names[name] = true
}
+ if s.ExcludeFromDefault && name == "" {
+ return echo.NewHTTPError(http.StatusBadRequest, a.i18n.T("settings.smtp.excludeFromDefaultNameRequired"))
+ }
set.SMTP[i].Name = name
// Assign a UUID. The frontend only sends a password when the user explicitly
@@ -214,7 +217,7 @@ func (a *App) UpdateSettings(c echo.Context) error {
}
}
if !has {
- return echo.NewHTTPError(http.StatusBadRequest, a.i18n.T("settings.errorNoSMTP"))
+ return echo.NewHTTPError(http.StatusBadRequest, a.i18n.T("settings.smtp.defaultRequired"))
}
// Normalize `from_addresses``. Values are either an e-mail address
diff --git a/i18n/en.json b/i18n/en.json
index 7bc2bb87..f0a58a10 100644
--- a/i18n/en.json
+++ b/i18n/en.json
@@ -607,6 +607,10 @@
"settings.security.name": "Security",
"settings.smtp.customHeaders": "Custom headers",
"settings.smtp.customHeadersHelp": "Optional array of e-mail headers to include in all messages sent from this server. eg: [{\"X-Custom\": \"value\"}, {\"X-Custom2\": \"value\"}]",
+ "settings.smtp.excludeFromDefault": "Exclude from default",
+ "settings.smtp.excludeFromDefaultHelp": "Exclude this server from the default 'email' messenger and From-address routing. Use it explicitly for campaigns or transactional emails by its name.",
+ "settings.smtp.excludeFromDefaultNameRequired": "SMTP servers excluded from default must have a name.",
+ "settings.smtp.defaultRequired": "At least one enabled SMTP server must be included in the default 'email' messenger.",
"settings.smtp.fromAddresses": "From addresses / domains",
"settings.smtp.fromAddressesHelp": "Optional list of e-mail addresses ([email protected]) or domains (example.com) to route through this SMTP server. If multiple servers specify the same entry, they are used round robin.",
"settings.smtp.heloHost": "HELO hostname",
diff --git a/internal/messenger/email/email.go b/internal/messenger/email/email.go
index b86d515c..f1008cf0 100644
--- a/internal/messenger/email/email.go
+++ b/internal/messenger/email/email.go
@@ -138,6 +138,9 @@ func (e *Emailer) Push(m models.Message) error {
pool = srvs
}
}
+ if len(pool) == 0 {
+ return fmt.Errorf("no SMTP servers available for messenger %q", e.name)
+ }
srv := pool[rand.Intn(len(pool))]
// Are there attachments?
diff --git a/internal/migrations/v7.0.0.go b/internal/migrations/v7.0.0.go
index e6608a75..b1411fc7 100644
--- a/internal/migrations/v7.0.0.go
+++ b/internal/migrations/v7.0.0.go
@@ -9,6 +9,24 @@ import (
)
func V7_0_0(db *sqlx.DB, fs stuffbin.FileSystem, ko *koanf.Koanf, lo *log.Logger) error {
+ // Add `exclude_from_default` field to all SMTP servers.
+ if _, err := db.Exec(`
+ UPDATE settings
+ SET value = (
+ SELECT JSONB_AGG(
+ JSONB_SET(v, '{exclude_from_default}', COALESCE(v->'exclude_from_default', 'false'::JSONB))
+ ORDER BY ord
+ )
+ FROM JSONB_ARRAY_ELEMENTS(value) WITH ORDINALITY AS t(v, ord)
+ )
+ WHERE key = 'smtp' AND EXISTS (
+ SELECT 1 FROM JSONB_ARRAY_ELEMENTS(value) AS v
+ WHERE NOT (v ? 'exclude_from_default')
+ );
+ `); err != nil {
+ return err
+ }
+
// OIDC claim mapping.
if _, err := db.Exec(`UPDATE settings SET value = JSONB_SET(value, '{roles}', '[]'::JSONB) WHERE key = 'security.oidc' AND NOT (value ? 'roles');`); err != nil {
return err
diff --git a/models/settings.go b/models/settings.go
index 84550b86..8cd05577 100644
--- a/models/settings.go
+++ b/models/settings.go
@@ -92,24 +92,25 @@ type Settings struct {
UploadS3Expiry string `json:"upload.s3.expiry"`
SMTP []struct {
- Name string `json:"name"`
- UUID string `json:"uuid"`
- Enabled bool `json:"enabled"`
- Host string `json:"host"`
- HelloHostname string `json:"hello_hostname"`
- Port int `json:"port"`
- AuthProtocol string `json:"auth_protocol"`
- Username string `json:"username"`
- Password string `json:"password,omitempty"`
- EmailHeaders []map[string]string `json:"email_headers"`
- MaxConns int `json:"max_conns"`
- MaxMsgRetries int `json:"max_msg_retries"`
- MsgRetryDelay string `json:"msg_retry_delay"`
- IdleTimeout string `json:"idle_timeout"`
- WaitTimeout string `json:"wait_timeout"`
- TLSType string `json:"tls_type"`
- TLSSkipVerify bool `json:"tls_skip_verify"`
- FromAddresses []string `json:"from_addresses"`
+ Name string `json:"name"`
+ UUID string `json:"uuid"`
+ Enabled bool `json:"enabled"`
+ Host string `json:"host"`
+ HelloHostname string `json:"hello_hostname"`
+ Port int `json:"port"`
+ AuthProtocol string `json:"auth_protocol"`
+ Username string `json:"username"`
+ Password string `json:"password,omitempty"`
+ EmailHeaders []map[string]string `json:"email_headers"`
+ MaxConns int `json:"max_conns"`
+ MaxMsgRetries int `json:"max_msg_retries"`
+ MsgRetryDelay string `json:"msg_retry_delay"`
+ IdleTimeout string `json:"idle_timeout"`
+ WaitTimeout string `json:"wait_timeout"`
+ TLSType string `json:"tls_type"`
+ TLSSkipVerify bool `json:"tls_skip_verify"`
+ FromAddresses []string `json:"from_addresses"`
+ ExcludeFromDefault bool `json:"exclude_from_default"`
} `json:"smtp"`
Messengers []struct {
diff --git a/schema.sql b/schema.sql
index ec59b405..42acd89d 100644
--- a/schema.sql
+++ b/schema.sql
@@ -279,8 +279,8 @@ INSERT INTO settings (key, value) VALUES
('upload.s3.bucket_type', '"public"'),
('upload.s3.expiry', '"167h"'),
('smtp',
- '[{"enabled":true, "host":"smtp.yoursite.com","port":25,"auth_protocol":"cram","username":"username","password":"password","hello_hostname":"","max_conns":10,"idle_timeout":"15s","wait_timeout":"5s","max_msg_retries":2,"msg_retry_delay":"10ms","tls_type":"STARTTLS","tls_skip_verify":false,"email_headers":[], "from_addresses":[]},
- {"enabled":false, "host":"smtp.gmail.com","port":465,"auth_protocol":"login","username":"[email protected]","password":"password","hello_hostname":"","max_conns":10,"idle_timeout":"15s","wait_timeout":"5s","max_msg_retries":2,"msg_retry_delay":"10ms","tls_type":"TLS","tls_skip_verify":false,"email_headers":[], "from_addresses":[]}]'),
+ '[{"enabled":true, "exclude_from_default":false, "host":"smtp.yoursite.com","port":25,"auth_protocol":"cram","username":"username","password":"password","hello_hostname":"","max_conns":10,"idle_timeout":"15s","wait_timeout":"5s","max_msg_retries":2,"msg_retry_delay":"10ms","tls_type":"STARTTLS","tls_skip_verify":false,"email_headers":[], "from_addresses":[]},
+ {"enabled":false, "exclude_from_default":false, "host":"smtp.gmail.com","port":465,"auth_protocol":"login","username":"[email protected]","password":"password","hello_hostname":"","max_conns":10,"idle_timeout":"15s","wait_timeout":"5s","max_msg_retries":2,"msg_retry_delay":"10ms","tls_type":"TLS","tls_skip_verify":false,"email_headers":[], "from_addresses":[]}]'),
('messengers', '[]'),
('bounce.enabled', 'false'),
('bounce.webhooks_enabled', 'false'),
diff --git a/static/admin/assets/js/views/settings.js b/static/admin/assets/js/views/settings.js
index c0051568..686377ad 100644
--- a/static/admin/assets/js/views/settings.js
+++ b/static/admin/assets/js/views/settings.js
@@ -252,7 +252,7 @@ function component(settings, userRoles, listRoles) {
// SMTP.
addSMTP() {
this.form.smtp.push({
- name: '', enabled: true, host: '', hello_hostname: '', port: 587, auth_protocol: 'none',
+ name: '', enabled: true, exclude_from_default: false, host: '', hello_hostname: '', port: 587, auth_protocol: 'none',
username: '', password: '', email_headers: [], strEmailHeaders: '[]', showHeaders: false,
from_addresses: [], max_conns: 10, max_msg_retries: 2, msg_retry_delay: '10ms',
idle_timeout: '15s', wait_timeout: '5s', tls_type: 'STARTTLS', tls_skip_verify: false,
diff --git a/static/admin/partials/settings/smtp.html b/static/admin/partials/settings/smtp.html
index a817cedc..1df0d3fb 100644
--- a/static/admin/partials/settings/smtp.html
+++ b/static/admin/partials/settings/smtp.html
@@ -168,7 +168,7 @@
<legend>{{ .L.T "globals.fields.name" }}</legend>
<label data-field>
{{ .L.T "globals.fields.name" }}
- <input maxlength="100" placeholder="email-primary" x-model="item.name" />
+ <input maxlength="100" placeholder="email-primary" x-model="item.name" :required="item.exclude_from_default" />
<small data-hint>{{ .L.T "settings.mailserver.nameHelp" }}</small>
</label>
<label data-field>
@@ -176,6 +176,11 @@
<ot-taginput x-model="item.from_addresses"><input placeholder="[email protected], anothersite.com" /></ot-taginput>
<small data-hint>{{ .L.T "settings.smtp.fromAddressesHelp" }}</small>
</label>
+ <label data-field>
+ <input type="checkbox" role="switch" x-model="item.exclude_from_default" :disabled="!item.name" x-effect="if (!item.name) item.exclude_from_default = false" />
+ {{ .L.T "settings.smtp.excludeFromDefault" }}
+ <small data-hint>{{ .L.T "settings.smtp.excludeFromDefaultHelp" }}</small>
+ </label>
</fieldset>
<template x-if="item.email_headers.length === 0 && !item.showHeaders">