diff --git a/cmd/verstak-server/server.go b/cmd/verstak-server/server.go index cb271f7..c36e379 100644 --- a/cmd/verstak-server/server.go +++ b/cmd/verstak-server/server.go @@ -9,6 +9,7 @@ import ( "encoding/json" "fmt" "io" + "log" "net" "net/http" "net/smtp" @@ -401,9 +402,12 @@ func (s *Server) smtpSend(to, subject, body string) error { pass := s.smtpGet("smtp_pass") from := s.smtpGet("smtp_from") if host == "" || port == "" || from == "" { - return fmt.Errorf("SMTP not configured") + err := fmt.Errorf("SMTP not configured") + log.Printf("smtp: %v (to=%s)", err, to) + return err } addr := net.JoinHostPort(host, port) + log.Printf("smtp: sending to %s via %s:%s", to, host, port) msg := []byte("From: " + from + "\r\n" + "To: " + to + "\r\n" + "Subject: " + subject + "\r\n" + @@ -416,35 +420,59 @@ func (s *Server) smtpSend(to, subject, body string) error { tlsCfg := &tls.Config{ServerName: host} conn, err := tls.Dial("tcp", addr, tlsCfg) if err != nil { + log.Printf("smtp: tls dial error: %v", err) return err } cl, err := smtp.NewClient(conn, host) if err != nil { + log.Printf("smtp: new client error: %v", err) return err } defer cl.Close() if err := cl.Auth(auth); err != nil { + log.Printf("smtp: auth error: %v", err) return err } if err := cl.Mail(from); err != nil { + log.Printf("smtp: mail from error: %v", err) return err } if err := cl.Rcpt(to); err != nil { + log.Printf("smtp: rcpt error: %v", err) return err } w, err := cl.Data() if err != nil { + log.Printf("smtp: data error: %v", err) return err } _, err = w.Write(msg) if err != nil { + log.Printf("smtp: write error: %v", err) return err } - return w.Close() + if err := w.Close(); err != nil { + log.Printf("smtp: close error: %v", err) + return err + } + log.Printf("smtp: sent OK to %s", to) + return nil } - return smtp.SendMail(addr, auth, from, []string{to}, msg) + err := smtp.SendMail(addr, auth, from, []string{to}, msg) + if err != nil { + log.Printf("smtp: sendmail error (auth): %v", err) + } else { + log.Printf("smtp: sent OK to %s", to) + } + return err } - return smtp.SendMail(addr, nil, from, []string{to}, msg) + err := smtp.SendMail(addr, nil, from, []string{to}, msg) + if err != nil { + log.Printf("smtp: sendmail error (no auth): %v", err) + } else { + log.Printf("smtp: sent OK to %s", to) + } + return err } // ============================================================ @@ -635,12 +663,19 @@ func (s *Server) handleRegister(w http.ResponseWriter, r *http.Request) { // Try to send email. host := s.smtpGet("smtp_host") if host != "" { - confirmURL := fmt.Sprintf("%s/confirm?token=%s", s.smtpGet("server_url"), tokenStr) - if confirmURL == "" { + srvURL := s.smtpGet("server_url") + var confirmURL string + if srvURL != "" { + confirmURL = fmt.Sprintf("%s/confirm?token=%s", srvURL, tokenStr) + } else { confirmURL = fmt.Sprintf("/api/v1/auth/confirm?token=%s", tokenStr) } body := fmt.Sprintf("Welcome to Verstak Sync!\n\nPlease confirm your email by clicking:\n%s\n\nIf you did not register, ignore this message.", confirmURL) - s.smtpSend(req.Email, "Confirm your Verstak Sync account", body) + if err := s.smtpSend(req.Email, "Confirm your Verstak Sync account", body); err != nil { + log.Printf("register: failed to send confirm email: %v", err) + } + } else { + log.Printf("register: SMTP not configured, confirmation token=%s for user %s", tokenStr, req.Username) } jsonOK(w, map[string]string{"status": "confirmation_sent"}) } @@ -668,6 +703,7 @@ func (s *Server) handleConfirm(w http.ResponseWriter, r *http.Request) { return } s.db.Exec("UPDATE server_users SET confirmed=1 WHERE id=?", userID) + log.Printf("confirm: user %s confirmed email", userID) s.db.Exec("DELETE FROM server_email_tokens WHERE token=?", tokenStr) w.Header().Set("Content-Type", "text/html; charset=utf-8") w.Write([]byte("
You can now log in.
")) @@ -1105,12 +1141,19 @@ func (s *Server) handleUserWebRegister(w http.ResponseWriter, r *http.Request) { // Try to send email. host := s.smtpGet("smtp_host") if host != "" { - confirmURL := fmt.Sprintf("%s/api/v1/auth/confirm?token=%s", s.smtpGet("server_url"), tokenStr) - if confirmURL == fmt.Sprintf("/api/v1/auth/confirm?token=%s", tokenStr) { + srvURL := s.smtpGet("server_url") + var confirmURL string + if srvURL != "" { + confirmURL = fmt.Sprintf("%s/api/v1/auth/confirm?token=%s", srvURL, tokenStr) + } else { confirmURL = fmt.Sprintf("http://%s/api/v1/auth/confirm?token=%s", r.Host, tokenStr) } body := fmt.Sprintf("Welcome to Verstak Sync!\n\nPlease confirm your email by clicking:\n%s\n\nIf you did not register, ignore this message.", confirmURL) - s.smtpSend(email, "Confirm your Verstak Sync account", body) + if err := s.smtpSend(email, "Confirm your Verstak Sync account", body); err != nil { + log.Printf("register web: failed to send confirm email: %v", err) + } + } else { + log.Printf("register web: SMTP not configured, confirmation token=%s for user %s", tokenStr, username) } w.Header().Set("Content-Type", "text/html; charset=utf-8") w.Write([]byte("Check your email to confirm (or if SMTP not configured, check server logs for the token).
Log in"))