From 74322cda36615c0b113eadc78280aaf8ad8cdaf6 Mon Sep 17 00:00:00 2001 From: Kailash Nadh Date: Thu, 10 Nov 2022 00:01:24 +0530 Subject: [PATCH] Fix e-mail From/Return-Path envelope sender. Closes #908, closes #1008. --- go.mod | 2 +- go.sum | 2 ++ internal/messenger/email/email.go | 27 ++++++++++++++++++--------- 3 files changed, 21 insertions(+), 10 deletions(-) diff --git a/go.mod b/go.mod index 26ec7a6..1d0da4c 100644 --- a/go.mod +++ b/go.mod @@ -15,7 +15,7 @@ require ( github.com/knadh/go-pop3 v0.3.0 github.com/knadh/goyesql/v2 v2.1.2 github.com/knadh/koanf v1.2.3 - github.com/knadh/smtppool v1.0.0 + github.com/knadh/smtppool v1.0.1 github.com/knadh/stuffbin v1.1.0 github.com/labstack/echo/v4 v4.9.0 github.com/lib/pq v1.10.3 diff --git a/go.sum b/go.sum index a29a35e..bffce65 100644 --- a/go.sum +++ b/go.sum @@ -84,6 +84,8 @@ github.com/knadh/koanf v1.2.3 h1:2Rkr0YhhYk+4QEOm800Q3Pu0Wi87svTxM6uuEb4WhYw= github.com/knadh/koanf v1.2.3/go.mod h1:xpPTwMhsA/aaQLAilyCCqfpEiY1gpa160AiCuWHJUjY= github.com/knadh/smtppool v1.0.0 h1:1c8A7+nD8WdMMzvd3yY5aoY9QBgyGTA+Iq1IdlgKGJw= github.com/knadh/smtppool v1.0.0/go.mod h1:3DJHouXAgPDBz0kC50HukOsdapYSwIEfJGwuip46oCA= +github.com/knadh/smtppool v1.0.1 h1:gNASLB4x4W5jhVHRApwr9d2xSpsuannAFJVt/bVR8pA= +github.com/knadh/smtppool v1.0.1/go.mod h1:3DJHouXAgPDBz0kC50HukOsdapYSwIEfJGwuip46oCA= github.com/knadh/stuffbin v1.1.0 h1:f5S5BHzZALjuJEgTIOMC9NidEnBJM7Ze6Lu1GHR/lwU= github.com/knadh/stuffbin v1.1.0/go.mod h1:yVCFaWaKPubSNibBsTAJ939q2ABHudJQxRWZWV5yh+4= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= diff --git a/internal/messenger/email/email.go b/internal/messenger/email/email.go index cbbd513..90a41f8 100644 --- a/internal/messenger/email/email.go +++ b/internal/messenger/email/email.go @@ -11,7 +11,10 @@ import ( "github.com/knadh/smtppool" ) -const emName = "email" +const ( + emName = "email" + hdrReturnPath = "Return-Path" +) // Server represents an SMTP server's credentials. type Server struct { @@ -125,16 +128,22 @@ func (e *Emailer) Push(m messenger.Message) error { } em.Headers = textproto.MIMEHeader{} - // Attach e-mail level headers. - if len(m.Headers) > 0 { - em.Headers = m.Headers - } // Attach SMTP level headers. - if len(srv.EmailHeaders) > 0 { - for k, v := range srv.EmailHeaders { - em.Headers.Set(k, v) - } + for k, v := range srv.EmailHeaders { + em.Headers.Set(k, v) + } + + // Attach e-mail level headers. + for k, v := range m.Headers { + em.Headers.Set(k, v[0]) + } + + // If the `Return-Path` header is set, it should be set as the + // the SMTP envelope sender (via the Sender field of the email struct). + if sender := em.Headers.Get(hdrReturnPath); sender != "" { + em.Sender = sender + em.Headers.Del(hdrReturnPath) } switch m.ContentType {