Skip to content

armgit / posta

Public Imported from GitHub
0

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.

fix(emails): match bare search text against recipients

Mike Kosulinc26f4db87032
diff --git a/docs/docs/email-sending/email-log.md b/docs/docs/email-sending/email-log.md
index a713c86..da68498 100644
--- a/docs/docs/email-sending/email-log.md
+++ b/docs/docs/email-sending/email-log.md
@@ -45,12 +45,12 @@ curl "http://localhost:9000/api/v1/workspaces/current/emails?page=0&size=20" \
## Search operators
The `q` parameter is a single search string that accepts operators. Words
-without an operator match the **subject** — the body is not searched (it may be
-stored as a blob or redacted).
+without an operator match the **subject or any recipient** — the body is not
+searched (it may be stored as a blob or redacted).
| Operator | Matches | Example |
|----------|---------|---------|
-| _(bare text)_ | Subject | `invoice reminder` |
+| _(bare text)_ | Subject or any recipient | `invoice reminder`, `[email protected]` |
| `subject:` | Subject | `subject:"weekly report"` |
| `from:` | Sender address | `from:[email protected]` |
| `to:` | Any recipient | `to:[email protected]` |
@@ -73,7 +73,8 @@ stored as a blob or redacted).
- 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 the
exact instant.
-- Operators combine with **AND**. Any bare words are merged into the subject search.
+- Operators combine with **AND**. When `subject:` is present, bare words are
+ merged into the subject search and recipients are not searched.
## Sorting
diff --git a/internal/storage/repositories/email_repo.go b/internal/storage/repositories/email_repo.go
index 4593014..0b5cfb5 100644
--- a/internal/storage/repositories/email_repo.go
+++ b/internal/storage/repositories/email_repo.go
@@ -130,6 +130,11 @@ func (r *EmailRepository) FindByScopeFiltered(scope ResourceScope, f EmailFilter
if f.Subject != "" {
q = q.Where("LOWER(subject) LIKE ?", likePattern(f.Subject))
}
+ if f.Text != "" {
+ pat := likePattern(f.Text)
+ q = q.Where("(LOWER(subject) LIKE ? OR EXISTS (SELECT 1 FROM unnest(recipients) AS r WHERE LOWER(r) LIKE ?))",
+ pat, pat)
+ }
if f.Template != "" {
q = q.Where("LOWER(template_name) LIKE ?", likePattern(f.Template))
}
diff --git a/internal/storage/repositories/email_search.go b/internal/storage/repositories/email_search.go
index 15a764b..3345fe6 100644
--- a/internal/storage/repositories/email_search.go
+++ b/internal/storage/repositories/email_search.go
@@ -47,6 +47,7 @@ type EmailFilter struct {
Sender string
Recipient string
Subject string
+ Text string
Template string
Statuses []string
HasAttachment bool
@@ -61,7 +62,8 @@ const maxSearchQueryLen = 512
// ParseSearchQuery turns a Twitter-style search string into an EmailFilter.
// Supported operators: from: to: subject: template: status: has:attachment after: before:
-// Bare words (no known prefix) accumulate into Subject as a free-text search.
+// Bare words (no known prefix) match the subject or any recipient, or narrow
+// Subject when subject: is given.
//
// Example:
//
@@ -120,7 +122,11 @@ func ParseSearchQuery(q string) EmailFilter {
}
if len(free) > 0 {
- f.appendSubject(strings.Join(free, " "))
+ if f.Subject != "" {
+ f.appendSubject(strings.Join(free, " "))
+ } else {
+ f.Text = strings.Join(free, " ")
+ }
}
return f
}
diff --git a/internal/storage/repositories/email_search_db_test.go b/internal/storage/repositories/email_search_db_test.go
new file mode 100644
index 0000000..b050f22
--- /dev/null
+++ b/internal/storage/repositories/email_search_db_test.go
@@ -0,0 +1,104 @@
+// Copyright (c) 2026 Jonas Kaninda.
+//
+// This file is part of Posta.
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Affero General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Affero General Public License for more details.
+//
+// You should have received a copy of the GNU Affero General Public License
+// along with this program. If not, see <https://www.gnu.org/licenses/>.
+//
+// SPDX-License-Identifier: AGPL-3.0-or-later
+
+package repositories
+
+import (
+ "sort"
+ "testing"
+
+ "github.com/goposta/posta/internal/models"
+ "github.com/lib/pq"
+ "gorm.io/gorm"
+)
+
+func seedSearchEmails(t *testing.T, tx *gorm.DB) ResourceScope {
+ t.Helper()
+ if err := tx.AutoMigrate(&models.APIKey{}, &models.Email{}); err != nil {
+ t.Skipf("skipping: cannot migrate email schema: %v", err)
+ }
+ user := createUser(t, tx, "[email protected]")
+ ws := uintPtr(90101)
+ 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: "Welcome [email protected]", Recipients: pq.StringArray{"[email protected]"}},
+ }
+ for i := range emails {
+ emails[i].UserID = user
+ emails[i].WorkspaceID = ws
+ if err := tx.Create(&emails[i]).Error; err != nil {
+ t.Fatalf("create email: %v", err)
+ }
+ }
+ return ResourceScope{UserID: user, WorkspaceID: ws}
+}
+
+func searchSubjects(t *testing.T, repo *EmailRepository, scope ResourceScope, q string) []string {
+ t.Helper()
+ items, total, err := repo.FindByScopeFiltered(scope, ParseSearchQuery(q), "", 50, 0)
+ if err != nil {
+ t.Fatalf("search %q: %v", q, err)
+ }
+ if int(total) != len(items) {
+ t.Errorf("search %q: total = %d, items = %d", q, total, len(items))
+ }
+ subjects := make([]string, 0, len(items))
+ for _, e := range items {
+ subjects = append(subjects, e.Subject)
+ }
+ sort.Strings(subjects)
+ return subjects
+}
+
+func TestFindByScopeFiltered_Search(t *testing.T) {
+ db := testDB(t)
+ tx := db.Begin()
+ t.Cleanup(func() { tx.Rollback() })
+
+ scope := seedSearchEmails(t, tx)
+ repo := NewEmailRepository(tx)
+
+ tests := []struct {
+ name string
+ q string
+ want []string
+ }{
+ {"bare text matches subject", "weekly", []string{"Weekly report"}},
+ {"bare address matches single recipient", "[email protected]", []string{"Weekly report"}},
+ {"bare address matches any of several recipients", "[email protected]", []string{"Invoice"}},
+ {"bare address matches recipient or subject", "[email protected]", []string{"Invoice", "Welcome [email protected]"}},
+ {"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"}},
+ }
+ for _, tc := range tests {
+ t.Run(tc.name, func(t *testing.T) {
+ got := searchSubjects(t, repo, scope, tc.q)
+ if len(got) != len(tc.want) {
+ t.Fatalf("search %q = %v, want %v", tc.q, got, tc.want)
+ }
+ for i := range got {
+ if got[i] != tc.want[i] {
+ t.Fatalf("search %q = %v, want %v", tc.q, got, tc.want)
+ }
+ }
+ })
+ }
+}
diff --git a/internal/storage/repositories/email_search_test.go b/internal/storage/repositories/email_search_test.go
index fcf66e5..9a25f7a 100644
--- a/internal/storage/repositories/email_search_test.go
+++ b/internal/storage/repositories/email_search_test.go
@@ -52,8 +52,13 @@ func TestParseSearchQuery(t *testing.T) {
want: EmailFilter{},
},
{
- name: "bare words become subject",
+ name: "bare words become free text",
q: "weekly invoice",
+ want: EmailFilter{Text: "weekly invoice"},
+ },
+ {
+ name: "bare words merge into subject when subject: is given",
+ q: "subject:weekly invoice",
want: EmailFilter{Subject: "weekly invoice"},
},
{
@@ -125,7 +130,7 @@ func TestParseSearchQuery(t *testing.T) {
{
name: "unknown prefix falls back to free text",
q: "cc:someone",
- want: EmailFilter{Subject: "cc:someone"},
+ want: EmailFilter{Text: "cc:someone"},
},
}
@@ -141,6 +146,9 @@ func TestParseSearchQuery(t *testing.T) {
if got.Subject != tc.want.Subject {
t.Errorf("Subject = %q, want %q", got.Subject, tc.want.Subject)
}
+ if got.Text != tc.want.Text {
+ t.Errorf("Text = %q, want %q", got.Text, tc.want.Text)
+ }
if got.Template != tc.want.Template {
t.Errorf("Template = %q, want %q", got.Template, tc.want.Template)
}