http_debug.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "html/template"
  6. "io"
  7. "net/http"
  8. "path/filepath"
  9. "strings"
  10. )
  11. func debug_sendmail(resp http.ResponseWriter, req *http.Request) {
  12. sess, ok := checkLogin(resp, req)
  13. if !ok {
  14. return
  15. }
  16. req.ParseForm()
  17. if req.Method == "GET" {
  18. //视图输出
  19. files := []string{
  20. filepath.Join(Cfg.TmplDir, "debug", "sendmail.tmpl"),
  21. filepath.Join(Cfg.TmplDir, "header.tmpl"),
  22. filepath.Join(Cfg.TmplDir, "navbar.tmpl"),
  23. }
  24. tmpl, err := template.New("sendmail.tmpl").Funcs(TmplFuncMap).ParseFiles(files...)
  25. if err != nil {
  26. io.WriteString(resp, fmt.Sprintf("Error: %s\n", err.Error()))
  27. } else {
  28. tmpl.Execute(resp, struct {
  29. Sess *Session
  30. Req *http.Request
  31. Title string
  32. }{
  33. sess,
  34. req,
  35. "Debug - 邮件测试",
  36. })
  37. }
  38. } else {
  39. sendto := req.PostForm.Get("sendto")
  40. title := req.PostForm.Get("title")
  41. content := req.PostForm.Get("content")
  42. if sendto == "" {
  43. ret, _ := json.Marshal(ErrorRet{Errno: 1, Error: "收件人不能为空"})
  44. resp.Write(ret)
  45. return
  46. }
  47. if title == "" {
  48. ret, _ := json.Marshal(ErrorRet{Errno: 2, Error: "标题不能为空"})
  49. resp.Write(ret)
  50. return
  51. }
  52. if content == "" {
  53. ret, _ := json.Marshal(ErrorRet{Errno: 3, Error: "内容不能为空"})
  54. resp.Write(ret)
  55. return
  56. }
  57. req := MailRequest{
  58. sendto: strings.Split(sendto, ";"),
  59. title: title,
  60. content: content,
  61. }
  62. Sender.Push(&req)
  63. ret, _ := json.Marshal(ErrorRet{Errno: 0, Error: ""})
  64. resp.Write(ret)
  65. }
  66. }