From 7a467a5a3b2adfc252dfc7f24c425e08aa4c651b Mon Sep 17 00:00:00 2001 From: Kailash Nadh Date: Sun, 31 May 2020 21:16:56 +0530 Subject: [PATCH] Add support for custom e-mail headers per SMTP server --- config.toml.sample | 4 ++++ internal/messenger/emailer.go | 22 ++++++++++++++++------ 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/config.toml.sample b/config.toml.sample index ab345c2..71362e8 100644 --- a/config.toml.sample +++ b/config.toml.sample @@ -116,6 +116,10 @@ max_idle = 10 tls_enabled = true tls_skip_verify = false + # One or more optional custom headers to be attached to all e-mails + # sent from this SMTP server. Uncomment the line to enable. + # email_headers = { "X-Sender" = "listmonk", "X-Custom-Header" = "listmonk" } + [smtp.postal] enabled = false host = "my.smtp.server2" diff --git a/internal/messenger/emailer.go b/internal/messenger/emailer.go index 1c96444..bb40374 100644 --- a/internal/messenger/emailer.go +++ b/internal/messenger/emailer.go @@ -5,6 +5,7 @@ import ( "fmt" "math/rand" "net/smtp" + "net/textproto" "github.com/jaytaylor/html2text" "github.com/knadh/smtppool" @@ -15,12 +16,13 @@ const emName = "email" // Server represents an SMTP server's credentials. type Server struct { Name string - Username string `json:"username"` - Password string `json:"password"` - AuthProtocol string `json:"auth_protocol"` - EmailFormat string `json:"email_format"` - TLSEnabled bool `json:"tls_enabled"` - TLSSkipVerify bool `json:"tls_skip_verify"` + Username string `json:"username"` + Password string `json:"password"` + AuthProtocol string `json:"auth_protocol"` + EmailFormat string `json:"email_format"` + TLSEnabled bool `json:"tls_enabled"` + TLSSkipVerify bool `json:"tls_skip_verify"` + EmailHeaders map[string]string `json:"email_headers"` // Rest of the options are embedded directly from the smtppool lib. // The JSON tag is for config unmarshal to work. @@ -128,6 +130,14 @@ func (e *Emailer) Push(fromAddr string, toAddr []string, subject string, m []byt Attachments: files, } + // If there are custom e-mail headers, attach them. + if len(srv.EmailHeaders) > 0 { + em.Headers = textproto.MIMEHeader{} + for k, v := range srv.EmailHeaders { + em.Headers.Set(k, v) + } + } + switch srv.EmailFormat { case "html": em.HTML = m