From d25215a7be8f1a60bdc6a1c06f15ea1613844fea Mon Sep 17 00:00:00 2001 From: linghaihui <75124771@qq.com> Date: Wed, 29 Mar 2023 11:31:24 +0800 Subject: [PATCH] =?UTF-8?q?=E9=82=AE=E7=AE=B1=E7=9B=91=E6=8E=A7=E5=BC=82?= =?UTF-8?q?=E5=B8=B8=E9=80=80=E5=87=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- wechatbot/bootstrap/bootstrap.go | 5 ++++- wechatbot/utils/send_mail.go | 37 ++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 wechatbot/utils/send_mail.go diff --git a/wechatbot/bootstrap/bootstrap.go b/wechatbot/bootstrap/bootstrap.go index bd59cd1..b7ea463 100644 --- a/wechatbot/bootstrap/bootstrap.go +++ b/wechatbot/bootstrap/bootstrap.go @@ -4,6 +4,7 @@ import ( "log" "github.com/bujnlc8/wechatbot/handlers" + "github.com/bujnlc8/wechatbot/utils" "github.com/eatmoreapple/openwechat" ) @@ -27,5 +28,7 @@ func Run() { } } // 阻塞主goroutine, 直到发生异常或者用户主动退出 - bot.Block() + if err := bot.Block(); err != nil { + utils.SendSimpleEmail("[!]wechatbot异常退出", err.Error()) + } } diff --git a/wechatbot/utils/send_mail.go b/wechatbot/utils/send_mail.go new file mode 100644 index 0000000..a4e57a5 --- /dev/null +++ b/wechatbot/utils/send_mail.go @@ -0,0 +1,37 @@ +package utils + +import ( + "log" + "net/smtp" + "os" +) + +const ( + SMTP_SERVER = "smtp.qq.com" + SMTP_PORT = "587" +) + +// 用qq邮箱发送邮件,从New Bing写的代码修改而来 +func SendSimpleEmail(subject string, body string) { + sender := os.Getenv("EMAIL_SENDER") + passwd := os.Getenv("EMAIL_PASSWD") + recipient := os.Getenv("EMAIL_RECIPIENT") + if len(recipient) == 0 { + recipient = sender + } + auth := smtp.PlainAuth("", sender, passwd, SMTP_SERVER) + header := make(map[string]string) + header["To"] = recipient + header["From"] = sender + header["Subject"] = subject + headerStr := "" + for k, v := range header { + headerStr += k + ": " + v + "\r\n" + } + message := headerStr + "\r\n\r\n" + body + err := smtp.SendMail(SMTP_SERVER+":"+SMTP_PORT, auth, sender, []string{recipient}, []byte(message)) + if err != nil { + log.Fatal(err) + } + log.Println("Email sent successfully") +}