mirror of
https://github.com/Dvorinka/SendMail.git
synced 2026-07-29 01:53:47 +00:00
feat(main): enhance logging and restrict server binding
Add detailed structured logging to `sendHandler` and `sendParamsHandler` to track request details, domain lookups, and SMTP outcomes (success/failure). Update the server to bind to `localhost` instead of `0.0.0.0` for improved security.
This commit is contained in:
@@ -215,10 +215,13 @@ func sendHandler(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
var data FormData
|
||||
if err := json.NewDecoder(r.Body).Decode(&data); err != nil {
|
||||
log.Printf("[send] Invalid JSON from %s", r.RemoteAddr)
|
||||
http.Error(w, "Invalid JSON", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
log.Printf("[send] request from=%s domain=%q subject=%q", data.Email, data.Domain, data.Subject)
|
||||
|
||||
// Determine SMTP configuration: prefer query params if provided; otherwise use domain map
|
||||
q := r.URL.Query()
|
||||
qEmail := strings.TrimSpace(q.Get("email"))
|
||||
@@ -237,15 +240,18 @@ func sendHandler(w http.ResponseWriter, r *http.Request) {
|
||||
http.Error(w, "Invalid port", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
log.Printf("[send] using query params host=%s port=%d email=%s", qServer, port, qEmail)
|
||||
config = EmailConfig{Email: qEmail, Password: qPass, SMTPHost: qServer, SMTPPort: port}
|
||||
} else {
|
||||
data.Domain = strings.ToLower(strings.TrimSpace(data.Domain))
|
||||
var ok bool
|
||||
config, ok = domainMap[data.Domain]
|
||||
if !ok {
|
||||
log.Printf("[send] FAILED unknown domain=%q", data.Domain)
|
||||
http.Error(w, "Unknown domain", http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
log.Printf("[send] using domain map host=%s port=%d email=%s", config.SMTPHost, config.SMTPPort, config.Email)
|
||||
}
|
||||
|
||||
m := gomail.NewMessage()
|
||||
@@ -261,11 +267,12 @@ func sendHandler(w http.ResponseWriter, r *http.Request) {
|
||||
d.SSL = true // Using SSL for secure connection
|
||||
|
||||
if err := d.DialAndSend(m); err != nil {
|
||||
log.Println("Email send error:", err)
|
||||
log.Printf("[send] FAILED to=%s subject=%q via %s:%d error=%v", config.Email, data.Subject, config.SMTPHost, config.SMTPPort, err)
|
||||
http.Error(w, "Failed to send email", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
log.Printf("[send] SUCCESS to=%s subject=%q via %s:%d", config.Email, data.Subject, config.SMTPHost, config.SMTPPort)
|
||||
w.Write([]byte("Email sent successfully"))
|
||||
}
|
||||
|
||||
@@ -340,11 +347,12 @@ func sendParamsHandler(w http.ResponseWriter, r *http.Request) {
|
||||
d.SSL = true
|
||||
|
||||
if err := d.DialAndSend(m); err != nil {
|
||||
log.Println("Email send error:", err)
|
||||
log.Printf("[send-params] FAILED to=%s subject=%q via %s:%d error=%v", to, subject, smtpServer, port, err)
|
||||
http.Error(w, "Failed to send email", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
log.Printf("[send-params] SUCCESS to=%s subject=%q via %s:%d", to, subject, smtpServer, port)
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_ = json.NewEncoder(w).Encode(map[string]string{"status": "ok", "message": "Email sent successfully"})
|
||||
}
|
||||
@@ -355,6 +363,6 @@ func main() {
|
||||
http.HandleFunc("/", enableCors(rootHandler))
|
||||
http.HandleFunc("/send", enableCors(sendHandler))
|
||||
http.HandleFunc("/send-params", enableCors(sendParamsHandler))
|
||||
log.Println("Server running on :660")
|
||||
log.Fatal(http.ListenAndServe("0.0.0.0:660", nil))
|
||||
log.Println("Server running on localhost:660")
|
||||
log.Fatal(http.ListenAndServe("localhost:660", nil))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user