diff --git a/config/go-fly.sql b/config/go-fly.sql index e3a86f2..4f04119 100644 --- a/config/go-fly.sql +++ b/config/go-fly.sql @@ -112,6 +112,9 @@ INSERT INTO `config` (`id`, `conf_name`, `conf_key`, `conf_value`) VALUES (NULL, INSERT INTO `config` (`id`, `conf_name`, `conf_key`, `conf_value`) VALUES (NULL, '微信小程序Token', 'WeixinToken', '')| INSERT INTO `config` (`id`, `conf_name`, `conf_key`, `conf_value`) VALUES (NULL, '当前小程序审核状态', 'MiniAppAudit', 'yes')| INSERT INTO `config` (`id`, `conf_name`, `conf_key`, `conf_value`) VALUES (NULL, '是否允许上传附件', 'SendAttachment', 'true')| +INSERT INTO `config` (`id`, `conf_name`, `conf_key`, `conf_value`) VALUES (NULL, '发送通知邮件(SMTP地址)', 'NoticeEmailSmtp', '')| +INSERT INTO `config` (`id`, `conf_name`, `conf_key`, `conf_value`) VALUES (NULL, '发送通知邮件(邮箱)', 'NoticeEmailAddress', '')| +INSERT INTO `config` (`id`, `conf_name`, `conf_key`, `conf_value`) VALUES (NULL, '发送通知邮件(密码)', 'NoticeEmailPassword', '')| DROP TABLE IF EXISTS `about`| CREATE TABLE `about` ( diff --git a/controller/folder.go b/controller/folder.go index 0f52ccd..8674178 100644 --- a/controller/folder.go +++ b/controller/folder.go @@ -251,7 +251,7 @@ func FolderSend(w http.ResponseWriter, r *http.Request) { smtpBody := sendData.Body smtpPass := mailServer.Password smtpSubject := sendData.Subject - err = tools.Send(smtpServer, smtpFrom, smtpPass, smtpTo, smtpSubject, smtpBody) + err = tools.SendSmtp(smtpServer, smtpFrom, smtpPass, smtpTo, smtpSubject, smtpBody) if err != nil { msg, _ := json.Marshal(tools.JsonResult{Code: 400, Msg: err.Error()}) w.Write(msg) diff --git a/controller/message.go b/controller/message.go index 0d87457..9f05f6f 100644 --- a/controller/message.go +++ b/controller/message.go @@ -9,6 +9,7 @@ import ( "github.com/taoshihan1991/imaptool/models" "github.com/taoshihan1991/imaptool/tools" "github.com/taoshihan1991/imaptool/ws" + "log" "os" "path" "strconv" @@ -145,6 +146,11 @@ func SendMessageV2(c *gin.Context) { }) return } + kefus, ok := ws.KefuList[kefuInfo.Name] + if !ok || len(kefus) == 0 { + log.Println("客服不在线,发送邮件通知") + go SendNoticeEmail(vistorInfo.Name, content) + } models.CreateMessage(kefuInfo.Name, vistorInfo.VisitorId, content, cType) var msg TypeMessage if cType == "kefu" { diff --git a/controller/shout.go b/controller/shout.go index edc13e4..16a00b1 100644 --- a/controller/shout.go +++ b/controller/shout.go @@ -22,3 +22,15 @@ func SendServerJiang(content string) string { res := tools.Get(url) return res } +func SendNoticeEmail(username, msg string) { + smtp := models.FindConfig("NoticeEmailSmtp") + email := models.FindConfig("NoticeEmailAddress") + password := models.FindConfig("NoticeEmailPassword") + if smtp == "" || email == "" || password == "" { + return + } + err:=tools.SendSmtp(smtp, email, password, []string{email}, "[通知]"+username, msg) + if err!=nil{ + log.Println(err) + } +} diff --git a/controller/visitor.go b/controller/visitor.go index 1bb7d33..8bb4654 100644 --- a/controller/visitor.go +++ b/controller/visitor.go @@ -99,6 +99,7 @@ func PostVisitorLogin(c *gin.Context) { } models.CreateVisitor(name, avator, c.ClientIP(), toId, id, refer, city, client_ip) visitor := models.FindVisitorByVistorId(id) + go SendNoticeEmail(visitor.Name, "来了") c.JSON(200, gin.H{ "code": 200, "msg": "ok", diff --git a/tools/smtp.go b/tools/smtp.go index 301a990..8e0ac04 100644 --- a/tools/smtp.go +++ b/tools/smtp.go @@ -1,17 +1,19 @@ package tools import ( + "encoding/base64" "github.com/emersion/go-sasl" "github.com/emersion/go-smtp" "strings" ) -func Send(server string, from string, password string, to []string, subject string, body string) error { +func SendSmtp(server string, from string, password string, to []string, subject string, body string) error { auth := sasl.NewPlainClient("", from, password) + subjectBase := base64.StdEncoding.EncodeToString([]byte(subject)) msg := strings.NewReader( "From: " + from + "\r\n" + "To: " + strings.Join(to, ",") + "\r\n" + - "Subject: " + subject + "\r\n" + + "Subject: =?UTF-8?B?" + subjectBase + "?=\r\n" + "\r\n" + body + "\r\n") err := smtp.SendMail(server, auth, from, to, msg)