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
email: add Date and Message-ID headers, randomize MIME boundaries
Akili3a7ea99ea61a
diff --git a/internal/services/email/service.go b/internal/services/email/service.goindex 7114c46..e079deb 100644--- a/internal/services/email/service.go+++ b/internal/services/email/service.go@@ -1326,6 +1326,11 @@ func diffRecipients(original, filtered []string) []string {// filterCustomHeaders removes headers that could conflict with system-set// headers or be used for spoofing. It returns a sanitized copy.+//+// Date and Message-ID are deliberately allowed: buildMessage emits them only+// when the caller has not supplied its own, so an integration that needs a+// stable Message-ID (e.g. for threading against an external system) can set+// one while every other message still gets a generated RFC 5322 pair.func filterCustomHeaders(headers map[string]string) map[string]string {// Headers that are set by the system and must not be overridden.reserved := map[string]bool{diff --git a/internal/services/email/smtp_sender.go b/internal/services/email/smtp_sender.goindex 50ef8ba..a06a595 100644--- a/internal/services/email/smtp_sender.go+++ b/internal/services/email/smtp_sender.go@@ -4,8 +4,10 @@package emailimport (+ "crypto/rand""crypto/tls""encoding/base64"+ "encoding/hex""errors""fmt""mime"@@ -15,7 +17,9 @@ import ("net/textproto""slices""strings"+ "time"+ "github.com/google/uuid""github.com/goposta/posta/internal/models")@@ -289,12 +293,54 @@ func isASCII(s string) bool {return true}+// hasHeader reports whether the caller-supplied headers already include key+// (header names are case-insensitive per RFC 5322).+func hasHeader(headers map[string]string, key string) bool {+ for k := range headers {+ if strings.EqualFold(k, key) {+ return true+ }+ }+ return false+}++// newBoundary returns a random MIME boundary. A fixed boundary shared by every+// message would collide with quoted or nested content that happens to contain+// the same string, and makes all Posta mail trivially fingerprintable. The+// token stays short enough that the Content-Type header line fits in 76+// characters even with the boundary quoted.+func newBoundary() string {+ var token [12]byte+ if _, err := rand.Read(token[:]); err != nil {+ // crypto/rand failing is extraordinary; a UUID keeps the boundary unique.+ return "posta-" + uuid.NewString()+ }+ return "posta-" + hex.EncodeToString(token[:])+}+func buildMessage(from string, to []string, subject, htmlBody, textBody string, attachments []models.Attachment, headers map[string]string, listUnsubscribeURL, listUnsubscribeMailto string, listUnsubscribePost bool) []byte {var b strings.Builderfmt.Fprintf(&b, "From: %s\r\n", from)fmt.Fprintf(&b, "To: %s\r\n", strings.Join(to, ", "))fmt.Fprintf(&b, "Subject: %s\r\n", mime.QEncoding.Encode("UTF-8", subject))++ // RFC 5322 requires Date and recommends Message-ID; relays are free to+ // forward a message without adding them, so emit both unless the caller+ // already supplied one through custom headers.+ if !hasHeader(headers, "Date") {+ fmt.Fprintf(&b, "Date: %s\r\n", time.Now().Format(time.RFC1123Z))+ }+ if !hasHeader(headers, "Message-ID") {+ domain := "posta.local"+ if addr, err := mail.ParseAddress(from); err == nil {+ if i := strings.LastIndex(addr.Address, "@"); i >= 0 && i+1 < len(addr.Address) {+ domain = addr.Address[i+1:]+ }+ }+ fmt.Fprintf(&b, "Message-ID: <%s@%s>\r\n", uuid.NewString(), domain)+ }+b.WriteString("MIME-Version: 1.0\r\n")// RFC 2369 / 8058 List-Unsubscribe. Emit mailto first, then the https URL.@@ -321,8 +367,8 @@ func buildMessage(from string, to []string, subject, htmlBody, textBody string,hasAttachments := len(attachments) > 0if hasAttachments {- mixedBoundary := "posta-mixed-boundary"- altBoundary := "posta-alt-boundary"+ mixedBoundary := newBoundary()+ altBoundary := newBoundary()fmt.Fprintf(&b, "Content-Type: multipart/mixed; boundary=\"%s\"\r\n\r\n", mixedBoundary)@@ -366,7 +412,7 @@ func buildMessage(from string, to []string, subject, htmlBody, textBody string,}fmt.Fprintf(&b, "\r\n--%s--\r\n", mixedBoundary)} else {- altBoundary := "posta-boundary-abc123"+ altBoundary := newBoundary()if htmlBody != "" && textBody != "" {fmt.Fprintf(&b, "Content-Type: multipart/alternative; boundary=\"%s\"\r\n\r\n", altBoundary)fmt.Fprintf(&b, "--%s\r\n", altBoundary)diff --git a/internal/services/email/smtp_sender_test.go b/internal/services/email/smtp_sender_test.goindex 4a6846e..54664d6 100644--- a/internal/services/email/smtp_sender_test.go+++ b/internal/services/email/smtp_sender_test.go@@ -6,9 +6,14 @@ package emailimport ("errors""fmt"+ "mime"+ "net/mail""net/smtp""net/textproto"+ "regexp"+ "strings""testing"+ "time")func TestSmtpReply(t *testing.T) {@@ -111,3 +116,127 @@ func TestAuthLoginRefusesUnencryptedConnection(t *testing.T) {t.Fatal("LOGIN over plaintext must be refused")}}++var messageIDRe = regexp.MustCompile(`^<[^@<>\s]+@[^@<>\s]+>$`)++// RFC 5322 requires Date and recommends Message-ID. A relay is free to forward+// a message without adding either, so buildMessage must emit them itself.+func TestBuildMessageAddsDateAndMessageID(t *testing.T) {+ raw := buildMessage(+ "Sender <[email protected]>", []string{"[email protected]"},+ "subject", "", "body",+ nil, nil, "", "", false,+ )++ msg, err := mail.ReadMessage(strings.NewReader(string(raw)))+ if err != nil {+ t.Fatalf("built message does not parse: %v\n---\n%s", err, raw)+ }++ date, err := msg.Header.Date()+ if err != nil {+ t.Fatalf("Date header missing or unparsable: %v", err)+ }+ if time.Since(date) > time.Minute || time.Until(date) > time.Minute {+ t.Errorf("Date = %v, want within a minute of now", date)+ }++ mid := msg.Header.Get("Message-ID")+ if !messageIDRe.MatchString(mid) {+ t.Fatalf("Message-ID %q is not a well-formed msg-id", mid)+ }+ // The right-hand side should be the sender domain, so replies and bounces+ // can be correlated to the originating domain.+ if !strings.HasSuffix(mid, "@example.com>") {+ t.Errorf("Message-ID %q does not use the sender domain", mid)+ }+}++// A caller that supplies its own Date or Message-ID (custom headers) must not+// end up with a duplicate second header.+func TestBuildMessageKeepsCallerSuppliedDateAndMessageID(t *testing.T) {+ headers := map[string]string{+ "Date": "Fri, 19 Sep 2026 22:33:25 +0000",+ "Message-ID": "<[email protected]>",+ }+ raw := buildMessage(+ "[email protected]", []string{"[email protected]"},+ "subject", "", "body",+ nil, headers, "", "", false,+ )++ if got := strings.Count(string(raw), "\r\nDate:"); got != 1 {+ t.Errorf("found %d Date headers, want exactly 1\n---\n%s", got, raw)+ }+ if got := strings.Count(string(raw), "\r\nMessage-ID:"); got != 1 {+ t.Errorf("found %d Message-ID headers, want exactly 1\n---\n%s", got, raw)+ }++ msg, err := mail.ReadMessage(strings.NewReader(string(raw)))+ if err != nil {+ t.Fatalf("built message does not parse: %v", err)+ }+ if got := msg.Header.Get("Message-ID"); got != "<[email protected]>" {+ t.Errorf("Message-ID = %q, want the caller-supplied value", got)+ }+}++// Custom-header lookups are case-insensitive: a lowercase "message-id" must+// also suppress the generated one.+func TestBuildMessageHeaderLookupIsCaseInsensitive(t *testing.T) {+ raw := buildMessage(+ "[email protected]", []string{"[email protected]"},+ "subject", "", "body",+ nil, map[string]string{"message-id": "<[email protected]>"}, "", "", false,+ )+ if strings.Count(strings.ToLower(string(raw)), "\r\nmessage-id:") != 1 {+ t.Fatalf("expected exactly one Message-ID header:\n%s", raw)+ }+}++// Two messages built back to back must not share a Message-ID, and every+// multipart message must get its own boundary instead of one hard-coded string.+func TestBuildMessageGeneratesUniqueMessageIDsAndBoundaries(t *testing.T) {+ const layout = "subject"++ var mids []string+ var boundaries []string+ for i := 0; i < 3; i++ {+ raw := buildMessage(+ "[email protected]", []string{"[email protected]"},+ layout, "<p>hi</p>", "hi", nil, nil, "", "", false,+ )+ msg, err := mail.ReadMessage(strings.NewReader(string(raw)))+ if err != nil {+ t.Fatalf("built message does not parse: %v", err)+ }+ mids = append(mids, msg.Header.Get("Message-ID"))++ _, params, err := mime.ParseMediaType(msg.Header.Get("Content-Type"))+ if err != nil {+ t.Fatalf("ParseMediaType: %v", err)+ }+ boundaries = append(boundaries, params["boundary"])+ }++ seen := map[string]bool{}+ for _, mid := range mids {+ if seen[mid] {+ t.Fatalf("duplicate Message-ID %q across messages", mid)+ }+ seen[mid] = true+ }+ seen = map[string]bool{}+ for _, bd := range boundaries {+ if bd == "" {+ t.Fatal("multipart message is missing a boundary parameter")+ }+ if seen[bd] {+ t.Fatalf("duplicate MIME boundary %q across messages", bd)+ }+ seen[bd] = true+ if strings.HasPrefix(bd, "posta-boundary-abc123") {+ t.Fatalf("boundary %q is the old fixed value", bd)+ }+ }+}