123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134 |
- package utils
- import (
- "fmt"
- 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) {
- // 创建邮件客户端
- //client := mail.NewSMTPClient()
- //// 设置SMTP服务器和认证信息
- //client.Host = "smtp.163.com" // 替换为你的SMTP服务器地址
- //client.Port = 25 // 替换为SMTP服务器端口
- //client.Username = "17673481176@163.com" // 替换为SMTP服务器的用户名
- //client.Password = "kk992471" // 替换为SMTP服务器的密码
- smtpClient, err := emailClient.Connect()
- if err != nil {
- fmt.Println("client.Connect()", err)
- }
- // 创建邮件对象
- 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")
- }
- //@author: [maplepie](https://github.com/maplepie)
- //@function: Email
- //@description: Email发送方法
- //@param: subject string, body string
- //@return: error
- func Email(To, subject string, body string) {
- smtpClient, err := emailClient.Connect()
- if err != nil {
- fmt.Println("client.Connect()", err)
- }
- // 创建邮件对象
- 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")
- }
- //@author: [SliverHorn](https://github.com/SliverHorn)
- //@function: ErrorToEmail
- //@description: 给email中间件错误发送邮件到指定邮箱
- //@param: subject string, body string
- //@return: error
- 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
- }
- func send(to []string, subject string, body string) error {
- //from := global.GlobalConfig.From
- //nickname := global.GlobalConfig.Nickname
- //secret := global.GlobalConfig.Secret
- //host := global.GlobalConfig.Host
- //port := global.GlobalConfig.Port
- //isSSL := global.GlobalConfig.IsSSL
- //
- //auth := smtp.PlainAuth("", from, secret, host)
- //e := email.NewEmail()
- //if nickname != "" {
- // e.From = fmt.Sprintf("%s <%s>", nickname, from)
- //} else {
- // e.From = from
- //}
- //e.To = to
- //e.Subject = subject
- //e.HTML = []byte(body)
- //var err error
- //hostAddr := fmt.Sprintf("%s:%d", host, port)
- //if isSSL {
- // err = e.SendWithTLS(hostAddr, auth, &tls.Config{ServerName: host})
- //} else {
- // err = e.Send(hostAddr, auth)
- //}
- //return err
- return nil
- }
|