module_settings_act_smtp.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. package modules
  2. import (
  3. "strings"
  4. "golang-fave/engine/wrapper"
  5. "golang-fave/utils"
  6. )
  7. func (this *Modules) RegisterAction_SettingsSmtp() *Action {
  8. return this.newAction(AInfo{
  9. WantDB: true,
  10. Mount: "settings-smtp",
  11. WantAdmin: true,
  12. }, func(wrap *wrapper.Wrapper) {
  13. pf_smtp_host := strings.TrimSpace(wrap.R.FormValue("smtp-host"))
  14. pf_smtp_port := strings.TrimSpace(wrap.R.FormValue("smtp-port"))
  15. pf_smtp_login := strings.TrimSpace(wrap.R.FormValue("smtp-login"))
  16. pf_smtp_password := strings.TrimSpace(wrap.R.FormValue("smtp-password"))
  17. pf_smtp_test_email := strings.TrimSpace(wrap.R.FormValue("smtp-test-email"))
  18. (*wrap.Config).SMTP.Host = pf_smtp_host
  19. (*wrap.Config).SMTP.Port = utils.StrToInt(pf_smtp_port)
  20. (*wrap.Config).SMTP.Login = pf_smtp_login
  21. // Update password only if not empty
  22. if pf_smtp_password != "" {
  23. (*wrap.Config).SMTP.Password = pf_smtp_password
  24. }
  25. if err := wrap.ConfigSave(); err != nil {
  26. wrap.MsgError(err.Error())
  27. return
  28. }
  29. // Send test message
  30. if pf_smtp_test_email != "" {
  31. if err := wrap.SendEmail(
  32. pf_smtp_test_email,
  33. "❤️ Fave.Pro SMTP test message",
  34. "Hello! This is Fave.Pro test message.<br />If you see this message, then you right configured SMTP settings!",
  35. ); err != nil {
  36. wrap.MsgError(err.Error())
  37. return
  38. }
  39. }
  40. // Reload current page
  41. wrap.Write(`window.location.reload(false);`)
  42. })
  43. }