package utils import ( "fmt" "gopkg.in/gomail.v2" "mime" "strings" ) //发送邮件 func SendEmail(title, content string, touser string) bool { var arr []string sub := strings.Index(touser, ";") if sub >= 0 { spArr := strings.Split(touser, ";") for _, v := range spArr { arr = append(arr, v) } } else { arr = append(arr, touser) } //mailConn := map[string]string{ // "user": "18170239278@163.com", // "pass": "JYWLATLVZJROWMWZ", // "host": "smtp.163.com", // "port": "465", //} //mailPort, _ := strconv.Atoi(mailConn["port"]) //转换端口类型为int m := gomail.NewMessage() //m.SetHeader("From", mailConn["user"]) m.SetHeader("From", m.FormatAddress("qunao007@qq.com", APPNAME)) //这种方式可以添加别名,即“XX官方” // 说明:如果是用网易邮箱账号发送,以下方法别名可以是中文,如果是qq企业邮箱,以下方法用中文别名,会报错,需要用上面此方法转码 //m.SetHeader("From", "FB Sample"+"<"+mailConn["user"]+">") //这种方式可以添加别名,即“FB Sample”, 也可以直接用m.SetHeader("From",mailConn["user"]) 读者可以自行实验下效果 //m.SetHeader("From", mailConn["user"]) m.SetHeader("To", arr...) m.SetHeader("Subject", title+" "+GetRandString(16)) m.SetBody("text/html", content) d := gomail.NewDialer("smtp.qq.com", 587, "qunao007@qq.com", "cyhjgffhaobteihd") //d := gomail.NewDialer(mailConn["host"], mailPort, mailConn["user"], mailConn["pass"]) if err := d.DialAndSend(m); err != nil { fmt.Println("err:", err) return false } return true } //发送邮件 func SendEmailByHongze(title, content string, touser, attachPath, attachName string) bool { var arr []string sub := strings.Index(touser, ";") if sub >= 0 { spArr := strings.Split(touser, ";") for _, v := range spArr { arr = append(arr, v) } } else { arr = append(arr, touser) } m := gomail.NewMessage() m.SetHeader("From", "public@hzinsights.com") m.SetHeader("To", arr...) m.SetHeader("Subject", title+" "+GetRandStringNoSpecialChar(8)) m.SetBody("text/html", content) //body := new(bytes.Buffer) if attachPath != "" { m.Attach(attachPath, gomail.Rename(attachName), gomail.SetHeader(map[string][]string{ "Content-Disposition": { fmt.Sprintf(`attachment; filename="%s"`, mime.QEncoding.Encode("UTF-8", attachName)), }, })) } d := gomail.NewDialer("smtp.mxhichina.com", 465, "public@hzinsights.com", "Hzinsights2018") if err := d.DialAndSend(m); err != nil { fmt.Println("send err:", err.Error()) return false } return true }