diff --git a/cfg.example.json b/cfg.example.json index 0e44024..f56efc7 100644 --- a/cfg.example.json +++ b/cfg.example.json @@ -16,6 +16,10 @@ "mailServerHost" : "smtp.exmail.qq.com", "mailServerPort" : 25, "mailServerAccount" : "anan.nie@tycloudstart.com", - "mailServerPasswd" : "" + "mailServerPasswd" : "", + "tos" : "", + "subject" : "", + "content" : "", + "user" : "" } -} \ No newline at end of file +} diff --git a/g/cfg.go b/g/cfg.go index e63afc4..31ea000 100644 --- a/g/cfg.go +++ b/g/cfg.go @@ -2,9 +2,10 @@ package g import ( "encoding/json" - "github.com/toolkits/file" "log" "sync" + + "github.com/toolkits/file" ) type HttpConfig struct { @@ -26,6 +27,10 @@ type MailConfig struct { MailServerPort int `json:"mailServerPort"` MailServerAccount string `json:"mailServerAccount"` MailServerPasswd string `json:"mailServerPasswd"` + Tos string `json:"tos"` + Subject string `json:"subject"` + Content string `json:"content"` + User string `json:"user"` } type GlobalConfig struct { diff --git a/http/sender_api_http.go b/http/sender_api_http.go index b45898e..df404d5 100644 --- a/http/sender_api_http.go +++ b/http/sender_api_http.go @@ -2,10 +2,12 @@ package http import ( "fmt" - "github.com/niean/mailsender/proc" - "github.com/niean/mailsender/sender" "net/http" "strings" + + "github.com/niean/mailsender/g" + "github.com/niean/mailsender/proc" + "github.com/niean/mailsender/sender" ) func configMailSenderApiRoutes() { @@ -19,30 +21,47 @@ func configMailSenderApiRoutes() { return } + cfg := g.GetConfig() req.ParseForm() params := req.Form content, exist := params["content"] if !exist || len(content[0]) < 1 { - RenderDataJson(w, "bad content") - return + if len(cfg.Mail.Content) > 0 { + content = append(content, cfg.Mail.Content) + } else { + RenderDataJson(w, "bad content") + return + } } subject, exist := params["subject"] if !exist || len(subject[0]) < 1 { - RenderDataJson(w, "bad subject") - return + if len(cfg.Mail.Subject) > 0 { + subject = append(subject, cfg.Mail.Subject) + } else { + RenderDataJson(w, "bad subject") + return + } } tos, exist := params["tos"] if !exist || len(tos[0]) < 1 { - RenderDataJson(w, "bad tos") - return + if len(cfg.Mail.Tos) > 0 { + tos = append(tos, cfg.Mail.Tos) + } else { + RenderDataJson(w, "bad tos") + return + } } from := []string{} fromUser, exist := params["user"] if exist && len(fromUser[0]) > 0 { from = append(from, fromUser[0]) + } else { + if len(cfg.Mail.User) > 0 { + from = append(from, cfg.Mail.User) + } } if ok := sender.AddMail(strings.Split(tos[0], ","), subject[0], content[0], from...); !ok { RenderDataJson(w, "error, service busy")