package utils import ( "github.com/sirupsen/logrus" mail "github.com/xhit/go-simple-mail/v2" "log" ) var emailClient = func() *mail.SMTPServer { // 创建邮件客户端 client := mail.NewSMTPClient() // 设置SMTP服务器和认证信息 client.Host = "smtp.163.com" // 替换为你的SMTP服务器地址 client.Port = 25 // 替换为SMTP服务器端口 client.Username = "17673481176@163.com" // 替换为SMTP服务器的用户名 client.Password = "kk992471" // 替换为SMTP服务器的密码 return client }() func EmailPicture(To, subject, data string, pic *mail.File) { // 创建邮件客户端 smtpClient, err := emailClient.Connect() if err != nil { logrus.Error("email client Connect err", err) return } // 创建邮件对象 email := mail.NewMSG() // 设置发件人 email.SetFrom("17673481176@163.com") // 设置收件人 email.AddTo(To) // 设置主题 email.SetSubject(subject) // 设置html文本内容 email.SetBody(mail.TextHTML, data) // 添加图片附件 if pic != nil { email.Attach(pic) } // 发送邮件 err = email.Send(smtpClient) if err != nil { log.Println("116 Failed to send email:", err) return } log.Println("Email sent successfully") } func Email(To, subject string, body string) { smtpClient, err := emailClient.Connect() if err != nil { logrus.Error("email client connect err", err) return } // 创建邮件对象 email := mail.NewMSG() // 设置发件人 email.SetFrom("17673481176@163.com") // 设置收件人 email.AddTo(To) // 设置主题 email.SetSubject(subject) // 设置纯文本内容 email.SetBody(mail.TextHTML, body) // 发送邮件 err = email.Send(smtpClient) if err != nil { log.Println("116 Failed to send email:", err) return } log.Println("Email sent successfully") } func ErrorToEmail(subject string, body string) error { //to := strings.Split(global.GlobalConfig.To, ",") //if to[len(to)-1] == "" { // 判断切片的最后一个元素是否为空,为空则移除 // to = to[:len(to)-1] //} //return send(to, subject, body) return nil }