From fde45a5138ed813c0dae4515e2685a4d82161e46 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ferdinand=20M=C3=BCtsch?= Date: Mon, 6 Mar 2023 20:31:31 +0100 Subject: [PATCH] fix: include missing mail headers (resolve #472) --- models/mail.go | 33 ++++++++++++++++++++++++++++----- models/mail_address.go | 13 ++++++++++++- services/mail/smtp.go | 2 ++ 3 files changed, 42 insertions(+), 6 deletions(-) diff --git a/models/mail.go b/models/mail.go index 052002b..ca68c29 100644 --- a/models/mail.go +++ b/models/mail.go @@ -2,18 +2,22 @@ package models import ( "fmt" + uuid "github.com/satori/go.uuid" "strings" + "time" ) const HtmlType = "text/html; charset=UTF-8" const PlainType = "text/html; charset=UTF-8" type Mail struct { - From MailAddress - To MailAddresses - Subject string - Body string - Type string + From MailAddress + To MailAddresses + Subject string + Body string + Type string + Date time.Time + MessageID string } func (m *Mail) WithText(text string) *Mail { @@ -28,17 +32,36 @@ func (m *Mail) WithHTML(html string) *Mail { return m } +func (m *Mail) Sanitized() *Mail { + if m.Type == "" { + m.Type = PlainType + } + if m.Date.IsZero() { + m.Date = time.Now() + } + if m.MessageID == "" { + m.MessageID = fmt.Sprintf("<%s@%s>", uuid.NewV4().String(), m.From.Domain()) + } + return m +} + func (m *Mail) String() string { return fmt.Sprintf("To: %s\r\n"+ "From: %s\r\n"+ "Subject: %s\r\n"+ + "Message-ID: %s\r\n"+ + "MIME-Version: 1.0\r\n"+ "Content-Type: %s\r\n"+ + "Content-Transfer-Encoding: 8bit\r\n"+ + "Date: %s\r\n"+ "\r\n"+ "%s\r\n", strings.Join(m.To.RawStrings(), ", "), m.From.String(), m.Subject, + m.MessageID, m.Type, + m.Date.Format(time.RFC1123Z), m.Body, ) } diff --git a/models/mail_address.go b/models/mail_address.go index 324f63e..3ce5136 100644 --- a/models/mail_address.go +++ b/models/mail_address.go @@ -1,6 +1,9 @@ package models -import "regexp" +import ( + "regexp" + "strings" +) const ( MailPattern = "[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\\.[a-zA-Z0-9-.]+" @@ -36,6 +39,14 @@ func (m MailAddress) Raw() string { return "" } +func (m MailAddress) Domain() string { + split := strings.Split(m.Raw(), "@") + if len(split) != 2 { + return "" + } + return split[1] +} + func (m MailAddress) Valid() bool { return emailAddrRegex.Match([]byte(m)) } diff --git a/services/mail/smtp.go b/services/mail/smtp.go index 4888e88..e9fc2fe 100644 --- a/services/mail/smtp.go +++ b/services/mail/smtp.go @@ -26,6 +26,8 @@ func NewSMTPSendingService(config conf.SMTPMailConfig) *SMTPSendingService { } func (s *SMTPSendingService) Send(mail *models.Mail) error { + mail = mail.Sanitized() + dial := smtp.Dial if s.config.TLS { dial = func(addr string) (*smtp.Client, error) {