[bugfix] add Date and Message-ID headers for email (#3031)

* [bugfix] add Date and Message-ID headers for email

This should make spam filters more happy, as most of them grant some
negative score for not having those headers. Also the Date is convenient
for the user receiving the mail.

* make golangci-lint happy
This commit is contained in:
Julian 2024-06-22 23:36:30 +02:00 committed by GitHub
commit c2738474d5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 33 additions and 3 deletions

View file

@ -26,7 +26,9 @@ import (
"path/filepath"
"strings"
"text/template"
"time"
"github.com/google/uuid"
"github.com/superseriousbusiness/gotosocial/internal/config"
"github.com/superseriousbusiness/gotosocial/internal/gtserror"
)
@ -37,7 +39,7 @@ func (s *sender) sendTemplate(template string, subject string, data any, toAddre
return err
}
msg, err := assembleMessage(subject, buf.String(), s.from, toAddresses...)
msg, err := assembleMessage(subject, buf.String(), s.from, s.msgIDHost, toAddresses...)
if err != nil {
return err
}
@ -65,7 +67,7 @@ func loadTemplates(templateBaseDir string) (*template.Template, error) {
// assembleMessage assembles a valid email message following:
// - https://datatracker.ietf.org/doc/html/rfc2822
// - https://pkg.go.dev/net/smtp#SendMail
func assembleMessage(mailSubject string, mailBody string, mailFrom string, mailTo ...string) ([]byte, error) {
func assembleMessage(mailSubject string, mailBody string, mailFrom string, msgIDHost string, mailTo ...string) ([]byte, error) {
if strings.ContainsAny(mailSubject, "\r\n") {
return nil, errors.New("email subject must not contain newline characters")
}
@ -103,7 +105,9 @@ func assembleMessage(mailSubject string, mailBody string, mailFrom string, mailT
// msg headers.'
msg.WriteString("To: Undisclosed Recipients:;" + CRLF)
}
msg.WriteString("Date: " + time.Now().Format(time.RFC822Z) + CRLF)
msg.WriteString("From: " + mailFrom + CRLF)
msg.WriteString("Message-ID: <" + uuid.New().String() + "@" + msgIDHost + ">" + CRLF)
msg.WriteString("Subject: " + mailSubject + CRLF)
msg.WriteString("MIME-Version: 1.0" + CRLF)
msg.WriteString("Content-Transfer-Encoding: 8bit" + CRLF)