From 3eec66e02b3413888b69bf534ee80feb121d7c72 Mon Sep 17 00:00:00 2001 From: ShinChven Date: Mon, 20 Nov 2023 21:28:15 +0800 Subject: [PATCH 1/2] feat: Add Message-ID to email headers to comply with RFC 5322 - Extract domain from SMTPFrom - Generate a unique Message-ID - Add Message-ID to email headers --- common/email.go | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/common/email.go b/common/email.go index 74f4cccd9e..7728cbbe5f 100644 --- a/common/email.go +++ b/common/email.go @@ -1,6 +1,7 @@ package common import ( + "crypto/rand" "crypto/tls" "encoding/base64" "fmt" @@ -13,15 +14,29 @@ func SendEmail(subject string, receiver string, content string) error { SMTPFrom = SMTPAccount } encodedSubject := fmt.Sprintf("=?UTF-8?B?%s?=", base64.StdEncoding.EncodeToString([]byte(subject))) + + // Extract domain from SMTPFrom + domain := strings.Split(SMTPFrom, "@")[1] + + // Generate a unique Message-ID + buf := make([]byte, 16) + _, err := rand.Read(buf) + if err != nil { + return err + } + messageId := fmt.Sprintf("<%x@%s>", buf, domain) + mail := []byte(fmt.Sprintf("To: %s\r\n"+ "From: %s<%s>\r\n"+ "Subject: %s\r\n"+ + "Message-ID: %s\r\n"+ // Add Message-ID to avoid being treated as spam, RFC 5322 "Content-Type: text/html; charset=UTF-8\r\n\r\n%s\r\n", - receiver, SystemName, SMTPFrom, encodedSubject, content)) + receiver, SystemName, SMTPFrom, encodedSubject, messageId, content)) + auth := smtp.PlainAuth("", SMTPAccount, SMTPToken, SMTPServer) addr := fmt.Sprintf("%s:%d", SMTPServer, SMTPPort) to := strings.Split(receiver, ";") - var err error + if SMTPPort == 465 { tlsConfig := &tls.Config{ InsecureSkipVerify: true, From 94afdc8e325d5147fab07f40785983f01b6e7178 Mon Sep 17 00:00:00 2001 From: JustSong Date: Fri, 24 Nov 2023 20:51:16 +0800 Subject: [PATCH 2/2] chore: check slice length --- common/email.go | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/common/email.go b/common/email.go index 7728cbbe5f..7d6963cc35 100644 --- a/common/email.go +++ b/common/email.go @@ -14,10 +14,13 @@ func SendEmail(subject string, receiver string, content string) error { SMTPFrom = SMTPAccount } encodedSubject := fmt.Sprintf("=?UTF-8?B?%s?=", base64.StdEncoding.EncodeToString([]byte(subject))) - - // Extract domain from SMTPFrom - domain := strings.Split(SMTPFrom, "@")[1] + // Extract domain from SMTPFrom + parts := strings.Split(SMTPFrom, "@") + var domain string + if len(parts) > 1 { + domain = parts[1] + } // Generate a unique Message-ID buf := make([]byte, 16) _, err := rand.Read(buf) @@ -29,7 +32,7 @@ func SendEmail(subject string, receiver string, content string) error { mail := []byte(fmt.Sprintf("To: %s\r\n"+ "From: %s<%s>\r\n"+ "Subject: %s\r\n"+ - "Message-ID: %s\r\n"+ // Add Message-ID to avoid being treated as spam, RFC 5322 + "Message-ID: %s\r\n"+ // add Message-ID header to avoid being treated as spam, RFC 5322 "Content-Type: text/html; charset=UTF-8\r\n\r\n%s\r\n", receiver, SystemName, SMTPFrom, encodedSubject, messageId, content))