package notify import ( "crypto/tls" "gopkg.in/gomail.v2" "iot_manager_service/config" "strconv" ) func SendEmail(html string, title string, receiver ...string) error { message := html config := config.Instance().MonitNotice.Email // QQ 邮箱: // SMTP 服务器地址:smtp.qq.com(SSL协议端口:465/994 | 非SSL协议端口:25) // 163 邮箱: // SMTP 服务器地址:smtp.163.com(端口:25) host := config.Host port := config.Port AuthName := config.AuthName password := config.PassWord m := gomail.NewMessage() m.SetHeader("From", AuthName) // 发件人 // m.SetHeader("From", "alias"+"<"+AuthName+">") // 增加发件人别名 m.SetHeader("To", receiver...) // 收件人,可以多个收件人,但必须使用相同的 SMTP 连接 //m.SetHeader("Cc", "******@qq.com") // 抄送,可以多个 //m.SetHeader("Bcc", "******@qq.com") // 暗送,可以多个 m.SetHeader("Subject", title) // 邮件主题 // text/html 的意思是将文件的 content-type 设置为 text/html 的形式,浏览器在获取到这种文件时会自动调用html的解析器对文件进行相应的处理。 // 可以通过 text/html 处理文本格式进行特殊处理,如换行、缩进、加粗等等 //m.SetBody("text/html", fmt.Sprintf(message, "testAuth")) m.SetBody("text/html", message) // text/plain的意思是将文件设置为纯文本的形式,浏览器在获取到这种文件时并不会对其进行处理 // m.SetBody("text/plain", "纯文本") // m.Attach("test.sh") // 附件文件,可以是文件,照片,视频等等 // m.Attach("lolcatVideo.mp4") // 视频 // m.Attach("lolcat.jpg") // 照片 portInt, _ := strconv.Atoi(port) d := gomail.NewDialer( host, portInt, AuthName, password, ) // 关闭SSL协议认证 d.TLSConfig = &tls.Config{InsecureSkipVerify: true} if err := d.DialAndSend(m); err != nil { return err } return nil }