feat: Move mail system to shared
Server Deploy / deploy (push) Successful in 2m46s

This commit is contained in:
2026-07-28 09:50:46 +01:00
parent 5e326335af
commit a232c74990
49 changed files with 1206 additions and 706 deletions
+16 -25
View File
@@ -11,7 +11,7 @@ import (
"strings"
"time"
"github.com/mrhid6/vantage/sitesvc/internal/mail"
"github.com/mrhid6/vantage/shared/mail"
)
const (
@@ -21,15 +21,18 @@ const (
)
type Server struct {
mail mail.Config
limiter *limiter
mail mail.Sender
contact string // where enquiries go; the sender itself has no default recipient
limiter *limiter
allowOrigin map[string]bool
trustProxy bool
}
func New(mailCfg mail.Config) *Server {
func New(sender mail.Sender, contactTo string) *Server {
return &Server{
mail: mailCfg,
mail: sender,
contact: contactTo,
limiter: newLimiter(perIPLimit, perIPWindow),
allowOrigin: parseOrigins(os.Getenv("SITE_ORIGIN")),
trustProxy: os.Getenv("TRUST_PROXY") == "true",
@@ -168,7 +171,7 @@ func (s *Server) handleContact(w http.ResponseWriter, r *http.Request) {
return
}
if !s.mail.Enabled() {
if !s.mail.Enabled() || s.contact == "" {
log.Println("contact submission dropped: smtp is not configured")
writeJSON(w, http.StatusServiceUnavailable, map[string]string{
"error": "The contact form is unavailable right now. Email support@hostxtra.co.uk directly.",
@@ -176,7 +179,13 @@ func (s *Server) handleContact(w http.ResponseWriter, r *http.Request) {
return
}
if err := s.mail.Send(subject(addr, fields), plainBody(addr, fields), addr); err != nil {
if err := s.mail.SendEnquiry(s.contact, mail.Enquiry{
Name: fields["name"],
Email: addr,
Servers: fields["servers"],
Topic: fields["topic"],
Message: fields["message"],
}); err != nil {
log.Printf("contact send: %v", err)
writeJSON(w, http.StatusBadGateway, map[string]string{
"error": "We could not send that. Try again, or email support@hostxtra.co.uk directly.",
@@ -187,24 +196,6 @@ func (s *Server) handleContact(w http.ResponseWriter, r *http.Request) {
writeJSON(w, http.StatusAccepted, map[string]string{"status": "received"})
}
func subject(addr string, fields map[string]string) string {
return fmt.Sprintf("[Vantage] %s %s", fields["topic"], addr)
}
func plainBody(addr string, fields map[string]string) string {
var b strings.Builder
b.WriteString("New contact enquiry from the Vantage site.\n\n")
fmt.Fprintf(&b, "Name: %s\n", fields["name"])
fmt.Fprintf(&b, "Email: %s\n", addr)
fmt.Fprintf(&b, "Servers: %s\n", fields["servers"])
fmt.Fprintf(&b, "Topic: %s\n", fields["topic"])
fmt.Fprintf(&b, "Received: %s\n\n", time.Now().UTC().Format(time.RFC1123))
b.WriteString("Message:\n")
b.WriteString(fields["message"])
b.WriteString("\n")
return b.String()
}
func decode(w http.ResponseWriter, r *http.Request, dst any) bool {
r.Body = http.MaxBytesReader(w, r.Body, maxBodyBytes)
dec := json.NewDecoder(r.Body)