package httpapi import ( "net/http" "strings" "github.com/descrybe/descrybe-v2/apps/api/internal/campaigns" "github.com/descrybe/descrybe-v2/apps/api/internal/mail" ) // POST /api/admin/settings/mail/test — send a one-off SMTP probe using platform settings. func (s *Server) handleAdminTestMail(w http.ResponseWriter, r *http.Request) { if s.Mail == nil { Error(w, http.StatusServiceUnavailable, "mailer unavailable") return } var body struct { To string `json:"to"` } if err := DecodeJSONOptional(r, &body); err != nil { Error(w, http.StatusBadRequest, "invalid json") return } to := strings.TrimSpace(body.To) if to == "" { if email, ok := s.sessionUserEmail(r.Context()); ok { to = email } } if to == "" { Error(w, http.StatusBadRequest, "to is required") return } normalized, err := campaigns.NormalizeEmail(to) if err != nil { Error(w, http.StatusBadRequest, "invalid email") return } to = normalized if s.PlatformSettings != nil { if dry, err := s.PlatformSettings.ResolveEmailDryRun(r.Context()); err == nil && dry.DryRun { JSON(w, http.StatusOK, map[string]any{ "status": "skipped", "smtp_enabled": false, "dry_run": true, "message": "Email dry-run is on; disable dry-run in admin platform mail settings to send a real probe", }) return } } enabled := s.Mail.Enabled() if !enabled { JSON(w, http.StatusOK, map[string]any{ "status": "skipped", "smtp_enabled": false, "message": "SMTP is not configured in admin platform mail settings", }) return } msg := mail.Message{ To: to, Subject: "Descrybe SMTP test", Text: "This is a Descrybe platform SMTP test message.", HTML: "
This is a Descrybe platform SMTP test message.
", } if err := s.Mail.Send(msg); err != nil { JSON(w, http.StatusOK, map[string]any{ "status": "failed", "smtp_enabled": true, "message": "SMTP send failed — check host/credentials in platform mail settings", }) return } JSON(w, http.StatusOK, map[string]any{ "status": "ok", "smtp_enabled": true, "message": "Test message accepted by SMTP", }) }