feat: harden SMTP transport

Add explicit TLS modes, context-aware delivery, and typed transport errors.
Preserve raw MIME messages and cover the new delivery paths with local SMTP tests.
This commit is contained in:
2026-08-18 18:16:22 -06:00
parent 537fbeebd9
commit b3c22c261e
12 changed files with 1560 additions and 652 deletions
+21 -8
View File
@@ -2,21 +2,30 @@ package main
import (
"bytes"
"context"
"log"
"net/smtp"
"time"
"gitea.urkob.com/urko/emailsender/pkg/email"
"gitea.wittrail.com/urko/emailsender/pkg/email"
)
func main() {
// Here fill with real data
emailService := email.NewInsecure(email.SecureConfig{
Auth: smtp.PlainAuth("", "your@email.com", "your-password", "smtp.youremail.com"),
Host: "smtp.youremail.com",
Port: "587",
From: "your@email.com",
emailService, err := email.New(email.Config{
Auth: smtp.PlainAuth("", "your@email.com", "your-password", "smtp.youremail.com"),
Host: "smtp.youremail.com",
Port: "587",
From: "your@email.com",
TLSMode: email.TLSModeSTARTTLS,
ConnectTimeout: 10 * time.Second,
OperationTimeout: 30 * time.Second,
})
if err != nil {
log.Fatal(err)
}
emailService.SendEmail(email.MessageWithAttachments{
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
err = emailService.SendEmailContext(ctx, email.MessageWithAttachments{
To: "other@email.com",
Subject: "Test Email",
Body: "<html><body><p>Here your body, you can attach as html<p/></body></html>",
@@ -27,4 +36,8 @@ func main() {
},
},
})
cancel()
if err != nil {
log.Fatal(err)
}
}