Encode mail subject as rfc2047 Q-Encoding and body as rfc2045 Quoted-Printable Encoding

This commit is contained in:
ChaoticByte 2025-06-12 20:13:08 +02:00
parent 9b00959fdb
commit ab235820b7
No known key found for this signature in database

28
mail.go
View file

@ -4,33 +4,33 @@ package main
import ( import (
"crypto/tls" "crypto/tls"
"encoding/base64"
"fmt" "fmt"
"mime"
"mime/quotedprintable"
"net/mail" "net/mail"
"net/smtp" "net/smtp"
"strings"
"time" "time"
) )
const MAIL_LINE_SEP = "\r\n"
type MailContent struct { type MailContent struct {
Subject string Subject string
Body string Body string
} }
func (c MailContent) serializeValidMail(from string, to string) []byte { func (c MailContent) serializeValidMail(from string, to string) []byte {
// We'll send base64 encoded Subject & Body, because we Dschörmäns have umlauts // format subject using Q Encoding from RFC2047
// and I'm too lazy to encode ä into =E4 and so on subjectEncoded := mime.QEncoding.Encode("utf-8", c.Subject)
subjectEncoded := base64.StdEncoding.EncodeToString([]byte(c.Subject)) // format body using Quoted-Printable Encoding from RFC2045
bodyEncoded := base64.StdEncoding.EncodeToString([]byte(c.Body)) var bodyEncoded strings.Builder
bew := quotedprintable.NewWriter(&bodyEncoded)
bew.Write([]byte(c.Body))
bew.Close()
// glue it all together
data := fmt.Appendf(nil, data := fmt.Appendf(nil,
"Content-Type: text/plain; charset=\"utf-8\"\r\nContent-Transfer-Encoding: base64\r\nFrom: %v%vTo: %v%vSubject: =?utf-8?b?%v?=%v%v%v", "Content-Type: text/plain; charset=\"utf-8\"\r\nContent-Transfer-Encoding: Quoted-Printable\r\nFrom: %v\r\nTo: %v\r\nSubject: %v\r\n\r\n%v",
from, MAIL_LINE_SEP, from, to, subjectEncoded, bodyEncoded.String(),
to, MAIL_LINE_SEP, )
subjectEncoded, MAIL_LINE_SEP,
MAIL_LINE_SEP,
bodyEncoded)
// done, I guess
return data return data
} }