Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions cfg.example.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
"mailServerHost" : "smtp.exmail.qq.com",
"mailServerPort" : 25,
"mailServerAccount" : "anan.nie@tycloudstart.com",
"mailServerPasswd" : ""
"mailServerPasswd" : "",
"tos" : "",
"subject" : "",
"content" : "",
"user" : ""
}
}
}
7 changes: 6 additions & 1 deletion g/cfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package g

import (
"encoding/json"
"github.com/toolkits/file"
"log"
"sync"

"github.com/toolkits/file"
)

type HttpConfig struct {
Expand All @@ -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 {
Expand Down
35 changes: 27 additions & 8 deletions http/sender_api_http.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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")
Expand Down