api_configs.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. // Copyright (C) 2019-2023 Nicola Murino
  2. //
  3. // This program is free software: you can redistribute it and/or modify
  4. // it under the terms of the GNU Affero General Public License as published
  5. // by the Free Software Foundation, version 3.
  6. //
  7. // This program is distributed in the hope that it will be useful,
  8. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. // GNU Affero General Public License for more details.
  11. //
  12. // You should have received a copy of the GNU Affero General Public License
  13. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  14. package httpd
  15. import (
  16. "net/http"
  17. "github.com/go-chi/render"
  18. "github.com/drakkan/sftpgo/v2/internal/dataprovider"
  19. "github.com/drakkan/sftpgo/v2/internal/smtp"
  20. )
  21. type smtpTestRequest struct {
  22. smtp.Config
  23. Recipient string `json:"recipient"`
  24. }
  25. func testSMTPConfig(w http.ResponseWriter, r *http.Request) {
  26. r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
  27. var req smtpTestRequest
  28. err := render.DecodeJSON(r.Body, &req)
  29. if err != nil {
  30. sendAPIResponse(w, r, err, "", http.StatusBadRequest)
  31. return
  32. }
  33. if req.Password == redactedSecret {
  34. configs, err := dataprovider.GetConfigs()
  35. if err != nil {
  36. sendAPIResponse(w, r, err, "", http.StatusInternalServerError)
  37. return
  38. }
  39. configs.SetNilsToEmpty()
  40. if err := configs.SMTP.Password.TryDecrypt(); err == nil {
  41. req.Password = configs.SMTP.Password.GetPayload()
  42. }
  43. }
  44. if err := req.SendEmail([]string{req.Recipient}, "SFTPGo - Testing Email Settings",
  45. "It appears your SFTPGo email is setup correctly!", smtp.EmailContentTypeTextPlain); err != nil {
  46. sendAPIResponse(w, r, err, "", http.StatusInternalServerError)
  47. return
  48. }
  49. sendAPIResponse(w, r, nil, "SMTP connection OK", http.StatusOK)
  50. }