Self-hosted email delivery platform that enables applications to send emails via HTTP APIs while handling SMTP delivery, templates, storage, security, and analytics. Fully self-hostable alternative to services like SendGrid or Mailgun.
Go email-relayemail-routinghttp-to-smtpmail-deliverymailchimp-alternativemailgun-alternativemailsendermailservermailtemplatemailtestpostmark-alternativesend-emailsendgrid-alternativesendmailtransactional-emailtransactional-outbox
feat(emails): add provider:, server: and key: search operators
Mike Kosulinf11412014f59
diff --git a/docs/docs/email-sending/email-log.md b/docs/docs/email-sending/email-log.mdindex da68498..6bc76f5 100644--- a/docs/docs/email-sending/email-log.md+++ b/docs/docs/email-sending/email-log.md@@ -55,6 +55,9 @@ searched (it may be stored as a blob or redacted).| `from:` | Sender address | `from:[email protected]` || `to:` | Any recipient | `to:[email protected]` || `template:` | Template name | `template:welcome` |+| `provider:` | Mailbox provider of the first recipient (Gmail, Yandex, Other, …), comma-separated | `provider:gmail,yandex` |+| `server:` | SMTP server hostname used to send, comma-separated | `server:smtp.acme.com` |+| `key:` | Name of the API key used to send, comma-separated | `key:billing` || `has:attachment` | Emails that have attachments | `has:attachment` || `status:` | One or more statuses (comma-separated) | `status:failed,suppressed` || `after:` | Created on/after a date | `after:2026-07-01` |@@ -62,14 +65,15 @@ searched (it may be stored as a blob or redacted).**Matching rules**-- Text operators (`from:`, `to:`, `subject:`, `template:`) and bare words use+- Text operators (`from:`, `to:`, `subject:`, `template:`, `provider:`,+ `server:`, `key:`) and bare words use**case-insensitive substring** matching.- Use `*` as a wildcard: `to:*@example.com` matches addresses ending in`@example.com`; `subject:invoice*` matches subjects that start with "invoice".- Wrap multi-word values in quotes: `subject:"payment failed"`.-- `status:` accepts a comma-separated list; an email matches if it is in **any**- of the listed statuses. See [Email Status](/docs/email-sending/email-status)- for the status values.+- `status:`, `provider:`, `server:` and `key:` accept a comma-separated list;+ an email matches if it matches **any** of the listed values. See+ [Email Status](/docs/email-sending/email-status) for the status values.- Dates bound `created_at`. A `YYYY-MM-DD` value is treated as a whole day, so`before:` is inclusive of that day; a full RFC3339 timestamp is used as theexact instant.diff --git a/internal/storage/repositories/email_repo.go b/internal/storage/repositories/email_repo.goindex 0b5cfb5..80707fe 100644--- a/internal/storage/repositories/email_repo.go+++ b/internal/storage/repositories/email_repo.go@@ -138,6 +138,18 @@ func (r *EmailRepository) FindByScopeFiltered(scope ResourceScope, f EmailFilterif f.Template != "" {q = q.Where("LOWER(template_name) LIKE ?", likePattern(f.Template))}+ if len(f.Providers) > 0 {+ cond, args := likeAny("LOWER(provider)", f.Providers)+ q = q.Where(cond, args...)+ }+ if len(f.Servers) > 0 {+ cond, args := likeAny("LOWER(smtp_hostname)", f.Servers)+ q = q.Where(cond, args...)+ }+ if len(f.APIKeys) > 0 {+ cond, args := likeAny("LOWER(k.name)", f.APIKeys)+ q = q.Where("EXISTS (SELECT 1 FROM api_keys k WHERE k.id = emails.api_key_id AND "+cond+")", args...)+ }if len(f.Statuses) > 0 {q = q.Where("status IN ?", f.Statuses)}diff --git a/internal/storage/repositories/email_search.go b/internal/storage/repositories/email_search.goindex 3345fe6..765d4b8 100644--- a/internal/storage/repositories/email_search.go+++ b/internal/storage/repositories/email_search.go@@ -31,6 +31,9 @@ const (opTo = "to"opSubject = "subject"opTemplate = "template"+ opProvider = "provider"+ opServer = "server"+ opKey = "key"opStatus = "status"opHas = "has"opAfter = "after"@@ -49,6 +52,9 @@ type EmailFilter struct {Subject stringText stringTemplate string+ Providers []string+ Servers []string+ APIKeys []stringStatuses []stringHasAttachment boolAfter *time.Time@@ -61,7 +67,9 @@ type EmailFilter struct {const maxSearchQueryLen = 512// ParseSearchQuery turns a Twitter-style search string into an EmailFilter.-// Supported operators: from: to: subject: template: status: has:attachment after: before:+// Supported operators: from: to: subject: template: provider: server: key:+// status: has:attachment after: before:+// provider:, server:, key: and status: accept comma-separated lists.// Bare words (no known prefix) match the subject or any recipient, or narrow// Subject when subject: is given.//@@ -91,6 +99,12 @@ func ParseSearchQuery(q string) EmailFilter {f.appendSubject(val)case opTemplate:f.Template = val+ case opProvider:+ f.Providers = splitList(val)+ case opServer:+ f.Servers = splitList(val)+ case opKey:+ f.APIKeys = splitList(val)case opStatus:// Accept a comma-separated list, e.g. status:sent,failed.for _, s := range strings.Split(val, ",") {@@ -131,6 +145,17 @@ func ParseSearchQuery(q string) EmailFilter {return f}+// splitList splits a comma-separated operator value, dropping empty items.+func splitList(val string) []string {+ var out []string+ for _, s := range strings.Split(val, ",") {+ if s = strings.TrimSpace(s); s != "" {+ out = append(out, s)+ }+ }+ return out+}+func (f *EmailFilter) appendSubject(s string) {s = strings.TrimSpace(s)if s == "" {@@ -158,6 +183,17 @@ func likePattern(term string) string {return "%" + s + "%"}+// likeAny builds a predicate matching col against any of the terms.+func likeAny(col string, terms []string) (string, []any) {+ conds := make([]string, len(terms))+ args := make([]any, len(terms))+ for i, t := range terms {+ conds[i] = col + " LIKE ?"+ args[i] = likePattern(t)+ }+ return "(" + strings.Join(conds, " OR ") + ")", args+}+// sortColumns whitelists the user-facing sort keys to real, safe column names.// ORDER BY is built only from these constants — user input never reaches SQL.var sortColumns = map[string]string{diff --git a/internal/storage/repositories/email_search_db_test.go b/internal/storage/repositories/email_search_db_test.goindex b050f22..3ffe326 100644--- a/internal/storage/repositories/email_search_db_test.go+++ b/internal/storage/repositories/email_search_db_test.go@@ -35,9 +35,15 @@ func seedSearchEmails(t *testing.T, tx *gorm.DB) ResourceScope {}user := createUser(t, tx, "[email protected]")ws := uintPtr(90101)+ key := &models.APIKey{UserID: user, WorkspaceID: ws, Name: "Billing service", KeyHash: "x", KeyPrefix: "pk_test"}+ if err := tx.Create(key).Error; err != nil {+ t.Fatalf("create api key: %v", err)+ }emails := []models.Email{- {Sender: "[email protected]", Subject: "Weekly report", Recipients: pq.StringArray{"[email protected]"}},- {Sender: "[email protected]", Subject: "Invoice", Recipients: pq.StringArray{"[email protected]", "[email protected]"}},+ {Sender: "[email protected]", Subject: "Weekly report", Recipients: pq.StringArray{"[email protected]"},+ Provider: "Google Workspace", SMTPHostname: "smtp.acme.com"},+ {Sender: "[email protected]", Subject: "Invoice", Recipients: pq.StringArray{"[email protected]", "[email protected]"},+ Provider: "Other", SMTPHostname: "smtps.relay.net", APIKeyID: &key.ID},{Sender: "[email protected]", Subject: "Welcome [email protected]", Recipients: pq.StringArray{"[email protected]"}},}for i := range emails {@@ -87,6 +93,15 @@ func TestFindByScopeFiltered_Search(t *testing.T) {{"to: matches any of several recipients", "to:[email protected]", []string{"Invoice"}},{"subject: stays subject-only", "subject:[email protected]", []string{"Welcome [email protected]"}},{"bare words merge into subject: as before", "subject:weekly report", []string{"Weekly report"}},+ {"provider: matches case-insensitively", "provider:google", []string{"Weekly report"}},+ {"provider: accepts a quoted name", `provider:"google workspace"`, []string{"Weekly report"}},+ {"server: matches the SMTP hostname", "server:relay", []string{"Invoice"}},+ {"key: matches the API key name", "key:billing", []string{"Invoice"}},+ {"key: with no matching key finds nothing", "key:nope", nil},+ {"provider: list matches any provider", "provider:gmail,google", []string{"Weekly report"}},+ {"provider: list matches several emails", "provider:google,other", []string{"Invoice", "Weekly report"}},+ {"server: list matches any server", "server:acme,relay", []string{"Invoice", "Weekly report"}},+ {"key: list matches any key", "key:nope,billing", []string{"Invoice"}},}for _, tc := range tests {t.Run(tc.name, func(t *testing.T) {diff --git a/internal/storage/repositories/email_search_test.go b/internal/storage/repositories/email_search_test.goindex 9a25f7a..c8201c1 100644--- a/internal/storage/repositories/email_search_test.go+++ b/internal/storage/repositories/email_search_test.go@@ -127,6 +127,16 @@ func TestParseSearchQuery(t *testing.T) {q: "template:welcome-email",want: EmailFilter{Template: "welcome-email"},},+ {+ name: "provider, server and key operators",+ q: `provider:"google workspace" server:smtp.acme.com key:billing`,+ want: EmailFilter{Providers: []string{"google workspace"}, Servers: []string{"smtp.acme.com"}, APIKeys: []string{"billing"}},+ },+ {+ name: "provider, server and key accept comma-separated lists",+ q: `provider:Gmail,Yandex server:a.com,b.com key:"Billing, ,Ops"`,+ want: EmailFilter{Providers: []string{"Gmail", "Yandex"}, Servers: []string{"a.com", "b.com"}, APIKeys: []string{"Billing", "Ops"}},+ },{name: "unknown prefix falls back to free text",q: "cc:someone",@@ -149,6 +159,15 @@ func TestParseSearchQuery(t *testing.T) {if got.Text != tc.want.Text {t.Errorf("Text = %q, want %q", got.Text, tc.want.Text)}+ if strings.Join(got.Providers, ",") != strings.Join(tc.want.Providers, ",") {+ t.Errorf("Providers = %v, want %v", got.Providers, tc.want.Providers)+ }+ if strings.Join(got.Servers, ",") != strings.Join(tc.want.Servers, ",") {+ t.Errorf("Servers = %v, want %v", got.Servers, tc.want.Servers)+ }+ if strings.Join(got.APIKeys, ",") != strings.Join(tc.want.APIKeys, ",") {+ t.Errorf("APIKeys = %v, want %v", got.APIKeys, tc.want.APIKeys)+ }if got.Template != tc.want.Template {t.Errorf("Template = %q, want %q", got.Template, tc.want.Template)}