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): decode sender names stored as encoded-words

Mike Kosulin8619cd1e2c0f
diff --git a/internal/storage/migration/upgrade/step.go b/internal/storage/migration/upgrade/step.go
index 83c3c83..fbd405c 100644
--- a/internal/storage/migration/upgrade/step.go
+++ b/internal/storage/migration/upgrade/step.go
@@ -57,6 +57,10 @@ var registry = []Step{
ID: "2026-08-28-default-workspace",
Apply: applyDefaultWorkspace,
},
+ {
+ ID: "2026-10-13-decode-email-senders",
+ Apply: applyDecodeEmailSenders,
+ },
}
func Registry() []Step {
diff --git a/internal/storage/migration/upgrade/steps_2026_10_13_decode_email_senders.go b/internal/storage/migration/upgrade/steps_2026_10_13_decode_email_senders.go
new file mode 100644
index 0000000..dd51581
--- /dev/null
+++ b/internal/storage/migration/upgrade/steps_2026_10_13_decode_email_senders.go
@@ -0,0 +1,50 @@
+// 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 upgrade
+
+import (
+ "fmt"
+ "net/mail"
+
+ "gorm.io/gorm"
+)
+
+// applyDecodeEmailSenders rewrites senders stored as RFC 2047 encoded-words
+// into the readable form now stored for display.
+func applyDecodeEmailSenders(tx *gorm.DB) error {
+ var rows []struct {
+ ID uint
+ Sender string
+ }
+ return tx.Table("emails").Select("id, sender").Where("sender LIKE ?", "%=?%").
+ FindInBatches(&rows, 500, func(batch *gorm.DB, _ int) error {
+ for _, r := range rows {
+ addr, err := mail.ParseAddress(r.Sender)
+ if err != nil || addr.Name == "" {
+ continue
+ }
+ sender := fmt.Sprintf("%q <%s>", addr.Name, addr.Address)
+ if err := tx.Table("emails").Where("id = ?", r.ID).Update("sender", sender).Error; err != nil {
+ return fmt.Errorf("decode sender of email %d: %w", r.ID, err)
+ }
+ }
+ return nil
+ }).Error
+}
diff --git a/internal/storage/migration/upgrade/steps_2026_10_13_decode_email_senders_test.go b/internal/storage/migration/upgrade/steps_2026_10_13_decode_email_senders_test.go
new file mode 100644
index 0000000..2d2351e
--- /dev/null
+++ b/internal/storage/migration/upgrade/steps_2026_10_13_decode_email_senders_test.go
@@ -0,0 +1,92 @@
+// 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 upgrade
+
+import (
+ "os"
+ "testing"
+
+ "github.com/goposta/posta/internal/models"
+ "github.com/lib/pq"
+ "gorm.io/driver/postgres"
+ "gorm.io/gorm"
+ "gorm.io/gorm/logger"
+)
+
+func TestDecodeEmailSenders(t *testing.T) {
+ dsn := os.Getenv("TEST_DATABASE_DSN")
+ if dsn == "" {
+ dsn = "host=localhost user=posta password=posta dbname=posta port=5432 sslmode=disable"
+ }
+ db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{Logger: logger.Default.LogMode(logger.Silent)})
+ if err != nil {
+ t.Skipf("skipping: no test database available: %v", err)
+ }
+ if err := db.AutoMigrate(&models.User{}, &models.APIKey{}, &models.Email{}); err != nil {
+ t.Skipf("skipping: cannot migrate test schema: %v", err)
+ }
+ tx := db.Begin()
+ t.Cleanup(func() { tx.Rollback() })
+
+ user := &models.User{Name: "test", Email: "[email protected]", PasswordHash: "x"}
+ if err := tx.Create(user).Error; err != nil {
+ t.Fatalf("create user: %v", err)
+ }
+
+ cases := []struct{ stored, want string }{
+ {"=?UTF-8?b?0KHQsNC90LDRgtC+0YDQuNC5LtGA0YM=?= <[email protected]>", `"Санаторий.ру" <[email protected]>`},
+ {"=?UTF-8?b?0JDQutC80LU=?= =?UTF-8?b?INCz0YDRg9C/0L8=?= <[email protected]>", `"Акме групп" <[email protected]>`},
+ {"=?UTF-8?q?Acme?= <[email protected]>", `"Acme" <[email protected]>`},
+ {`"Санаторий.ру" <[email protected]>`, `"Санаторий.ру" <[email protected]>`},
+ {"=?broken <not an address", "=?broken <not an address"},
+ }
+ ids := make([]uint, len(cases))
+ for i, tc := range cases {
+ em := &models.Email{UserID: user.ID, Sender: tc.stored, Subject: "s", Recipients: pq.StringArray{"[email protected]"}}
+ if err := tx.Create(em).Error; err != nil {
+ t.Fatalf("create email: %v", err)
+ }
+ ids[i] = em.ID
+ }
+
+ if err := applyDecodeEmailSenders(tx); err != nil {
+ t.Fatalf("apply: %v", err)
+ }
+
+ for i, tc := range cases {
+ var em models.Email
+ if err := tx.First(&em, ids[i]).Error; err != nil {
+ t.Fatalf("load email: %v", err)
+ }
+ if em.Sender != tc.want {
+ t.Errorf("sender %q became %q, want %q", tc.stored, em.Sender, tc.want)
+ }
+ }
+}
+
+func TestDecodeEmailSendersStepIsRegistered(t *testing.T) {
+ for _, s := range Registry() {
+ if s.ID == "2026-10-13-decode-email-senders" {
+ return
+ }
+ }
+ t.Fatal(`upgrade step "2026-10-13-decode-email-senders" is not registered`)
+}