module_settings_act_smtp.go 1.3 KB

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