fix(mail): add Message-ID to email messages
This commit is contained in:
@@ -1,10 +1,13 @@
|
||||
package mail
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/smtp"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"wedding-server/internal/config"
|
||||
"wedding-server/internal/store"
|
||||
@@ -18,6 +21,19 @@ func New(cfg config.EmailConfig) *Mailer {
|
||||
return &Mailer{cfg: cfg}
|
||||
}
|
||||
|
||||
// newMessageID builds an RFC 5322 Message-ID, scoped to the sender's own
|
||||
// domain so it satisfies mail servers that reject messages lacking one.
|
||||
func newMessageID(from string) string {
|
||||
domain := "localhost"
|
||||
if i := strings.LastIndex(from, "@"); i != -1 {
|
||||
domain = strings.TrimSuffix(from[i+1:], ">")
|
||||
}
|
||||
|
||||
var buf [16]byte
|
||||
_, _ = rand.Read(buf[:])
|
||||
return fmt.Sprintf("<%d.%s@%s>", time.Now().UnixNano(), hex.EncodeToString(buf[:]), domain)
|
||||
}
|
||||
|
||||
func (m *Mailer) send(subject, body string) error {
|
||||
if m.cfg.SMTPHost == "" {
|
||||
log.Printf("mail: SMTP not configured, skipping email %q", subject)
|
||||
@@ -28,8 +44,8 @@ func (m *Mailer) send(subject, body string) error {
|
||||
auth := smtp.PlainAuth("", m.cfg.SMTPUser, m.cfg.SMTPPass, m.cfg.SMTPHost)
|
||||
|
||||
msg := fmt.Sprintf(
|
||||
"From: %s\r\nTo: %s\r\nSubject: %s\r\nContent-Type: text/plain; charset=UTF-8\r\n\r\n%s",
|
||||
m.cfg.From, m.cfg.To, subject, body,
|
||||
"From: %s\r\nTo: %s\r\nSubject: %s\r\nMessage-ID: %s\r\nDate: %s\r\nContent-Type: text/plain; charset=UTF-8\r\n\r\n%s",
|
||||
m.cfg.From, m.cfg.To, subject, newMessageID(m.cfg.From), time.Now().Format(time.RFC1123Z), body,
|
||||
)
|
||||
|
||||
return smtp.SendMail(addr, auth, m.cfg.From, []string{m.cfg.To}, []byte(msg))
|
||||
|
||||
Reference in New Issue
Block a user