邮箱监控异常退出

This commit is contained in:
linghaihui 2023-03-29 11:31:24 +08:00
parent 78419c0d33
commit d25215a7be
2 changed files with 41 additions and 1 deletions

View File

@ -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())
}
}

View File

@ -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")
}